?
Solved

Scrolling a text file using C or C++ functions

Posted on 2000-05-11
3
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 600 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

718 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