The UNIX and Linux Forums  

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
move the last word to begining of next line - SED baskar Shell Programming and Scripting 4 02-15-2008 09:28 AM
Perl how to move pointer to previous line in a txt file? tqlam Shell Programming and Scripting 5 01-27-2008 02:24 PM
confusion (file pointer and file descripter) johnray31 UNIX for Dummies Questions & Answers 1 08-16-2006 12:59 AM
file pointer bankpro High Level Programming 1 02-20-2006 07:50 AM
file pointer bankpro High Level Programming 3 02-07-2006 01:10 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-25-2008
Registered User
 

Join Date: Sep 2007
Posts: 163
how to move file pointer to a particular line in c

Hello experts,

I ve a text file I want to go to particular line . what is the best way to do this in c ?

I am tried as follows
Code:
 fseek ( fh, pos, SEEK_SET);
but this functions moves the file pointer according to a number of bytes. Unfortunately I don't know the exact byte of the desired pointer position. All I have is the Line no I want to go , and that is my variable pos here.

suppose pos = 10 , then I want to jump to the 10th record in my text file. and one more thing my text file is about 2 million lines.

regards,
user_prady
--

Last edited by user_prady; 03-25-2008 at 09:03 PM.
Reply With Quote
Forum Sponsor
  #2  
Old 03-25-2008
Registered User
 

Join Date: Dec 2007
Location: TamilNadu,INDIA
Posts: 48
Hi,

I beleive you can do it with fseek itself by mentioning the bytes. See our terminal is 23*79, I mean to say 23 rows and 79 columns.

Say, if we have each coulmn occupies 1 character exactly, it means 1 byte. Therefore, a line can have 79 bytes of data in it. Then to move the nth line, you can use this formula
byte position=(((n-1)*number of bytes per line)+1).
I derived this formula, hope it will work.

Put this byte position in your fseek() function.

Please find the number of characters or bytes occpied by each column in a line of terminal. ere I assumed each column occupies 1 charcter and derived the formula.

please revert me back with the solution resulted.

regards,
Ramkrix
Reply With Quote
  #3  
Old 03-25-2008
Registered User
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by ramkrix View Post
Hi,

I beleive you can do it with fseek itself by mentioning the bytes. See our terminal is 23*79, I mean to say 23 rows and 79 columns.

Say, if we have each coulmn occupies 1 character exactly, it means 1 byte. Therefore, a line can have 79 bytes of data in it. Then to move the nth line, you can use this formula
byte position=(((n-1)*number of bytes per line)+1).
I derived this formula, hope it will work.

Put this byte position in your fseek() function.

Please find the number of characters or bytes occpied by each column in a line of terminal. ere I assumed each column occupies 1 charcter and derived the formula.

please revert me back with the solution resulted.

regards,
Ramkrix
Thanks for the reply..
But how to know number of bytes per line . Its changing per line in my case.

I tried something like follow but still in half way.
Code:
int i ;
char data[80];
long cur_pos[200];
while( feof(fh) == 0){
            cur_pos[i++] = ftell(fh);
 printf( "cur_cursor = %ld i= %d\n", cur_pos[i],i);
            fgets(data, 80, fh);
     }
output
Code:
cur_cursor = 1128098488 i= 1
cur_cursor = -30932 i= 2
cur_cursor = -30960 i= 3
cur_cursor = 1127283141 i= 4
In the above case it displays me something starnage output but when i change the above code to following

Code:
char data[80];
long cur_pos;
while( feof(fh) == 0){
            cur_pos = ftell(fh);
 printf( "cur_cursor = %ld \n", cur_pos);
            fgets(data, 80, fh);
    }
I am getting what I supposed to get.
output:
Code:
cur_cursor = 0
cur_cursor = 20
cur_cursor = 39
cur_cursor = 59
cur_cursor = 81
cur_cursor = 102
cur_cursor = 121
But still I cant find out my sol..
Thanks in advance..

Last edited by user_prady; 03-25-2008 at 10:02 PM.
Reply With Quote
  #4  
Old 03-25-2008
Registered User
 

Join Date: Dec 2007
Location: TamilNadu,INDIA
Posts: 48
cool... I did not know the corect code to find the no of bytes per line and now i got abit visibility with your code.
ftell function obtains the current value of the file position indicator for the stream pointed to by stream.

May I have the content of your text file in which you use ftell? Atleast first ten lines.

Regards,
Ramkrix
Reply With Quote
  #5  
Old 03-25-2008
Registered User
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by ramkrix View Post
cool... I did not know the corect code to find the no of bytes per line and now i got abit visibility with your code.
ftell function obtains the current value of the file position indicator for the stream pointed to by stream.

May I have the content of your text file in which you use ftell? Atleast first ten lines.

Regards,
Ramkrix
Oh sure

0.137667 0.0595724
0.102683 0.109343
0.0508147 0.141131
-0.00941674 0.149706
-0.0681001 0.133655
-0.115576 0.09561
-0.144043 0.0418468
-0.148821 -0.0187982
-0.129114 -0.0763566
-0.0881689 -0.121354
-0.03272 -0.146388
0.0281091 -0.147345
0.0843097 -0.12406
0.126645 -0.0803778
0.148155 -0.0234622


I think the simple for loop will do that for me ..

Code:
for(i=0; i< my_pos; i++){
  fgets(fh);
}
Regards,
user_prady

Last edited by user_prady; 03-25-2008 at 11:01 PM.
Reply With Quote
  #6  
Old 03-26-2008
Registered User
 

Join Date: Dec 2007
Location: TamilNadu,INDIA
Posts: 48
Arrow

Quote:
char data[80];
long cur_pos;
while( feof(fh) == 0){
cur_pos = ftell(fh);
printf( "cur_cursor = %ld \n", cur_pos);
fgets(data, 80, fh);
}
A simple change to this code may work:

char data[80];
int ct_str_length;
long new_byte_position;

long cur_pos;
while( feof(fh) == 0){
cur_pos = ftell(fh);
printf( "cur_cursor = %ld \n", cur_pos);
fgets(data, 80, fh);
ct_str_length=strlen(data);
new_byte_position=ct_str_length+1;
printf("new_byte_position or cursor position is %ld" ,new_byte_position);
fseek(fh,new_byte_position,SEEKSET);

}



we know that fgets() stops once it reaches newline characte/EOF.

Till EO of file is reached(feof(fp)==0), with the curent byte position as 0th byte or first byte of first line, we are reading the first line by fgets() till newline characte is reached. Once \n is reached, fgets() tops its function and the ead content is stored in data array. the string length in this aray is the total number of characters/bytes in first line. So adding 1 byte more than that, now the current byte position becomes the beginning byte of the next line. Check this.

-Ramkrix
Reply With Quote
  #7  
Old 03-26-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
If you had followed the reply from jim mcnamara's post then you wouldn't be asking this question.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0