user3050163
user3050163

Reputation: 135

Read from file in c++ till end of line?

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

Upvotes: 9

Views: 82218

Answers (4)

user7925034
user7925034

Reputation:

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

Upvotes: 0

Rakia
Rakia

Reputation: 11

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

Upvotes: 1

herohuyongtao
herohuyongtao

Reputation: 50657

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

Upvotes: 12

Some programmer dude
Some programmer dude

Reputation: 409136

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

Upvotes: 2

Related Questions