Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

File line replacements using C

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
6 posts • Page 1 of 1
Author
Message
prague14
n00b
n00b
Posts: 40
Joined: Fri Nov 09, 2007 6:01 pm
Location: USA

File line replacements using C

  • Quote

Post by prague14 » Mon Aug 25, 2008 8:29 pm

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: Select all

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.
Top
rokstar83
Guru
Guru
User avatar
Posts: 423
Joined: Sat Apr 09, 2005 6:18 am
Location: MD

  • Quote

Post by rokstar83 » Mon Aug 25, 2008 9:13 pm

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: Select all

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");
Top
barophobia
Apprentice
Apprentice
User avatar
Posts: 229
Joined: Tue Apr 27, 2004 4:37 am
Location: somewhere

  • Quote

Post by barophobia » Tue Aug 26, 2008 4:51 am

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!
Top
prague14
n00b
n00b
Posts: 40
Joined: Fri Nov 09, 2007 6:01 pm
Location: USA

  • Quote

Post by prague14 » Tue Aug 26, 2008 12:10 pm

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.
Top
wellwhoopdedooo
n00b
n00b
Posts: 69
Joined: Fri Mar 04, 2005 6:06 pm

  • Quote

Post by wellwhoopdedooo » Tue Aug 26, 2008 12:59 pm

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.
Top
prague14
n00b
n00b
Posts: 40
Joined: Fri Nov 09, 2007 6:01 pm
Location: USA

  • Quote

Post by prague14 » Tue Aug 26, 2008 3:31 pm

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.
Top
Post Reply

6 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic