The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
see a specific line in file zivsegal UNIX for Dummies Questions & Answers 7 04-23-2009 07:08 PM
read specific text from a log file ragha81 Shell Programming and Scripting 4 10-17-2006 01:17 PM
extract some specific text file urgent pls reyazan UNIX for Dummies Questions & Answers 2 10-20-2005 09:36 AM
Delete specific lines in a text file dniz High Level Programming 9 08-08-2005 08:30 AM
Jumping to next line with awk gozer13 Shell Programming and Scripting 4 02-02-2005 06:36 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-16-2005
mridula mridula is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 20
jumping to a specific line in a text file

hi everybody!

i need to read a specific line from a text file using C. can any one suggest how to do it.

i m aware abt fread(), fwrite(), fseek()... but using these allows the pointer to be moved 1 character at a time. Is there a way i could jump directly to a line if i know the line number?

i would also like to know how to read rows of a text file and store it in a variable if the type of data in each row is different.

my sample text file looks like this:

username:xyz
login time: 00:00:00
status:0

Thanks!
  #2 (permalink)  
Old 09-16-2005
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
Quote:
Originally Posted by mridula
i m aware abt fread(), fwrite(), fseek()... but using these allows the pointer to be moved 1 character at a time. Is there a way i could jump directly to a line if i know the line number?
not so !
with fseek - file pointer can be repositioned anywhere within the file
if the file records are of standard length ( for ex: each and every line of the file contains x chars)

then to switch to nth line
fseek(filePtr, (n * x) + 1, SEEK_CUR);
(assuming \n as the default line separator in the file)
SEEK_CUR is the whence position.

use man fseek you will get the required info
  #3 (permalink)  
Old 09-16-2005
mridula mridula is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 20
thanx for telling abt fseek().

I agree with you.
but in my case, i know the number of lines in the text file but am not aware of what the length of each line is going to be.

eg:format of the text file

Username : xyz
Machine : 10.101.11.41
Priority : 1
Status : 01
Type :
Time_of_Submittion : 00:00:00
Time_of_Execution : 00:00:00
Time_of_Completion : 00:00:00
Kill : 0
Iteration : 2000/10000


my text file will have a fixed number of rows (eg: 10 in this case). but their values might change with time(eg:username can change to abcd, machine:101.101.101.101...and so on..)
So if i want to read the value of status how do i go about it. to use fseek() i need to know the offset value.
  #4 (permalink)  
Old 09-16-2005
RishiPahuja's Avatar
RishiPahuja RishiPahuja is offline
Registered User
  
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Thumbs up

Techincally speaking you cannot escape going for a char by char parsing.
Even if it happens to be a in built function it has to do a search for "\n" character n times to go to n'th line.

For simplicity if you want to totally exclude some portions you can use some unix tools like sed, grep to filter the file contents and then do your stuff

rishi
  #5 (permalink)  
Old 09-20-2005
devesh devesh is offline
Registered User
  
 

Join Date: Mar 2005
Location: Mumbai, India
Posts: 12
You can use fgets() in a loop:

char *fgets(char *s, int n, FILE *stream);

The fgets() function reads characters from the stream into the array pointed to by s, until n-1 characters are read, or a newline character is read and transferred to s, or an end-of-file condition is encountered.

If you want to go to Nth line use fgets() N times. and at the end you can go back to the start of the file using fseek() to start a new read.
--------------------------------------------------
Alternatively you can use combination of head and tail unix commands in shell script to solve your problem.
Closed Thread

Bookmarks

Tags
unix commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0