473,382 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 4245
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

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

Similar topics

3
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
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
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
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.