简体   繁体   English

C编程-复制文件缓冲区并写入文件

[英]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() . 以下是最后调用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. custom-%ld.log尚不存在,需要创建。

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. 这将分配4或8个字节的缓冲区,具体取决于计算机的字长。 'sizeof' is a compile time directive that gives you the size of the underlying type. 'sizeof'是一个编译时指令,可为您提供基础类型的大小。

char tmp[sizeof(_log)]; 字符tmp [sizeof(_log)];

So do this instead (where 100 is just a big enough number to hold the result): 而是这样做(其中100只是一个足够容纳结果的数字):

char tmp[100]; 字符tmp [100];

Next using a char for 'a' will not be able to hold the EOF value. 接下来,将char用作'a'将无法保存EOF值。 Use int. 使用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. 通过固定“ a”的定义,您的循环现在不再是无限的,但最终它将把恒定的EOF写入文件,这将是乱码。 Change it like so: 像这样更改它:

while ((a = fgetc(fp1)) != EOF) { fputc(a, fp2); 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 您正在将EOF写入文件, 然后使用进行检查

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. EOF不是文件的一部分(某些旧格式除外)。 It is an indicator returned by file reading functions. 它是文件读取功能返回的指示符。

Note that fflush(fp1); 注意fflush(fp1); is undefined behaviour when fp1 is opened for input. 当打开fp1进行输入时,是未定义的行为。

You say "custom-%ld.log" does not already exist yet you open it with 您说"custom-%ld.log"尚不存在,但是您使用

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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