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
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
Actually, I did not use a while loop with fgets. I wrote the following code thinking I had to first load the buffer: - if(fgets(buf, BUFFERSIZE, in) == NULL)
-
fprintf(stderr, "%s: can't transfer\n",
-
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.
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: - if(fgets(buf, BUFFERSIZE, in) == NULL)
-
fprintf(stderr, "%s: can't transfer\n",
-
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |