Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File open, Change, and save.

Posted on 1999-07-26
7
Medium Priority
?
189 Views
Last Modified: 2010-05-18
I want to update a .ini file via an executable. First I need to be able to read the .ini file, then change 1 line, then save the file to its original location. I can't just do a file replace because the .ini file may differ from machine to machine. I do however know that the line I need to change will always remain on line 9. How can i open the file, do a line replace then save the file?
0
Comment
Question by:pote
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 1201084
Hmm - assuming you're on a Windows' platform, you might want to use 'WitePrivateProfileString()' to change the line instead of doing that manually, e.g.

WritePrivateProfileString ( "entry", "KeyToReplace", "New String", "c:\\windows\\yourfile.ini");
0
 
LVL 7

Accepted Solution

by:
KangaRoo earned 800 total points
ID: 1201085
You cannot really replace lines in a file. You'd have to copy the lines that can remain unmodified and keep track of line number. When at that special line, write the modified version. Then continue with the other lines.

#include <stdio.h>
#define BUFFER_SIZE 4096

void replace(FILE* in, FILE* out, int line_no, char* replace_with)
{
   char buffer[BUFFER_SIZE];
   for(int line = 1; !feof(in); ++line)
   {
      fgets(buffer, BUFFER_SIZE, in);
      if(line == line_no)
         fputs(replace_with, out);
      else
         fputs(buffer, out);
   }

}
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1201086
Hey, missed your comment jkr. Would be a lot easier indeed....
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:jkr
ID: 1201087
Hmm, KangaRoo, your approach would also require to copy the new file to the old location...
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1201088
Yes, but that clutters too much.

char* temp = strdup(tmpnam(0));
char* inifile = "YourIniFile.ini";
rename(inifile, temp);
FILE* in = fopen(temp, "rt");
FILE* out = fopen(inifile, "wt");
replace(in, out, 9, "new text\n");
fclose(out);
fclose(in);
remove(temp);


0
 

Author Comment

by:pote
ID: 1201089
Thanks for the help, I have a good handle now.
Much appreciated.
Pote
0
 

Author Comment

by:pote
ID: 1201090
Thanks for the help, I have a good handle now.
Much appreciated.
Pote
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question