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

  • leoson
  • Born
  • Born
  • leoson
  • Posts: 1

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • GT500Shlby
  • Guru
  • Guru
  • User avatar
  • Posts: 1205
  • Loc: Philadelphia, PA

Post 3+ Months Ago

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


;-)
  • dr_geek
  • Beginner
  • Beginner
  • User avatar
  • Posts: 62
  • Loc: london town

Post 3+ Months Ago

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
  • Posts: 1205
  • Loc: Philadelphia, PA

Post 3+ Months Ago

My mindset was where !=, ==, =, <, >, etc are overloaded for the string class. I guess that would be more C++esque.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 66 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.