LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2007, 10:52 AM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Rep: Reputation: 30
Howto edit a file in C


Hi,

I want to edit a file in C, but I don't understand disk I/O fully yet. I hope someone can help me with it.

I want to add some characters at the end of the second line of a file I have. Can someone show me how I could do that?

Thanks for your help.

Regards,

Ben
 
Old 02-23-2007, 12:01 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You will not be able to 'edit' directly, in the sense of a user's impression of using an editor. In general, you need to open() the file, read() the entire file into memory (or a temporary file), close() the file, and then open() the file anew. You will need to write() from the stored copy to the new version of the file, part-by-part, until you encounter the part where you will insert the desired characters. Write() the desired characters, and then resume writing the remainder of the stored original file. Then close() the file.

The italicized words above are the standard C functions you will need to use (hint: man is your friend). There is also a parallel set of functions that operates on streams: fopen(), fread(), fwrite(), fclose(), and the helpful, higher level, fscanf(), and fprintf(). See also: eof()/feof().

While learning this stuff, you will do yourself a favor by writing some simple test programs that open, read, write and close files in very simple ways. Your greatest challenge may be parsing the input file into lines, which might be easier to do with fscanf().
--- rod.
 
Old 02-23-2007, 12:30 PM   #3
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Thank you very much for your reply Rod. I am going to print some of those man pages, and I will try what you've suggested.

Regards,

Ben
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing file permission, How to edit a file? rainchild Linux - Newbie 8 01-14-2007 10:18 PM
10.1 howto edit the right mouse button menu ? pc-overkill SUSE / openSUSE 2 06-10-2006 02:29 AM
howto edit etc/hotplug/blacklist??? lek Slackware 1 09-09-2005 03:47 PM
KDE Howto edit / add to menus bpasdar Linux - Software 6 05-03-2004 05:18 PM
Howto edit a Perl file mickeyboy Red Hat 5 09-30-2003 03:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration