Solved

Scrolling a text file using C or C++ functions

Posted on 2000-05-11
3
205 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 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

Technology Partners: 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!

Question has a verified solution.

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

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…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

632 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