C如何開啟用變數命名的檔案,C 如何開啟用變數命名的檔案

時間 2021-08-11 17:35:05

1樓:金色潛鳥

c++ 和 c 類似,檔名可以用變數。

例如 char a[5][20]=;

有5 個檔名。 用 a[0] 就用了 abc.txt下面作為 建立和寫 檔案 開啟:

#include

#include

using namespace std;

#include

main();

int x=5;

if ( (fp=fopen(a[0],"w"))==null);

fprintf(fp,"%d\n",x);

fclose(fp);

cout << "output in " <作為輸入檔案開啟方法:

ifstream ifs;

ifs.open (a[0], std::ifstream::in);

作為輸出檔案開啟方法:

ofstream ofs;

ofs.open (a[0], std::ofstream::out);

===若討論字串運算:

char name[80];

char a[10]="123";

int x=4567;

sprintf(name,"%s.txt",a);

name 就變 "123.txt"

sprintf(name,"%d.txt",x);

name 就變 "4567.txt"

開啟語句裡用name就可以

2樓:在漁梁古壩看燃情歲月的薺菜

#include

#include

#include

#include

using namespace std;

int main()

return 0;}

3樓:一生吥勝

#include

#include

#include

using namespace std;

int main(void)

;int i = 0;

string stmp,sfile;

ifstream ifile;

for (i=0;i<5;++i)

cout << "the file " << sfile << " content is:" << endl;

while (getline(ifile,stmp))ifile.close();

ifile.clear();

}getchar();

return 0;

}lz,參考一下吧,大致是這樣了你如果要輸入a[0]的話,可以定義一個函式來接收檔名.

4樓:

std::stringstream ss;

ss << a[0] << ".txt";

ss.str() // 這樣就是"123.txt"了

如何使用c語言開啟檔案,如何使用c語言開啟一個檔案

實現的方法和詳細的操作步驟如下 1 第一步,開啟軟體,在 的第一行寫標頭檔案,然後在此處寫 include 其中包含要在此標頭檔案中呼叫的函式,見下圖,轉到下面的步驟。2 第二步,完成上述步驟後,在第二行中是主要功能,是程式執行的入口點。int maihn void 可以省略void,見下圖,轉到下...

C 中,標頭檔案與名稱空間的關係

yangguo2005 五級。看這個例子 namespace std 建立一個名字空間。如果不使用using 那麼你在外面這樣 temp 9 是錯誤的,不過可以這樣 std temp 9 加上using namespace std後。就可以 temp 9了。當然對於cout來說,你不加using n...

c語言怎開啟有內容的檔案啊,C語言怎開啟一個有內容的檔案啊

安徽新華電腦專修學院 預設路徑是原始檔的路徑。如果指定別的路徑,兩種方法 一是相對當前預設路徑指定路徑。二是指定絕對路徑。 沒有寫讀取檔案內容的函式,肯定是看不到檔案內容的啊。你的程式本身沒有錯誤。const int max size 1024 char sz max size fgets sz,m...