C++ not reading number values from text file

Part of my homework deals with reading floating-point values from a text file, and then outputting them. As such, I have two "float" variables into which data from my ifstream is supposed to be read. However, that is not happening. My stream did not fail. The text file itself just contains:
180.5 200

I'm coding the program in a virtual machine, though, so I'll have to log into the forums via the VM to paste the code.
I'm coding the program in a virtual machine, though, so I'll have to log into the forums via the VM to paste the code.


Really nothing we can do to help you then at this point. Just post your code in this thread when possible :)

Edit:

If you were going to get the code 3 minutes later, you could have just posted 3 minutes later with the actual code lol >.>
Last edited on
OK, here's the code:

1
2
3
4
5
6
7
8
9
10
11
	ifstream assmtScoreIStream;
	assmtScoreIStream.open(assmtScoreLocation);
	float studentScore;
	int totalPossibleScore;
	if (!assmtScoreIStream.fail()) {
	assmtScoreIStream >> studentScore >> totalPossibleScore;
	}
	else
	{
		cout << "Fail";
	}
Since nothing is happening, I suspect something is wrong with the path to your file.

Also, just use if (assmtScoreIStream.is_open()) instead
Last edited on
@TarikNeaj,

The file appears to be opening just fine. Oh, and after the >> line, studentScore is -0 and totalPossibleScore is some big, negative number. Except when I initialize it to 0 in the declaration line, in which case it's 0.

Also, if I use this code:
1
2
string fileInput;
		assmtScoreIStream >> fileInput;

instead of this code:
 
assmtScoreIStream >> studentScore >> totalPossibleScore;


"fileInput" ends up being 0.
Last edited on
When you open a file it's a good idea to put in a check to make sure it's open.
@SamuelAdams: I did that, but nothing different happened.
What does { std::cout << "contents of file:\n" << std::ifstream(assmtScoreLocation).rdbuf() ; } print out?
@JLBorges: I get -1.#IND
> I get -1.#IND

There is no way you could get that.

With { std::cout << "contents of file:\n" << ... }
The output must start with the line
contents of file:
@JLBorges: I meant after the "contents of file:" line
The file does not contain the expected
180.5 200


It appears to have been generated by a (badly written) program.

Verify that assmtScoreLocation has been initialised correctly (that it contains the correct path to the input file.)
@JLBorges: Notepad on Windows 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <string>

void test_it( std::string assmtScoreLocation )
{
    { std::cout << "contents of file:\n" << std::ifstream(assmtScoreLocation).rdbuf() ; }
    std::cout << "\n\n" ;

    float studentScore;
    int totalPossibleScore;

    std::ifstream assmtScoreIStream(assmtScoreLocation) ;

    if( assmtScoreIStream >> studentScore ) std::cout << "first number: " << studentScore << '\n' ;
    else std::cerr << "input of first number failed\n" ;

    if( assmtScoreIStream >> totalPossibleScore ) std::cout << "second number: " << totalPossibleScore << '\n' ;
    else std::cerr << "input of second number failed\n" ;
}

int main()
{
    const std::string assmtScoreLocation = "test_file.txt" ;

    // bad input file
    {
        { std::ofstream(assmtScoreLocation) << "-1.#IND" ; }

        test_it(assmtScoreLocation) ;
    }

    std::cout << "\n------------------\n\n" ;

    // good input file
    {
        { std::ofstream(assmtScoreLocation) << "180.5 200" ; }

        test_it(assmtScoreLocation) ;
    }
}

contents of file:
-1.#IND

first number: -1
input of second number failed

------------------

contents of file:
180.5 200

first number: 180.5
second number: 200

http://coliru.stacked-crooked.com/a/e00eef7ba0976899
OK, so I now noticed that I have an error in my file path. Instead of "Lab 4" the folder should have been "Lab 2" Running that, I get my "could not open" error & abort. Strange thing - I'm using a USB disk drive at E, there is no "Lab 4" folder, so the program should have complained then, not now.

Edit: It appears the problem was with my path. You see, the string was hardcoded like this:
E:\CIS 022\Lab 2\Files\studentGrade.txt

when I should have written this:
E:\\CIS 022\\Lab 2\\Files\\studentGrade.text


Edit: It works up to a point. I mean, I run it one time, it outputs 9.5 and 1.#INF. Second time, like 1.#INF and -1.#IND0

Edit #2: The 9.5 is because I changed the 180.5 to 9.5.
Last edited on
> when I should have written this:
> E:\\CIS 022\\Lab 2\\Files\\studentGrade.text


Favour using '/' as the directory separator. Everywhere. eg. E:/CIS 022/Lab 2/Files/studentGrade.text

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23026 for x86

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    const std::string path = "C:/Windows/Logs/DISM/dism.log" ;
    
    std::ifstream file(path) ;
    
    std::string line ;
    for( int i = 0 ; i < 25 && std::getline( file, line ) ; ++i ) std::cout << line << '\n' ;
}

http://rextester.com/HQX91490
Topic archived. No new replies allowed.