简体   繁体   中英

C Programming - Copy File Buffer and Write to File

I'm making some small changes to a preexisting application. The app is writing lines of text to a buffer and then flushing the buffer. I'm not sure when it actually writes the text to the file, but I'm trying to copy everything in that buffer and write a copy of it all out to a completely different file.

Below is the last line of writing to the preexisting buffer before it eventually calls fflush() .

fprintf(_log, "-- FINAL TEXT LINE --\n");

Below is my code that I'm using in an attempt to copy that buffer to a separate file which is dynamically named according to the log time. This custom-%ld.log does not already exist and needs to be created.

char tmp[sizeof(_log)];

sprintf(tmp, "custom-%ld.log", (long int)lf->time);

FILE *fp1, *fp2;
char a;

fp1 = _log;

fp2 = fopen(tmp, "a");
if (fp2 != NULL) {

    do {
      a = fgetc(fp1);
      fputc(a, fp2);
    } while (a != EOF);

    fflush(fp1);
    fclose(fp2);
}

fflush(_log);

I'm sure my mistakes are very basic, but I don't know what I'm doing. I've tried dozens of suggestions on other websites, and suggestions here from other questions, but I'm not having any luck.

Yes, there are a few mistakes in here.

This will allocate a buffer of 4 or 8 bytes depending on the word size of your computer. 'sizeof' is a compile time directive that gives you the size of the underlying type.

char tmp[sizeof(_log)];

So do this instead (where 100 is just a big enough number to hold the result):

char tmp[100];

Next using a char for 'a' will not be able to hold the EOF value. Use int.

int a;

By fixing the definition of 'a' your loop is now not infinite, but it will eventually write the constant EOF to the file, which will be some garbled character. Change it like so:

while ((a = fgetc(fp1)) != EOF) { fputc(a, fp2); }

So in the end you should have:

char tmp[100];

sprintf(tmp, "custom-%ld.log", (long int)lf->time);

FILE *fp1, *fp2;
int a;

fp1 = _log;

fp2 = fopen(tmp, "a");
if (fp2 != NULL) {
    while ((a = fgetc(fp1)) != EOF) {
        fputc(a, fp2);
    }

    fflush(fp1);
    fclose(fp2);
}

fflush(_log);

You are writing EOF to file before checking it with

do {
    a = fgetc(fp1);
    fputc(a, fp2);
} while (a != EOF);

You could try

int a;                          // make sure you have the correct type
while((a = fgetc(fp1)) != EOF)
    fputc(a, fp2);
}

EOF is not (except some legacy formats) part of the file. It is an indicator returned by file reading functions.

Note that fflush(fp1); is undefined behaviour when fp1 is opened for input.

You say "custom-%ld.log" does not already exist yet you open it with

fp2 = fopen(tmp, "a");

Which would append, if you forgot to delete a previous version. To make sure it is a new file, open with

fp2 = fopen(tmp, "w");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM