Reading from a file

Hey guys, Im trying to read from a file, line by line, with no specified number of values in the file. Check out my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    string x;
    ifstream fin;
    int count = 0;
    char ch;

    fin.open("CWC_Master.txt");
    if(!fin)
    {
        cout << "File has failed to open" << endl;
        return 1;
    }

    while(fin >> x && !fin.eof())
    {
        fin.get(ch);
        getline(fin, x);
        count++;
        cout << x << " " << endl;
    }


Now, this works great! However, its skipping some lines. And I dont know why. For example: Lets say that the input file is:

superman toy
sm2335
19.99
batman toy
bm5532
25.99
aquaman toy
am6786
26.00

Where it should output the above, instead it outputs every other one. Like:
superman toy
19.99
batman toy
25.99
aquaman toy
26.00


How can I fix my code so that it SIMPLY(i say simply because I am still a beginner coder) can read line by line?

Thanks for the help!
Please do not post more than once: http://www.cplusplus.com/forum/beginner/162125/
Different program. Different problem. Last was trying to read into an array. This is gathering info from an input file.


I also didnt feel like I received much understanding from the last post concerning a different issue.
I was confused because you are opening the same file as before.

The issue is because your code does not match the file format. Your file format appears to be sets of 3 lines where the first two lines are strings and the third is a number. You should read input like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(std::ifstream in ("CWC_Master.txt"))
{
    std::string line1, line2, line3;
    while(std::getline(in, line1) && std::getline(in, line2) && std::getline(in, line3))
    {
        //line1 and line2 are ready for processing, but we need to extract the number from line 3
        std::istringstream iss (line3); //#include <sstream>
        double num;
        iss >> num;
        //now num has the number in it
        std::cout << line1 << " " << line2 << " " << num << std::endl;
    }
}
else
{
    std::cerr << "Unable to open file." << std::endl;
}
Last edited on
I'm a little confused about your code, if Im to be honest. Im sure its standard stuff, but I fresh into c++.

What alterations to my code could read the line by line? I also dont think I understand what you are saying about the file formatting is wrong. Because the getline should get the line no matter what the data type is, correct?

And also, im outputting numbers and strings. just not line by line. I feel like im missing one line of code somewhere..
ouput all contents of file to console:
1
2
3
4
5
6
7
8
9
10
11
12
int main () {	
		
	ifstream fin("CWC_Master.txt");
	string s;
	
	while(getline(fin,s)) {
		cout << s;
		if(!fin.eof()) cout << "\n";
	}

return 0;
}

This is your original code:[quote=Thecal]
15
16
17
18
19
20
21
    while(fin >> x && !fin.eof())
    {
        fin.get(ch);
        getline(fin, x);
        count++;
        cout << x << " " << endl;
    }
You first try to read a single word from the file, then you try to read an entire line into the same variable. Then you print it. This doesn't match the file content at all.
Topic archived. No new replies allowed.