Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Scrolling a text file using C or C++ functions

I am working in UNIX-Solaris platform with c++. I have a text file, contains say 50 lines. I want to move the file pointer line by line forward (it is done) now I want to move the file pointer line by line backward, each line is of varied size. Is there any header files in C or C++, supports this type of scrolling up or down?

Thanks in advance.
ARKEE

0
arkee
Asked:
arkee
1 Solution
 
wraith0Commented:
Have you thought about keeping the previous line's length in a variable and then making a call to fseek, something like this:

fseek(fp,prev_length,SEEK_CUR);

The header is stdio.h
0
 
arkeeAuthor Commented:
Yeah, thats a good idea, that will work for smaller fiels but if it is a huge file with thousands of lines it is not possible.( may be array of that many number of elements is not advisible).

anyway thanks for your comment, think of the other way...
0
 
AlexVirochovskyCommented:
It is very simple(but a bit slow):
In end of every line must be "\r\n".
So, you algoritm: you move back from current position to 2 bites and
read 1 bait. If this byte = '\n', that is end of previous line.Now you make same thing again(go back some times) and find begin of this line.
All function for this : standart fseek/fread

I hope , it helps. Alex

BTW: there is other way, but demand 2-nd file(binary), say place.dat  
It is file with fixed size of value(long). In this file you save place of every readed line and if you want go to some line, you  simple read offset of it from place.dat  and use fseek.
Again: it is faster, but demands 2-nd file.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now