這個想法是,主要打開幾個文件,並給它們寫入一個條件,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