0

I am writing a program to copy a text file "input.txt" to "output.txt" however instead of copying the first line to the last line I would need to inversely copy the last line to the first line to the "output.txt" file. Could someone please give some advice thanks!

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>


int main()
{
    char filename[]={"input.txt"};
    char filename2[]={"output.txt"};
    char a;

    FILE *inptr, *outptr;

    inptr = fopen(filename, "r");

    if(inptr == NULL)
    {
        printf("The file could not be opened!\n");
        system("pause");
        return 1;
    }

    outptr = fopen(filename2, "w");

    if(outptr == NULL)
    {
        printf("The file could not be opened!\n");
        printf("Creating a new file......\n");
        system("pause");

        return 1;
    }

    while((fscanf(inptr, "%c", &a)) != EOF)
    {
        fprintf(outptr, "%c", a);
        puts("A character was copied!\n\n");
    }

    fclose(inptr);
    fclose(outptr);

    system("pause");
    return 0;
}

For example lets say there are 3 lines in the text file:

Hi Bye Hello

so I would need to copy the contexts to another file but it starts from:

Hello Bye Hi

Thanks!

AskingQnsPro
  • 19
  • 1
  • 1
  • 7

3 Answers3

1

In a method as follows in the case where there is sufficient memory can be read and processed files in memory.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node {
    char *str;
    struct node *next;
} Node;

Node *pushNode(Node *current, char *str){
    Node *node = malloc(sizeof(Node));
    if(node){
        node->str = strdup(str);
        node->next = current;
    }
    return node;
}

char *popNode(Node **current){
    if(*current){
        Node wk = **current;
        free(*current);
        *current = wk.next;
        return wk.str;
    }
    return NULL;
}

#define MAXOFLINESIZE 1024

int main(){
    char *str, buff[MAXOFLINESIZE];//max size include '\0'
    Node *current = NULL;
    FILE *inFile, *outFile;
    inFile = stdin;//Mock
    outFile = stdout;

    while(NULL!=fgets(buff, sizeof(buff), inFile)){
        current=pushNode(current, buff);//need check return of pushNode
    }
    while(NULL!=(str=popNode(&current))){
        fprintf(outFile, "%s", str);
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • note : Adding a new line may be necessary for flies to see if the last line does not contain a newline. – BLUEPIXY Dec 02 '13 at 09:12
  • I think to be able to read the writing by positioned in SEEK using the file instead of memory if there is not enough memory to work. – BLUEPIXY Dec 02 '13 at 09:15
0

You can read the lines from input file in forward direction. Can you think of any data structure where you can store the lines? The data structure should help you output the lines in reverse order.

Santanu C
  • 1,362
  • 3
  • 20
  • 38
  • 2
    @AskingQnsPro, does "first-in last-out" ring a bell? :) – Guido Dec 01 '13 at 23:17
  • Im quite new at C Programming so im not sure of any identifiers. I would create multiple arrays according to the number of lines but im running C89 which does not allow it. – AskingQnsPro Dec 01 '13 at 23:22
0

I wouldn't read it char by char. There are line by line reading functions in C, such as fgets(). So for each line, you could allocate a buffer, read the line into the buffer, and store a pointer to the buffer in an array. Once you're done. You go from the end of your array and output the stored strings. The painful part about this is that C doesn't have dynamic arrays so you must emulate that (malloc, free), unless you know the maximum length of your lines and their maximum number beforehand.

Alternatively, there should be a way to do this without loading the whole file into memory (marking newline positions within your source file and then seeking to them in reverse order).

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142