这个想法是,主要打开几个文件,并给它们写入一个条件,fp通过ref在这里和那里传递,writeresult负责保存在实际的文件中。 我们可以知道它在写什么文件吗?如何知道哪个文件是一个被引用的fp正在写入?
#include <stdio.h>
#include <string.h>
void Write2File(char *iMessage, FILE *fp)
{
fprintf(fp, "%s\n", iMessage);
// can i know in here where it is printing ?
}
int main(int argc, const char * argv[])
{
FILE *fp1, *fp2, *fp3, *fp4;
char message[250];
memset(message, '\0', 250);
strncpy(message, "sample text", 10);
fp1 = fopen("./output1.txt", "w+");
fp2 = fopen("./output2.txt", "w+");
fp3 = fopen("./output3.txt", "w+");
fp4 = fopen("./output4.txt", "w+");
Write2File(message, fp1);
Write2File(message, fp2);
Write2File(message, fp3);
Write2File(message, fp4);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
return 0;
}
为了记录,我不认为在我知道的任何操作系统(至少那些提供文件流的操作系统)中,文件流和磁盘文件之间存在直接对应关系。这并不意味着答案是错误的(不是),但是当我阅读它时,它似乎暗示它是“一件Linux事情”:-) – Jcl 2015-02-24 18:02:47
对。我说“在Linux上”,因为我在例子中使用了/ dev,hardlinks和/ proc。我会澄清。 – 2015-02-24 18:12:49
另外,请考虑由POSIX定义的'fmemopen',其中文件描述符可能实际上不存在。 – 2015-02-24 19:00:41