if i have a file like
1 5 6 9 7 1
2 5 4 3 8 9
3 4 3 2 1 6
4 1 3 6 5 4
and i want to sort the numbers in every line ..
how to know when there is a newline ?
code for example :
while (!input.eof) {
input>>num;
while (not new line ?){
input>>o;
b.push_back(o);
}
sort (b.begin(),b.end());
se=b.size();
output<<num<<" "<<b[se-1]<<endl;
b.clear();
}
Note: i tried while(input>>num) and getline will now work with me
any ideas ?
Your input doesn't work! Using a loop testing for stream.eof() as the only control for the input is always wrong. You always need to test your input after you tried to read it. Incidentally, I posted earlier how you can guarantee that there is no newline between to objects. there is already an answer using std::getline() as a first stage which is somewhat boring. Here is an alternate approach:
std::istream& noeol(std::istream& in) {
for (int c; (c = in.peek()) != std::char_traits<char>::eof()
&& std::isspace(c); in.get()) {
if (c == '\n') {
in.setstate(std::ios_base::failbit);
}
}
return in;
}
// ...
while (input >> num) {
do {
b.push_back(num);
} while (input >> noeol >> num);
std::sort (b.begin(),b.end());
se=b.size();
output<<num<<" "<<b[se-1]<<endl;
b.clear();
input.clear();
}
You can use std::getline together with std::istringstream to read the file line by line, and then process each line individually:
#include <sstream>
std::string line;
while (std::getline(infile, line))
{
std::istringstream iss(line);
int a, b;
//You can now read the numbers in the current line from iss
}
For further reference on how to read the file line by line, see this post.
Related
How can i read data untill end of line?I have a text file "file.txt" with this
1 5 9 2 59 4 6
2 1 2
3 2 30 1 55
I have this code:
ifstream file("file.txt",ios::in);
while(!file.eof())
{
....//my functions(1)
while(?????)//Here i want to write :while (!end of file)
{
...//my functions(2)
}
}
in my functions(2) i use the data from the lines and it need to be Int ,not char
Don't use while(!file.eof()) as eof() will only be set after reading the end of the file. It does not indicate, that the next read will be the end of the file. You can use while(getline(...)) instead and combine with istringstream to read numbers.
#include <fstream>
#include <sstream>
using namespace std;
// ... ...
ifstream file("file.txt",ios::in);
if (file.good())
{
string str;
while(getline(file, str))
{
istringstream ss(str);
int num;
while(ss >> num)
{
// ... you now get a number ...
}
}
}
You need to read Why is iostream::eof inside a loop condition considered wrong?.
As for reading until the end of the line. there's std::getline.
You have another problem though, and that is that you loop while (!file.eof()) which will most likely not work as you expect. The reason is that the eofbit flag is not set until after you try to read from beyond the end of the file. Instead you should do e.g. while (std::getline(...)).
char eoln(fstream &stream) // C++ code Return End of Line
{
if (stream.eof()) return 1; // True end of file
long curpos; char ch;
curpos = stream.tellp(); // Get current position
stream.get(ch); // Get next char
stream.clear(); // Fix bug in VC 6.0
stream.seekp(curpos); // Return to prev position
if ((int)ch != 10) // if (ch) eq 10
return 0; // False not end of row (line)
else // (if have spaces?)
stream.get(ch); // Go to next row
return 1; // True end of row (line)
} // End function
If you want to write it as function in order to call some where, you can use a vector. This is a function which I use to read such file and return integers element wise.
vector<unsigned long long> Hash_file_read(){
int frames_sec = 25;
vector<unsigned long long> numbers;
ifstream my_file("E:\\Sanduni_projects\\testing\\Hash_file.txt", std::ifstream::binary);
if (my_file) {
//ifstream file;
string line;
for (int i = 0; i < frames_sec; i++){
getline(my_file, line);
numbers.push_back(stoull(line));
}
}
else{
cout << "File can not be opened" << endl;
}
return numbers;
}
I am new to c++.My aim is to read a file containing all integers, like
1 2 3\n 1 3 4\n 1 2 4\n
and
1,2 3,4 5,6\n 1,3 3,4 3,5\n 1,3 3,4 4,2\n
I could use getline to read them, but how could I split it into an integer array. Like array[3]={1,2,3} and array2={1,2,3,4,5,6} for the first line reading result? Sorry I forgot to mention that I was trying to not use the STLs for C++
You can do it without boost spirit
// for single line:
std::vector<int> readMagicInts(std::istream &input)
{
std::vector<int> result;
int x;
while(true) {
if (!(input >> x)) {
char separator;
input.clear(); // clear error flags;
if (!(input >> separator >> x)) break;
if (separator != ',') break;
}
result.push_back(x);
}
return result;
}
std::vector<std::vector<int>> readMagicLinesWithInts(std::istream &input)
{
std::vector<std::vector<int>> result;
std::string line;
while (std::getline(input, line)) {
std::istringstream lineData(line);
result.push_back(readMagicInts(lineData));
}
return result;
}
I want to read an input like this from file
sphere 3 2 3 4
pyramid 2 3 4 12 3 5 6 7 3 2 4 1 2 3
rectangle 2 3 4 1 9 12
I want to do something like this
char name[64];
int arr[12];
ifstream file (..);
while(file)
{
file >> name;
while( //reach end of line)
file >> arr[i]
}
As you can see I don't know how many integers will be entered, that's why I want to stop at new line. I did it with getline, and then splitting the line, but they told me it can be done only with >> operator.
Note: I can't use std::string or std::vector.
The simple version is to use a manipulator similar to std::ws but instead of skipping all whitespace setting std::ios_base::failbit when a newline is encountered. This manipulator would then be used to instead of skipping whitespace implicitly whitespace other than newlines are skipped. For example (the code isn't test but I think something like this with the bugs and compilation errors removed should work):
std::istream& my_ws(std::istream& in) {
std::istream::sentry kerberos(in);
while (isspace(in.peek())) {
if (in.get() == '\n') {
in.setstate(std::ios_base::failbit);
}
}
return in;
}
// ...
char name[64];
int array[12];
while (in >> std::setw(sizeof(name)) >> name) { // see (*) below
int* it = std::begin(array), end = std::end(array);
while (it != end && in >> my_ws >> *it) {
++it;
}
if (it != end && in) { deal_with_the_array_being_full(); }
else {
do_something_with_the_data(std::begin(array), it);
if (!in.eof()) { in.clear(); }
}
}
My personal guess is that the assignment asked for reading the values into char arrays followed by converting them using atoi() or strol(). I think that would be a boring solution to the exercise.
(*) Never, not even in exmaple code, use the formatted input operator with a char array array without also setting the maximum allowed size! The size can be set by setting the stream's width(), e.g., using the manipulator std::setw(sizeof(array)). If the width() is 0 when using the formatted input operator with a char array, an arbitrary number of non-whitespace characters is read. This can easily overflow the array and become a security problem! Essentially, this is the C++ way of spelling C's gets() (which is now removed from both the C and the C++ standard libraries).
I suppose that you can use peek method:
while (file)
{
file >> name;
int i = 0;
while(file.peek() != '\n' && file.peek() != EOF) {
file >> arr[i++];
}
}
I have a text file, that is formatted somewhat like this:
1 3 4 5 6
6 7 8
4 12 16 17 18 19 20
20
0
A line can contain 1 to 10000 integers. What I need to do, is read all of them line by line.
Pseudocode like this:
line=0;
i=0;
while(!file.eof()){
while(!endLine){
array[0][i++]=file.readChar();
}
line++;i=0;
}
So, I have an array , into which I would like to read every line, and each line would consist of each of these integers.
The problem I'm having, is how to check if the end of a line has come.
Note, I can't use strings.
Yes, This is for a homework, but the main task for the assignment is to build a tree and then transform it. I can do that, but I've no idea how to read the integers from the file.
Probably something like this:
after reading an int, I manually skip spaces, tabs, carriage return and end of line (for this one you'll have to implement your logic).
To read an int I read it directly using the C++ functions of ifstream. I don't read it character by character and then recompose it as a string :-)
Note that I skip \r as "spaces. The end of line for me is \n.
#include <iostream>
#include <fstream>
#include <vector>
int main()
{
std::ifstream file("example.txt");
std::vector<std::vector<int>> ints;
bool insertNewLine = true;
int oneInt;
//The good() here is used to check the status of
//the opening of file and for the failures of
//peek() and read() (used later to skip characters).
while (file.good() && file >> oneInt)
{
if (insertNewLine)
{
std::vector<int> vc;
ints.push_back(vc);
//With C++11 you can do this instead of the push_back
//ints.emplace_back(std::vector<int>());
insertNewLine = false;
}
ints.back().push_back(oneInt);
std::cout << oneInt << " ";
int ch;
while ((ch = file.peek()) != std::char_traits<char>::eof())
{
if (ch == ' '|| ch == '\t' || ch == '\r' || ch == '\n')
{
char ch2;
if (!file.read(&ch2, 1))
{
break;
}
if (ch == '\n' && !insertNewLine)
{
std::cout << std::endl;
insertNewLine = true;
}
}
else
{
break;
}
}
}
//Here we should probably check if we exited for eof (good)
//or for other file errors (bad! bad! bad!)
return 0;
}
There is a function called getline() which will read a whole line. Link
You need a function to read a value from a file or indicates an end of line or end of file condition, something like:
result_type GetNextValue (input_file, &value)
{
if next thing in file is a number, set value and return number_type
if next thing in file is an end of line, return end_of_line_type
if end of file found, return end_of_file_type
}
and then your array building loop becomes:
line = 0
item = 0
eof = false
while (!eof)
{
switch (GetNextValue (input_file, value))
{
case value_type:
array [line][item++] = value
case end_of_line_type:
line++;
item = 0;
case end_of_file_type:
eof = true
}
}
I'll leave the details to you as it's homework.
You could read the numbers in a char and check against carriage return. A snippet that I had just tried is given below:
ifstream ifile;
ifile.open("a.txt");
char ch;
while((ch = ifile.get()) != EOF)
{
std::cout<<ch<<"\n";
if (ch == '\n')
std::cout<<"Got New Line";
}
ifile.close();
My test file has data like this:
1
2
3
0
1, 2
3, 4
0, 0
4, 3
2, 1
0, 0
How would I separate the data by line but also separate each section of data by the zeros.
ifstream data("testData.txt");
string line, a, b;
while(getline(data,line))
{
stringstream str(line);
istringstream ins;
ins.str(line);
ins >> a >> b;
hold.push_back(a);
hold.push_back(b);
}
How do I separate them by the zeros?
First of all, I would try to improve the problem definition ☺
So the lines are significant, and the zero-delimited lists of numbers are also significant? Try something like this:
std::ifstream data("testData.txt");
std::vector<int> hold;
std::string line;
std::vector<std::string> lines;
while(std::getline(data,line))
{
lines.push_back(line);
std::stringstream str(line);
// Read an int and the next character as long as there is one
while (str.good())
{
int val;
char c;
str >> val >> c;
if (val == 0)
{
do_something(hold);
hold.clear();
}
else
hold.push_back(val);
}
}
This isn't very fault-tolerant, but it works. It relies on a single character (a comma) to be present after every number except the last one on each line.
When you're done you have
[1,2,3,0,1,2,3,4,0,0,4,3,2,1,0,0]
How about using std::find()?