By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,795 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,795 IT Pros & Developers. It's quick & easy.

String Problems

P: 2
Reading numbers from file into two strings, converting to int using atoi so I can use addition. Now I need to convert back to strings. I can't find a way to converting the ints that I just created using atoi in to a string format! Any help would be great.

Sorry, additional info: the string I need has to be written out to a new file. using the g++ compiler


Mat
Jun 30 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. int x = 45;
  2. char myString[50];
  3.  
  4. sprintf( myString, "%d", x );
  5.  
  6. /* or if you are using a file handle */
  7.  
  8. FILE *fout;
  9.  
  10. fout = fopen( "MyFile.txt", "w" );
  11.  
  12. if ( NULL != fout )
  13. {
  14.     fprintf( fout, "%d\n", x );
  15.  
  16.     fclose(fout);
  17. }
  18.  
Obviously sprintf and fprintf can take all the format strings that you could pass printf.
Jun 30 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.