468,458 Members | 1,825 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

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 1574
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Kevin | last post: by
11 posts views Thread by Tim Marshall | last post: by
45 posts views Thread by Ajay | last post: by
3 posts views Thread by dru | last post: by
4 posts views Thread by hello12 | last post: by
16 posts views Thread by Scott | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.