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