我想打开名为 ex1、ex2、ex3 ...exn
等的文件。
现在,当我输入 n
的值时,n=1,ex1
将被打开
for, n=2, ex2
文件将被打开,然后我将从其中读取或写入我的 C 程序输出数组。
文件名可以以字符串形式给出吗?
由于我是编程新手,请帮助我解决这个问题。
最佳答案
通常,当您打开文件时,您会使用函数fopen
fp = fopen ("file.txt", "w+");
if (fp == NULL)
{
exit(1); // Or you can raise some error code and return if this code is in a function.
}
// Process the file
现在,在您的情况下,您需要操作文件名。因此您可以为此使用 C 字符串。
char filename[10];
// N is set from code above
sprintf(filename,"ex%d",N);
fp = fopen (filename, "w+");
// Further behaviour is same
关于c - 如何在c中使用数组打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50960815/