By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,418 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,418 IT Pros & Developers. It's quick & easy.

Copying a Text File in Reverse

P: 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
Share this Question
Share on Google+
5 Replies


Savage
Expert 100+
P: 1,764
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

P: 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
Expert 100+
P: 1,764
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
Expert Mod 5K+
P: 9,197
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
Expert 100+
P: 1,764
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.