jumping to a specific line in a text file


 
Thread Tools Search this Thread
Top Forums Programming jumping to a specific line in a text file
# 1  
Old 09-16-2005
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  
Old 09-16-2005
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  
Old 09-16-2005
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  
Old 09-16-2005
MySQL

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 Smilie

rishi
# 5  
Old 09-20-2005
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

2. Shell Programming and Scripting

Update specific field in a line of text file

I have a text file like this: subject1:LecturerA:10 subject2:LecturerA:40 if I was given string in column 1 and 2 (which are subject 1 and LecturerA) , i need to update 3rd field of that line containing that given string , which is, number 10 need to be updated to 100 ,for example. The... (6 Replies)
Discussion started by: bmtoan
6 Replies

3. Linux

Get a specific line number from a text file

Hello! All, Could you please tell me how to get a specific line number from a text file? For example below, ABC DEF ---> Get this line number, return to an variable GHI My OS is Linux. Thank you so much for your help in advance! (3 Replies)
Discussion started by: barryxian
3 Replies

4. UNIX for Dummies Questions & Answers

Inputting text to a specific line of a file

Hi all, I have a script which uses a basic line to add text into another file e.g. grep -i test * >> test.txt Is there a way I can get the output of the grep to output to a specific line in the text.txt for example output above the line starting "Bottom line..." (6 Replies)
Discussion started by: JayC89
6 Replies

5. Programming

Jumping to a particular line in a file

Hi, I have an output file which has more than 1,000,000,000 lines. I am accessing this file in another C++ program. Now while accessing the output file using cin, I want to jump, say, to the 5,000,000th line directly and start accessing data from there. Is this possible? Could someone please... (4 Replies)
Discussion started by: mugga
4 Replies

6. Shell Programming and Scripting

Reading data from a specific line in a text file

Hello, I have a problem which is giving me headache for days, can some please help. Please see code and text fiel below. Please see text in red for the problem I am facing # Program gets an input x from user while read line ; do echo... (4 Replies)
Discussion started by: jermaine4ever
4 Replies

7. Shell Programming and Scripting

Reading data from a specific line in a text file

hello, I have got the following problem that I am hoping someone can help with please. 1. I have got the following text file (below) , the columns data are 'Test Day', 'Board', 'Betting Number'. TEXT FILE ============================================ 1 3 02-01-27-28-29-30 0 1... (1 Reply)
Discussion started by: jermaine4ever
1 Replies

8. Shell Programming and Scripting

Problem inserting text into file after specific line

this is utterly embarassing :( after posting here i revisited my files and found that when i used "vi" instead of a gui based editor, i suddenly found that the indentations were in fact wrong :( sorry about this :( (0 Replies)
Discussion started by: mocca
0 Replies

9. Shell Programming and Scripting

Modify Specific Line of a Text File

Given a text file, how do you add a line of text after a specific line number? I believe I would want to use "sed" but I am unsure of the syntax. Thank you. Mike (5 Replies)
Discussion started by: msb65
5 Replies

10. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies
Login or Register to Ask a Question