Sponsored Content
Top Forums Programming how to move file pointer to a particular line in c Post 302178585 by user_prady on Wednesday 26th of March 2008 12:52:35 AM
Old 03-26-2008
Quote:
Originally Posted by ramkrix
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-26-2008 at 02:02 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl how to move pointer to previous line in a txt file?

I have a text file that has blocks of text. Each block starts with ### and ends with End_###. I wrote a perl script to search a string from line 2 (ignore any line starts with ###) of each block if matched, need to print that whole block. According to the input file in below, it will print... (5 Replies)
Discussion started by: tqlam
5 Replies

2. Shell Programming and Scripting

how to move the line after a certain pattern in the file

Hi, I have a file called /bb/bin/rstrt. I need to move the line/entry "ccpm_load_shared_memory" after the entry "initcorp". The problem is that there are several entries for "initcorp" in this file and I need the entry to be moved only after the first instance of "initcorp" Is there a way... (5 Replies)
Discussion started by: aoussenko
5 Replies

3. Shell Programming and Scripting

Script to move the first line of a file to the end

I'm rather new to scripting, and despite my attempts at finding/writing a script to do what I need, I have not yet been successful. I have a file named "list.txt" of arbitrary length with contents in the following format: /home/user/Music/file1.mp3 /home/user/Music/file2.mp3... (21 Replies)
Discussion started by: Altay_H
21 Replies

4. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

5. Shell Programming and Scripting

Move a line to end of file

Can somebody help me with a script .... Read a file /etc/inittab find the string starting with rcml and move it entirely towards the end of file. rcml:2:once:/usr/sni/aix52/rc.ml > /dev/console 2>&1 I basically want to change the startup sequence. (2 Replies)
Discussion started by: imanuk2007
2 Replies

6. Shell Programming and Scripting

awk - if field is empty, move line to new file

I have a script with this statement: /usr/xpg4/bin/awk -F"" 'NR==FNR{s=$2;next}{printf "%s\"%s\"\n", $0, s}' LOOKUP.TXT finallistnew.txt >test.txt I want to include logic or an additional step that says if there is no data in field 3, move the whole line out of test.txt into an additional... (9 Replies)
Discussion started by: scriptr2be
9 Replies

7. Shell Programming and Scripting

move contents from one file to another based on line number or content

I want a script that will move everything beyond a certain line number or beyond a certain content word into another file. For example, if file A has this: first line second line third line forth line fifth line sixth line I want to run a script that will move everything beyond the third... (4 Replies)
Discussion started by: robp2175
4 Replies

8. Shell Programming and Scripting

Move a text to next line in a file

Hi , I need your help for the below issue. I have a file which has data as below An error came (/u01/app/12.csv) pkg1.func1: detail s 1111-->pkg1.func1: detail s 2222--> Now pkg1.func1: .... --> can come multiple times in the second line. I need to arrange the data in the below... (9 Replies)
Discussion started by: bhaski2012
9 Replies

9. Shell Programming and Scripting

Move a line to top of the file

Hi, I have a following file and it has only one occurrence of line that says "Output view:". It could be in middle somewhere ( i don't know the exact location ). I want to move it as the first line of the file. Input AAA BBBB CCCC Output view: XXXX YYYY ZZZZ Output should be: Output... (13 Replies)
Discussion started by: jakSun8
13 Replies

10. Solaris

Solaris 5.10 "Move Pointer" stuck

Hey there, I joined this forum just now cause I need help with an old SUN machine at work. I work on a helpdesk and we use a SOLARIS 5.10 OS. Every once in a while we will try to move one of our windows out of the way to make room on the desktop, and sometimes the mouse doesn't release what we... (7 Replies)
Discussion started by: TRex_2005
7 Replies
FSEEK(3)						   BSD Library Functions Manual 						  FSEEK(3)

NAME
fgetpos, fseek, fseeko, fsetpos, ftell, ftello, rewind -- reposition a stream LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <stdio.h> int fseek(FILE *stream, long int offset, int whence); int fseeko(FILE *stream, off_t offset, int whence); long int ftell(FILE *stream); off_t ftello(FILE *stream); void rewind(FILE *stream); int fgetpos(FILE * restrict stream, fpos_t * restrict pos); int fsetpos(FILE * restrict stream, const fpos_t * restrict pos); DESCRIPTION
The fseek() function sets the file position indicator for the stream pointed to by stream. The new position, measured in bytes, is obtained by adding offset bytes to the position specified by whence. If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the start of the file, the current position indicator, or end-of-file, respectively. A successful call to the fseek() function clears the end-of-file indicator for the stream and undoes any effects of the ungetc(3) function on the same stream. The fseeko() function is identical to the fseek() function except that the offset argument is of type off_t. The ftell() function obtains the current value of the file position indicator for the stream pointed to by stream. The ftello() function is identical to the ftell() function except that the return value is of type off_t. The rewind() function sets the file position indicator for the stream pointed to by stream to the beginning of the file. It is equivalent to: (void)fseek(stream, 0L, SEEK_SET) except that the error indicator for the stream is also cleared (see clearerr(3)). In this implementations, an ``fpos_t'' object is a complex object that represents both the position and the parse state of the stream making these routines are the only way to portably reposition a text stream. The pos argument of fsetpos() must always be initialized by a call to fgetpos(). RETURN VALUES
The rewind() function returns no value. Upon successful completion, fgetpos(), fseek(), fseeko(), and fsetpos() return 0. The functions ftell() and ftello() return the current offset. Otherwise, fseek(), fseeko(), ftell(), and ftello() return -1 while fgetpos() and fsetpos() return a nonzero value. On error all functions the global variable errno is set to indicate the error. Since the rewind() function does not return an error code, applications need to clear errno before calling it in order to detect errors. ERRORS
[EBADF] The stream specified is not a seekable stream. [EINVAL] The whence argument to fseek() was not SEEK_SET, SEEK_END, or SEEK_CUR. [EOVERFLOW] For ftell(), the current file offset cannot be represented correctly in an object of type long. The function fgetpos(), fseek(), fseeko(), fsetpos(), ftell(), ftello(), and rewind() may also fail and set errno for any of the errors spec- ified for the routines fflush(3), fstat(2), lseek(2), and malloc(3). SEE ALSO
lseek(2) STANDARDS
The fgetpos(), fsetpos(), fseek(), ftell(), and rewind() functions conform to ANSI X3.159-1989 (``ANSI C89''). The fseeko() and ftello() functions conform to X/Open System Interfaces and Headers Issue 5 (``XSH5''). BUGS
The fgetpos() and fsetpos() functions don't store/set shift states of the stream in this implementation. BSD
January 21, 2012 BSD
All times are GMT -4. The time now is 10:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy