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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.