[英]Writing (logging) into same file from different threads , different functions?
[英]Writing a file with different extension but with same name as the file being read?
我想使用freopen寫入與在程序中讀取的文件名稱相同但擴展名不同的文件嗎?
假設我正在讀取文件abc.jpg,我想在同一程序中寫入文件abc.txt。
我給文件的縮寫作為參數,但得到編譯錯誤。 我正在讀取多個文件並寫入多個文件。
freopen ( "abc" + ".txt" , "w" , stdout ) ;
error : cannot convert parameter 1 from 'std:string' to 'char *'
+
運算符不使用兩個字符串文字進行字符串連接。 在C ++中(按標簽進行),可以使用std::string
進行串聯:
#include <string>
...
std::string baseFilename("abc");
std::string newFilename(baseFilename + ".txt");
freopen(newFilename.c_str(), "w", file);
std::string
類確實支持通過+
串聯。 請注意,由於freopen()
函數仍采用C樣式的字符串指針( const char *
),因此我們使用c_str()
)。
正如所寫的那樣,它不是C ++。
#include <cstdio>
#include <string>
FILE* fp;
...
freopen ( (std::string("abc")+std::string(".txt")).c_str() , "w" , fp ) ;
作為函數使用const char *和文件指針FILE *。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.