LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-03-2005, 08:29 AM   #1
fanatic_ravi
LQ Newbie
 
Registered: Jan 2005
Posts: 22

Rep: Reputation: 0
rading a file


How should i replace a line in a file thru a C program
 
Old 02-03-2005, 09:28 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can't, unless the new line is the exact same length as the old line. You'll have to read in the old file and write it back out to a new file with the line modified.
 
Old 02-03-2005, 09:53 AM   #3
fanatic_ravi
LQ Newbie
 
Registered: Jan 2005
Posts: 22

Original Poster
Rep: Reputation: 0
how do i search on a particular line ...shall i have to strtok on the lines or what ??
 
Old 02-03-2005, 10:16 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can use strchr() to look for a specific character in a string, or you can use strstr() to look for a substring in a string. It depends on what you want to search for.
 
Old 02-04-2005, 01:53 AM   #5
harken
Member
 
Registered: Jan 2005
Location: Between the chair and the desk
Distribution: Debian Sarge, kernel 2.6.13
Posts: 666

Rep: Reputation: 30
My 20c in here:
If the file is structured in lines maybe you could do thispen the file r/w, read it line by line, compare the line with whatever string you want; when found, fseek to the line's position-1, write the desired line, fseek to the new location then write the rest of the lines skipping the one found.

edit: maybe it's not clear what I said above but of course you'll have to use a temporary file which then can be renamed to the original file, previously deleted.

Last edited by harken; 02-04-2005 at 03:17 AM.
 
Old 02-04-2005, 09:35 PM   #6
JrLz
Member
 
Registered: Mar 2004
Location: Jakarta
Posts: 164

Rep: Reputation: 30
Agree with harken....
You can do anything with that.......
wueheheh
 
  


Reply



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
File recovery after partition table damage - XFS file system gracecourt Linux - General 4 09-28-2017 05:29 PM
forrtl: severe (47): write to READONLY file, unit 5, file /dev/pts/1 terrence Programming 1 10-01-2005 10:22 PM
gave wrong syntax for tar as tar -cvzf file file.tgz how to recover the file gautham Linux - General 4 04-13-2005 03:15 AM
Yum update complains missing file (broken dep), but file can be located. davidas Linux - Software 0 03-27-2004 09:11 PM
How to play a media file/ video file/mp3 file recorded in harddisk/cd-rom arindam Linux - Newbie 2 09-05-2003 10:31 AM

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

All times are GMT -5. The time now is 09:29 AM.

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