Jump to content

[C] change content of a 'textfile

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
skelesp

skelesp

    Newbie

  • Members
  • Pip
  • 1 posts
Hi,

I'm new here on the forum, so I will give a brief introduction. I'm a belgian student who likes programming in his spare time. Now I'm working on a school project which is actually almost finished, but I'd like to add an extra 'function'.

The idea is that I have a textfile with following content:

[startpath]=C:\testmap1\
[endpath]=C:\testmap2\test\

Now I would like to change the paths in my program (so without entering an editor and changing the code). The problem is that I can't search my textfile for the string "[endpath]=" and put my cursor after this string .

Does anybody knows the solution for my problem?

BTW, I have already managed to change the startpath by using FSEEK and FGETS, but when I tried to overwrite the path C:\testmap1\ by C:\map3\, the result was: C:\map3\ap1\. So, the last 4 characters of the previous (and longer) path stayed in the file.

Can anyone give me some tips how to easily change text in a file. I can imagine many people have already done this in configuration file or something like that...

Thanks for the help!!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
If you read in one line at a time, you should be able to look at the initial characters in each line.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
Or you could (if you're using Windows) use the Windows API functions GetPrivateProfileString and SetPrivateProfileString to automatically read from and write to the file if you change it a little bit, like so:


;INI FILE FORMAT

;this is a comment

;[section]

;key=value


[PATHS]

start=c:\windows\blah

end=c:\x\y\z