Solved

Scrolling a text file using C or C++ functions

Posted on 2000-05-11
3
166 Views
Last Modified: 2010-04-02
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
Comment
Question by:arkee
3 Comments
 

Expert Comment

by:wraith0
ID: 2806332
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
 

Author Comment

by:arkee
ID: 2806529
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
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 200 total points
ID: 2806721
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now