Solved

Scrolling a text file using C or C++ functions

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
SNMP error No Such Object available on this agent at this OID 3 454
White board coding practice 3 99
Header of docx file 17 145
Can Live bindings change TGrid Cell Colour ? 1 64
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 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.

710 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