0

Hello!
I'm new to c++ but I have a problem and desparate for help.
I'm trying to store string and integers into a file, but only strings get stored and integers dont. I tried casting,didnt work. Not to convolute you with my code, I came up with this small program to show. Any help is greatly appreciated

int main (int argc, char * const argv[]) {
	ofstream outfile ("test.txt");
	
	outfile << "hello" << endl;
	outfile << 44 << endl;
	
	outfile.close();
	
    return 0;
}

Edited by atman: mistake

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by StuXYZ
0

The code as posted works find. [assuming that you put a using namespace std;

There are two possible reasons that you are not seeing this code working.

(a) you haven't actually run THIS code. The error with the lack of std::, includes and
using namespace std; indicate that.

(b) You have strange file permissions, and you write the file BUT second time you cannot overwrite the file. To test that change the text, or put a test into the code to see if outfile is good. e.g. if (outfile.good()) std::cout<<"File good"<<std::endl; Note: try adding an extra line to the output , very very often people forget the last std::endl and also try putting the number output before the test string, and always change the txt string at each test.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.