By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

reversing a file

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a

"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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
"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.