473,480 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Copying a Text File in Reverse

2 New Member
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 4249
Savage
1,764 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 Recognized Expert Moderator Expert
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 Recognized Expert Top Contributor
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

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

Similar topics

3
3616
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
22
6927
by: Matt | last post by:
When browsing a web page a user has the ability to highlight content on a page (by holding down the left mouse button and dragging the mouse over the desired content). Is there a way to disable...
6
6313
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
2
1948
by: seash | last post by:
H I developed an windows form application(visual c#), i want to protect it against copying(only licensed should be able to use) and i heard a topic like reverse engineering(c# decompilers) etc...Is...
8
16152
by: John Smith | last post by:
Hi folks, I know how to place text into the user's clipboard: Clipboard.SetDataObject("My Copied Text"); but how do I place a file in there? So, if I have a file C:\test.txt, how can I place...
4
3555
by: Stupid48 | last post by:
I'm trying to do a simple task but can't seem to find a solution. How do I read lines from a text file backwards. i.e. I want to select the last 20 lines of a text file and display them in...
6
4946
by: kimiraikkonen | last post by:
Hi, I use system.io.file class to copy files but i have a difficulty about implementing a basic / XP-like progress bar indicator during copying process. My code is this with no progress bar,...
4
1594
by: Jim Barlow | last post by:
Does anyone know why K&R2 uses the term "File Copying" at this point (1.5.1)? Also, in the K&R2 answers to exercises maintained by Richard Heathfield, for Listing KRX113 Mr Heathfield repeatedly...
11
4179
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6911
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.