Thursday, July 9, 2009

Help with outfile in Bloodsheed C++?

Here is the code but instead of printing 1234 it prints 1234229367200, anybody know how to correct this?


in.txt = 1234 out.txt = 1234229367200????


#include %26lt;fstream%26gt;


#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


using namespace std;





int main()


{





ifstream inFile;


ofstream outFile;





int num1;


int num2;


int num3;


int num4;





inFile.open("C\\in.txt");


outFile.open("C:\\out.txt");





inFile %26gt;%26gt; num1 %26gt;%26gt; num2 %26gt;%26gt; num3 %26gt;%26gt; num4;


outFile %26lt;%26lt; num1 %26lt;%26lt; num2 %26lt;%26lt; num3 %26lt;%26lt; num4;


inFile.close();


outFile.close();








system("PAUSE");


return EXIT_SUCCESS;


}

Help with outfile in Bloodsheed C++?
Here's the problem:





If the file in.txt contains "1234" when you read from the file, you're going to store the integer 1234 in num1.


What's going into num2, num3, and num4 is not determined.


You need to put spaces or newlines between the digits in in.txt for the code to work properly.














Beware, though, that for the best results, the good old fprintf statements from good old reliable stdio.h are the way to go.
Reply:i do c and C++ this is how i would write it.


#include %26lt;stdio.h%26gt;


#include %26lt;windows.h%26gt;


int main()


{


int n=1234;


FILE *file;


file = fopen("C:\\file.txt","w");


fprintf(file,"%d\n",n);


system("pause");


}


you can email me at srp333@comcast.net for any help.


No comments:

Post a Comment