Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
File line replacements using C
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
prague14
n00b
n00b


Joined: 09 Nov 2007
Posts: 40
Location: USA

PostPosted: Mon Aug 25, 2008 8:29 pm    Post subject: File line replacements using C Reply with quote

Okay, in Perl, this would be a no-brainer. But, I'm trying to do this basic line change using C (call me crazy). Say, for example, I want to swap a line with the text "-enabled\n" to "-disabled\n"; here's what I have to *find* the line, file opens okay and everything:

Code:

while((fgets(line, 1024, fp) != NULL) && (line[0] != '\0')) {
    if(strncmp("-enabled", line, 8) == 0) {
        printf("Line: %s", line);
    }
}


This loop works just fine. The problem is actually doing the replacement. I'm completely stuck. Based on what I have read, it looks like I need to mark the beginning of the line with ftell() and then replace it with fseek()... or something(please, stop me if I'm headed in the wrong direction). However, I am not finding a very well documented case on how this is done exactly. That's where you bad-ass C guys come in.

Thanks for any help you guys can give me. :D
_________________
For a wounded man shall say to his assailant,
"If I live, I will kill you. If I die, you are forgiven."
Such is the rule of honor.
Back to top
View user's profile Send private message
rokstar83
Guru
Guru


Joined: 09 Apr 2005
Posts: 423
Location: MD

PostPosted: Mon Aug 25, 2008 9:13 pm    Post subject: Reply with quote

Honestly your best bet is to read the file in to a temp file, replace the string as its reading/writing then delete the old file and move the new file.

Prolly want something like this (my c is a little rusty but this should be close):

Code:

FILE * infile;
FILE * outfile;
char * tmpFileName;
char line[128];

infile = fopen("somefile.txt","r");
tmpFileName = tmpnam(NULL);
outfile = fopen(tmpFileName,"w");

while(fgets(line, sizeof(line), infile)) {
   if(strcmp(line,"sometext") == 0)
      strcpy(line, "someOtherText");
   fputs(line,outfile);
}

fclose(infile);
fflush(outfile);
fclose(outfile);
remove("somefile.txt");
rename(tmpFileName,"somefile.txt");
Back to top
View user's profile Send private message
barophobia
Apprentice
Apprentice


Joined: 27 Apr 2004
Posts: 229
Location: somewhere

PostPosted: Tue Aug 26, 2008 4:51 am    Post subject: Reply with quote

No easy way around it with out some library I don't know about.

1) read file in memory
2) make modifications
3) write file back

There is one exception to the rule that is if you do not change the number of characters in the line you are replacing. Then you can just mmap or something and change those bytes.
_________________
An apple is an apple unless you say it is not an apple!
Back to top
View user's profile Send private message
prague14
n00b
n00b


Joined: 09 Nov 2007
Posts: 40
Location: USA

PostPosted: Tue Aug 26, 2008 12:10 pm    Post subject: Reply with quote

rokstar83: I was looking at that option as well, it certainly seems a bit easier.

baraphobia: If this implies something other than what rokstar83 suggested (using a temp file), how is this done exactly? Do you need to store the file lines in an array and go back to the beginning using rewind()? Can you post some example code if that's the case?

Let me know if you guys have any other thoughts/suggestions. Thanks! :D
_________________
For a wounded man shall say to his assailant,
"If I live, I will kill you. If I die, you are forgiven."
Such is the rule of honor.
Back to top
View user's profile Send private message
wellwhoopdedooo
n00b
n00b


Joined: 04 Mar 2005
Posts: 69

PostPosted: Tue Aug 26, 2008 12:59 pm    Post subject: Reply with quote

There is no OS-level support for inserting into or deleting from a file. You'll need to use a temp file.

Keep it on the same filesystem, and you can just move the file over the old one; don't do the unlink or any checking of your ability to do it, that's a race condition. Just do the move and check the return value.
Back to top
View user's profile Send private message
prague14
n00b
n00b


Joined: 09 Nov 2007
Posts: 40
Location: USA

PostPosted: Tue Aug 26, 2008 3:31 pm    Post subject: Reply with quote

Yeah, the temp file was easy enough to implement, so I did that. If anyone wants to post another approach though, please do so. Thanks to all who replied.
_________________
For a wounded man shall say to his assailant,
"If I live, I will kill you. If I die, you are forgiven."
Such is the rule of honor.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum