How to use C to modify the content (in the middle) of a f

  • leoson
  • Born
  • Born
  • No Avatar
  • Joined: Jul 23, 2004
  • Posts: 1
  • Status: Offline

Post July 23rd, 2004, 11:14 pm

Suppose I have a file with these content:

... other content...
Counter:1
......other tcontent...

How to read the text file and get the number (1) in the Counter and then change it, and save it to the original file?

The 'Counter' line number is fixed.
then the number is key in from the user

please help!!!

Thanks you in advance.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 23rd, 2004, 11:14 pm

  • GT500Shlby
  • Guru
  • Guru
  • User avatar
  • Joined: Jul 28, 2004
  • Posts: 1205
  • Loc: Philadelphia, PA
  • Status: Offline

Post July 29th, 2004, 7:12 am

File Stream, while loop
while (blah != "Counter:")
edit, keep going.


;-)
"Now, I don't want to get off on a rant here, but..." - Dennis Miller
Image
  • dr_geek
  • Beginner
  • Beginner
  • User avatar
  • Joined: Jul 07, 2004
  • Posts: 62
  • Loc: london town
  • Status: Offline

Post July 30th, 2004, 5:49 am

in C:
Code: [ Select ]
blah != "Counter"
compares pointers, not strings!

You need
Code: [ Select ]
#include <string.h>

...

//loop over all lines in file
 //read blah from file

 if (strcnmp(blah,"Counter:",8) == 0) {
   //modify blah
 }

 //write blah to file
  1. #include <string.h>
  2. ...
  3. //loop over all lines in file
  4.  //read blah from file
  5.  if (strcnmp(blah,"Counter:",8) == 0) {
  6.    //modify blah
  7.  }
  8.  //write blah to file
  • GT500Shlby
  • Guru
  • Guru
  • User avatar
  • Joined: Jul 28, 2004
  • Posts: 1205
  • Loc: Philadelphia, PA
  • Status: Offline

Post July 30th, 2004, 6:05 am

My mindset was where !=, ==, =, <, >, etc are overloaded for the string class. I guess that would be more C++esque.
"Now, I don't want to get off on a rant here, but..." - Dennis Miller
Image

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 105 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.