471,336 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

Copying a Text File in Reverse

2
Hello all-

I'm trying to write a progam that reads in a single text file, then copies the contents in reverse order to a new file. I'm guessing the most efficient way to do this is to read the file into a buffer (which I have done using fgets), then find where the file ends in this buffer, and read this backwards into the new file. Is that reasonable? What I don't understand is how to traverse this buffer and find the end of the file. If I knew how to do that I could probably figure out how to read the buffer backwards into the new file. Can someone give me a hint on this? I'd be most grateful.

Thanks-
bbgun
Jun 10 '07 #1
5 4096
Savage
1,764 Expert 1GB
Hello all-

I'm trying to write a progam that reads in a single text file, then copies the contents in reverse order to a new file. I'm guessing the most efficient way to do this is to read the file into a buffer (which I have done using fgets), then find where the file ends in this buffer, and read this backwards into the new file. Is that reasonable? What I don't understand is how to traverse this buffer and find the end of the file. If I knew how to do that I could probably figure out how to read the buffer backwards into the new file. Can someone give me a hint on this? I'd be most grateful.

Thanks-
bbgun
It's reasonable.Now u are reading in file using while loop,right?So basicly u have array of strings,which means that the last index is index of the last line.When u know this it's easy to do what u want?

Make us know if u need more hints?

BTW:Welcome and thank u for joining thescripts

Savage
Jun 10 '07 #2
bbgun
2
Actually, I did not use a while loop with fgets. I wrote the following code thinking I had to first load the buffer:

Expand|Select|Wrap|Line Numbers
  1. if(fgets(buf, BUFFERSIZE, in) == NULL)
  2.         fprintf(stderr, "%s: can't transfer\n",
  3.                 argv[0]);
...Doesn't this put the array of strings into buf? I was going to use fputs to write the reversed strings into the new file.
Jun 10 '07 #3
Savage
1,764 Expert 1GB
Actually, I did not use a while loop with fgets. I wrote the following code thinking I had to first load the buffer:

Expand|Select|Wrap|Line Numbers
  1. if(fgets(buf, BUFFERSIZE, in) == NULL)
  2.         fprintf(stderr, "%s: can't transfer\n",
  3.                 argv[0]);
...Doesn't this put the array of strings into buf? I was going to use fputs to write the reversed strings into the new file.
U need to use loops,fgets reads in a single line and stores it into buf and later read from the buf into the file in reverse oreder by using fgets.

BTW:what is declaration of the buf is it:

char buf[BUFFERSIZE]

??

if it is like this,this is array of chars or one string u need arrays of strings.

Savage
Jun 11 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Don't overlook the approch of doing SEEK_END and fetching a character which then put into the output file. Then back up in the inout file and repeat.

This would be a character-by-character transfer.

Youe wouldn't need a giant buffer and you wouldn;t have to worry abot the contents of the file.

If this is C++ you use the copy algorithm.
Jun 11 '07 #5
Savage
1,764 Expert 1GB
Don't overlook the approch of doing SEEK_END and fetching a character which then put into the output file. Then back up in the inout file and repeat.

This would be a character-by-character transfer.

Youe wouldn't need a giant buffer and you wouldn;t have to worry abot the contents of the file.

If this is C++ you use the copy algorithm.
I think that OP uses C.

Savage
Jun 11 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Robert Tarantino | last post: by
22 posts views Thread by Matt | last post: by
6 posts views Thread by Rajorshi Biswas | last post: by
8 posts views Thread by John Smith | last post: by
6 posts views Thread by kimiraikkonen | last post: by
4 posts views Thread by Jim Barlow | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.