473,379 Members | 1,302 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,379 software developers and data experts.

reversing a file

neo
Hello everyone,
I want to write a function that takes a simple text file as input and
outputs a file such that the last line in the input file becomes the first
line in the output file and the first line of the input becomes the last
line of output.

I had written this function as:

void fileReverse(char* inputFileName, char *outputFileName)
{
FILE *input = fopen(inputFileName, "r");
FILE *output = fopen(outputFileName, "w");

int ch, prev = '\n';
while ( (ch = fgetc(input)) != EOF ) /* Read all chars in the file. */
{
if ( ch == '\n' )
{
fputc(EOF, output);
fseek(output, 0, SEEK_SET);
// rewind(output);
fputc(ch, output);
fseek(output, 0, SEEK_SET);
// rewind(output);
}
else
{
fputc(ch, output);
}
prev = ch; /* Keep a copy to later test whether... */
}

if ( prev != '\n' ) /* ...the last line did not end in a newline. */
{
fputc('\n', output);
}

fclose(output);
fclose(input);
}

But on giving input file input.txt as:
line 1
line 2
line 3

it gives output file output.txt as:
line 3

Can someone suggest me a solution?

Regards,
neo.
Jul 7 '06 #1
6 1852
TB
neo skrev:
Hello everyone,
I want to write a function that takes a simple text file as input and
outputs a file such that the last line in the input file becomes the first
line in the output file and the first line of the input becomes the last
line of output.

I had written this function as:
<code snipped>
>
But on giving input file input.txt as:
line 1
line 2
line 3

it gives output file output.txt as:
line 3

Can someone suggest me a solution?
#include <fstream>
#include <string>
#include <vector>
#include <algorithms>

int main(int argc, char* argv[])
{
std::ifstream in("in.txt");
if(in) {
std::vector<std::stringv;
while(!in.eof() && in) {
std::string str;
std::getline(in,str,'\n');
v.push_back(str);
}
std::ofstream out("out.txt");
if(out) {
std::copy(v.rbegin(),v.rend(),
std::ostream_iterator<std::string>(out,"\n"));
}
}
return 0;
}
--
TB @ SWEDEN
Jul 7 '06 #2

"TB" <TB@SWEDENwrote in message
news:44***********************@news-taz.teranews.com...
#include <fstream>
#include <string>
#include <vector>
#include <algorithms>
#include <algorithm>

Jul 7 '06 #3

TB wrote:
neo skrev:
Hello everyone,
I want to write a function that takes a simple text file as input and
outputs a file such that the last line in the input file becomes the first
line in the output file and the first line of the input becomes the last
line of output.

I had written this function as:

<code snipped>

But on giving input file input.txt as:
line 1
line 2
line 3

it gives output file output.txt as:
line 3

Can someone suggest me a solution?

#include <fstream>
#include <string>
#include <vector>
#include <algorithms>

int main(int argc, char* argv[])
{
std::ifstream in("in.txt");
if(in) {
std::vector<std::stringv;
while(!in.eof() && in) {
std::string str;
std::getline(in,str,'\n');
v.push_back(str);
}
std::ofstream out("out.txt");
if(out) {
std::copy(v.rbegin(),v.rend(),
std::ostream_iterator<std::string>(out,"\n"));
}
}
return 0;
}
--
TB @ SWEDEN
And if it is a huge file, better split your output to multiple files
(clearing the vector after every iteration) and in the end, just append
those files in reverse order.

Jul 7 '06 #4
neo
Thanks for the solution but the thing is i need to write it in C, not C++.
-neo

"TB" <TB@SWEDENwrote in message
news:44***********************@news-taz.teranews.com...
neo skrev:
>Hello everyone,
I want to write a function that takes a simple text file as input and
outputs a file such that the last line in the input file becomes the
first line in the output file and the first line of the input becomes the
last line of output.

I had written this function as:

<code snipped>
>>
But on giving input file input.txt as:
line 1
line 2
line 3

it gives output file output.txt as:
line 3

Can someone suggest me a solution?

#include <fstream>
#include <string>
#include <vector>
#include <algorithms>

int main(int argc, char* argv[])
{
std::ifstream in("in.txt");
if(in) {
std::vector<std::stringv;
while(!in.eof() && in) {
std::string str;
std::getline(in,str,'\n');
v.push_back(str);
}
std::ofstream out("out.txt");
if(out) {
std::copy(v.rbegin(),v.rend(),
std::ostream_iterator<std::string>(out,"\n"));
}
}
return 0;
}
--
TB @ SWEDEN

Jul 7 '06 #5
Plese don't top-post.

neo wrote:
Thanks for the solution but the thing is i need to write it in C, not C++.
Why are you asking in a C++ newsgroup then instead of a C newsgroup?

Jul 7 '06 #6
"TB" <TB@SWEDENwrote in message
news:44***********************@news-taz.teranews.com...
>>#include <fstream>
#include <string>
#include <vector>
#include <algorithms>


#include <algorithm>
I tried using the code in Borland Builder 6.0 and I get the error:
'ostream_iterator' is not a member of 'std'.
Is it included in other file
PZ
Jul 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kevin | last post by:
Hello, I need to some help in reversing an 2-dimensional array. I am working with gif images and I am trying to make the mirror image. I was hoping that someone could help give me a headstart...
11
by: Tim Marshall | last post by:
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users to pick colours for various control properties in certain apps. Is there a way to take the colour code that is displayed in...
45
by: Ajay | last post by:
Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed.
3
by: dru | last post by:
Problem: Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the...
4
by: hello12 | last post by:
Hello, I am new to java and i was having a hard time figuring out on how to do certain string manipulations. I was asked to read in a text file and reverse the words. So far, I have put all...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
16
by: Scott | last post by:
Yeah I know strings == immutable, but question 1 in section 7.14 of "How to think like a computer Scientist" has me trying to reverse one. I've come up with two things, one works almost like it...
7
by: benn686 | last post by:
Anyone know of an efficient way of reversing the bits of a word??
8
by: gearoid | last post by:
Hey I've designed an intranet site where users can upload documents (called 'resources') as part of a knowledge base. Part of the functionality of said site is that privilaged users can edit the...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
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.