-2

I am having problems with copying txt files. I need to info from one file to another.

My code looks like this,

_tprintf (TEXT("%s\n"), FindFileData.cFileName);

memset(fileName, 0x00, sizeof(fileName));
_stprintf(fileName, TEXT("%s\\%s"), path, FindFileData.cFileName); //iegust

FILE *fptr = fopen(fileName, "r");//atver

fscanf(fptr,"%[^\n]",c); //iegust datus no faila
printf("Data from file:\n%s",a);

strcpy(a, c); //nokope datus
buffer2 = strtok (c, ","); //norada partraukumu un tadas lietas
while (buffer2) {
        buffer2 = strtok (NULL, ",");
        if(i<1){ printf("%s\n", c);} 
        i++;
        while (buffer2 && *buffer2 == '\040'){
                buffer2++;
                // TODO ieliec iekavinas
        }
}

And after that I use basic fputs(). My problem is that this code ignores new lines. It prints out fine, each string in it's own line, but that does not happen in file. (\n).

Eric Tsui
  • 1,924
  • 12
  • 21
Them4
  • 1
  • 2

1 Answers1

1

Your problem is that you just need to copy information from one file to another. So, why you don't use a simple solution to do it than your. I have a snipet code can solve your problem easily as shown below.

If I am wrong about your question, please give me advices.

#include <stdio.h>
#include <stdlib.h> // For exit()

int main()
{
    FILE *fptr1, *fptr2;
    char filename[100], c;

    printf("Enter the filename to open for reading \n");
    scanf("%s", filename);

    // Open one file for reading
    fptr1 = fopen(filename, "r");
    if (fptr1 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }

    printf("Enter the filename to open for writing \n");
    scanf("%s", filename);

    // Open another file for writing
    fptr2 = fopen(filename, "w");
    if (fptr2 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }

    // Read contents from file
    c = fgetc(fptr1);
    while (c != EOF)
    {
        fputc(c, fptr2);
        c = fgetc(fptr1);
    }

    printf("\nContents copied to %s", filename);

    fclose(fptr1);
    fclose(fptr2);
    return 0;
}
NoName
  • 877
  • 12
  • 28
  • This is just a part of it.. I have a template, i copy template to new file, then i take info from another file in put each string separated by comma in specific line. – Them4 Jun 30 '15 at 08:42