![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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! |
|
||||
|
Quote:
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 |
|
||||
|
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. |
|
||||
|
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. |
![]() |
| Bookmarks |
| Tags |
| unix commands |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|