reversing a file

N

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.
 
T

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:

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::string> v;
while(!in.eof() && in) {
std::string str;
std::getline(in,str,'\n');
v.push_back(str);
}
std::eek:fstream out("out.txt");
if(out) {
std::copy(v.rbegin(),v.rend(),
std::eek:stream_iterator<std::string>(out,"\n"));
}
}
return 0;
}
 
V

Vikram

TB said:
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:

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::string> v;
while(!in.eof() && in) {
std::string str;
std::getline(in,str,'\n');
v.push_back(str);
}
std::eek:fstream out("out.txt");
if(out) {
std::copy(v.rbegin(),v.rend(),
std::eek:stream_iterator<std::string>(out,"\n"));
}
}
return 0;
}

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.
 
N

neo

Thanks for the solution but the thing is i need to write it in C, not C++.
-neo

TB said:
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:

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::string> v;
while(!in.eof() && in) {
std::string str;
std::getline(in,str,'\n');
v.push_back(str);
}
std::eek:fstream out("out.txt");
if(out) {
std::copy(v.rbegin(),v.rend(),
std::eek:stream_iterator<std::string>(out,"\n"));
}
}
return 0;
}
 
R

Rolf Magnus

Plese don't top-post.
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?
 
N

news.onet.pl

#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top