Read line after line but from the eof.

G

Grizlyk

Ground21 ÐÉÓÁÌ(Á):
Hello.
How could I read the whole text file line after line from the end of
file? (I want to ~copy~ file)

Take it, i am slightly adapted it for C++ from C:
Declare in your program:

#include <stdio.h>

namespace NGround21
{
typedef unsigned int uint;

// store 'max_visible_chars' chars maximum to 'buf' and close by \0
//( ! ADD '\0' char to buf, total size can be 'max_visible_chars' +1
maximum )
// all 256 chars of DOS/Win/UNIX <EOL> allowed
//special: to skip all chars till <EOL> write:
getln(any_number,0,correct_pointer);

// "fi" must be open as "rb"
// return 0 if errors in parameters, else return !0
// "fi" file pointer will be after <EOL> if return !0
char getln(char *const buf, const uint max_visible_chars, FILE
*const fi)throw();

//namespace NGround21
}
using namespace NGround21;

Write as separated cpp file, compile it and then link object file to
your project

#include <stdio.h>

char NGround21::getln(
char *const buf,
const uint max_visible_chars,
FILE *const fp
)throw()
{
if(
( (max_visible_chars) && (!buf) )
||( !fp )
)return 0;

char *p=buf;
for( uint len=0; ; )
{
int ch=fgetc(fp);
if( feof(fp) )break;

if( ch==13 )continue;
if( ch==10 )break;

if( len >= max_visible_chars )continue;

++len;
*p++=static_cast<char>(ch);
}

if( p ) *p=0;
return 1;
}
 
G

Grizlyk

Oy, google makes error, look to the sorted list:

Building Compatible Libraries 11 ÎÏ×ÙÈ ÉÚ 11 Peter Olcott
(Á×ÔÏÒÙ: 4) 11 ñÎ×.
Simple program 20 Ground21 (Á×ÔÏÒÙ: 4) 18 äÅË. 2006
Read line after line but from the eof. 27 Ground21 (Á×ÔÏÒÙ:
8) 24 äÅË. 2006
Something Strange From the Days Way Before C++ 9 ÎÏ×ÙÈ ÉÚ 9
Tim Slattery (Á×ÔÏÒÙ: 7) 11 ñÎ×.
Possible causes for the 'delete ptr;' to core?? 7 ÎÏ×ÙÈ ÉÚ
7 Noah Roberts (Á×ÔÏÒÙ: 6) 11 ñÎ×.
 
S

SKumar

I have the same need, but it needs to be done for a large file (~
5GB).
So I don't want to use stack/vector/queus etc.,

any efficient C++ / STL solution would be great

Thanks,
SK
 
K

kwikius

I have the same need, but it needs to be done for a large file (~
5GB).
So I don't want to use stack/vector/queus etc.,

any efficient C++ / STL solution would be great

If its too big for memory. Read as many as you can to the stack.
when attempting to push stack throws bad_alloc pop all the stack to a
unique name temp file. (You need to take care to not lose the last bit
of input data of course, but the stack contents that succeeded should
not be violated)
save the unique temp filenames on another stack.
then start again reading data, starting with the last bit of data that
failed.
when the input is done pop the filename stack one by one and read the
resulting files to your output file.

To speed things up you could preallocate a big chunk in your stack
container. You would probably need to experiment to find how much
memory you can grab, just keep catching exceptions and reducing the
size.

Untested .... ;-)

regards
Andy Little
 
K

kwikius

If its too big for memory. Read as many as you can to the stack.
when attempting to push stack throws bad_alloc pop all the stack to a
unique name temp file. (You need to take care to not lose the last bit
of input data of course, but the stack contents that succeeded should
not be violated)
save the unique temp filenames on another stack.
then start again reading data, starting with the last bit of data that
failed.
when the input is done pop the filename stack one by one and read the
resulting files to your output file.

To speed things up you could preallocate a big chunk in your stack
container. You would probably need to experiment to find how much
memory you can grab, just keep catching exceptions and reducing the
size.

Untested .... ;-)

regards
Andy Little


There is a fatal flaw in this scheme, which is that when you try to
push your filename stack you will probably get bad_alloc too. Youre
all out of memory :)
You will need to find a way to give the system some memory back when
required, or try a less agressive approach to the whole thing ;-)

regards
Andy Little
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top