9

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

user3050163
  • 135
  • 1
  • 1
  • 8
  • Why do you need to read until the end of the line? And by "end of the line" do you mean the end of the *first* line, or the end of all the lines? – 0x499602D2 Jan 24 '14 at 14:09
  • by "end of line" i mean read the first line,use the numbers to do what i have to and go on the second line...,then on the third line until the end of the file – user3050163 Jan 24 '14 at 14:20
  • Are you performing the same operations on each line? – 0x499602D2 Jan 24 '14 at 14:21
  • Do any of the current answers help? Please give feedback. – 0x499602D2 Jan 24 '14 at 14:34
  • No,they don't help. the code which herohuyongtao writes works incorrect. i will repeat:i want to take one by one the elemnts from the 1st line till the end and do operations with them. Then i do the same thing and same operations with the 2nd line .... – user3050163 Jan 24 '14 at 14:40
  • On the contrary, what you described is *extactly* what herohuyongtao's code does. Maybe you should explain what you believe his example does incorrectly? – 0x499602D2 Jan 24 '14 at 14:45
  • I would like to submit an answer, but first I need to know more information about what you're trying to do. herohuyongtao's answer goes through each line and then uses a string stream to iterate over the integers of that line. If that isn't satisfactory for you, I would need to have more detail on what's trying to be done. – 0x499602D2 Jan 24 '14 at 14:58
  • @0x499602D2 herohuyongtao's answer is returning random values i take the elements one by one and the result is 1 2 3 2 30 1 55 1... how is this happening?i think it must have another solution – user3050163 Jan 24 '14 at 15:03
  • [The example here](http://ideone.com/aHUR3X) shows that his code is working as expected. Does the code posted there differ from your code? – 0x499602D2 Jan 24 '14 at 15:15
  • @0x499602D2 ok,now it works ,thank you very much. – user3050163 Jan 24 '14 at 15:58

4 Answers4

10

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?.

Community
  • 1
  • 1
herohuyongtao
  • 45,575
  • 23
  • 118
  • 159
2

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(...)).

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
  • @user3050163 Read into a `std::string`, put in a `std::istringstream`, loop using the input operator `>>` to get each whitespace-delimited value. – Some programmer dude Jan 24 '14 at 13:42
  • @user3050163 Or after you read the line and put it in an [`std::istringstream`](http://en.cppreference.com/w/cpp/io/basic_istringstream), use [`std::copy`](http://en.cppreference.com/w/cpp/algorithm/copy) with [`std::istream_iterator`](http://en.cppreference.com/w/cpp/iterator/istream_iterator) to put the values directly into a [container](http://en.cppreference.com/w/cpp/container) using [`std::back_inserter`](http://en.cppreference.com/w/cpp/iterator/back_inserter). – Some programmer dude Jan 24 '14 at 13:44
1
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
Rakia
  • 11
  • 1
  • You can use this to read until the rest of the line: string line;getline(stream, line); . I use it to read the remaining part of the file – mathengineer May 30 '19 at 18:43
0

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;
}