使用变量名的C fopen调用?
是否存在以下情况可以工作?(我试图提取一个变量的值,并根据数组中存储的文本创建一个文件。)使用变量名的C fopen调用?,c,file-io,C,File Io,是否存在以下情况可以工作?(我试图提取一个变量的值,并根据数组中存储的文本创建一个文件。) #包括 int main() { char a=“a”; 归档; out=fopen(“%s.txt”,a,“w”); fclose(out); 返回0; } 谢谢您不能用字符串文本分配char变量。您应该将代码更改为: char a[] = "a"; 另一个问题是,fopen函数只获取两个参数,但传递了三个。否,fopen()返回一个文件* FILE*fopen(常量字符*路径,常量字符*模式) +
#包括
int main()
{
char a=“a”;
归档;
out=fopen(“%s.txt”,a,“w”);
fclose(out);
返回0;
}
谢谢您不能用字符串文本分配char变量。您应该将代码更改为:
char a[] = "a";
另一个问题是,fopen
函数只获取两个参数,但传递了三个。否,fopen()
返回一个文件*
FILE*fopen(常量字符*路径,常量字符*模式)代码>
+
字符a[]=“a”
是否存在以下情况可以工作
没有
不要做假设改为阅读手册。这是值得的
char buf[0x100];
snprintf(buf, sizeof(buf), "%s.txt", random_string);
FILE *f = fopen(buf, "r");
不直接。但是你可以这样间接地做(或者类似的事情)
#包括
int main()
{
char*a=“a”;
char*扩展名=“.txt”;
charfilespec[strlen(a)+strlen(扩展)+1];
归档;
snprintf(fileSpec,sizeof(fileSpec),“%s%s”,a,扩展名);
out=fopen(fileSpec,“w”);
fclose(out);
返回0;
}
不,那不行。您需要使用类似于sprintf()
的中间步骤来编写要传递给fopen()的字符串。您需要阅读更多关于:
文件*fopen(常量字符*文件名,常量字符*模式)
打开文件
打开在参数filename中指定名称的文件,并将其与
可在将来的操作中通过返回的文件指针识别的流
下面介绍如何修复代码
#include <stdio.h>
main(){
char a = 'a';
char filename[64];
FILE *out;
sprintf(filename, "%c.txt", a)
out = fopen( filename, "w");
fclose(out);
return 0;
}
#包括
main(){
字符a='a';
字符文件名[64];
归档;
sprintf(文件名为“%c.txt”,a)
out=fopen(文件名,“w”);
fclose(out);
返回0;
}
不,您必须事先将sprintf()转换为字符串,然后像往常一样调用fopen(名称、模式)。我知道此线程已关闭,但@soon的评论向我建议了一种方法
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fs;
char c[]="Dayum.csv";
fs = fopen( ("%s", c),"a");
if (fs == NULL)
printf("Couldn't open file\n");
for (int i=0; i<5; i++)
for (int j=0; j<5; j++)
fprintf(fs, "%lf,%lf\n",i,j);
fclose(fs);
}
但是,如果您决定添加文件扩展名,如
char c[]="Dayum";
fs = fopen( ("%s.csv",c), "a")
它不起作用。系统创建一个名为“Dayum”的文件,该文件作为普通文本文件处理,而不是csv格式
注意:如果您可以在一个文件中存储扩展名的值,在另一个文件中存储文件名的值,然后将它们连接起来编写filename.extension数组,这也可以达到目的。为了解决这个问题,我做了:
#include <string.h>
int main()
{
char fileName = "a";
FILE *out;
char fileToOpen[strlen(fileName) + 5];
// strlen(fileName)+5 because it's the length of the file + length of ".txt" + char null
strcpy(fileToOpen, fileName);
// Copying the file name to the variable that will open the file
strcat(fileToOpen, ".txt");
// appending the extension of the file
out = fopen( fileToOpen, "w" );
// opening the file with a variable name
if(out == NULL)
{
perror("Error: ");
exit(EXIT_FAILURE);
}
fclose(out);
return EXIT_SUCCESS;
}
#包括
int main()
{
char fileName=“a”;
归档;
char fileToOpen[strlen(文件名)+5];
//strlen(fileName)+5,因为它是文件的长度+长度“.txt”+字符null
strcpy(fileToOpen,fileName);
//将文件名复制到将打开文件的变量
strcat(fileToOpen,.txt);
//追加文件的扩展名
out=fopen(fileToOpen,“w”);
//使用变量名打开文件
if(out==NULL)
{
佩罗尔(“错误:”);
退出(退出失败);
}
fclose(out);
返回退出成功;
}
可能会按你的要求做。可能chara[]=“a”代码>做出假设FTW+1(抵消失望的投票)。虽然这个问题可能没有反映出多年开发C应用程序的经验,但它看起来是一个合理的问题。当存在一个文件%s.txt
并且指向“a”
文本存储位置的指针的第一个字节是'r'
时,它就会起作用,'w'
或类似的东西没有环境允许此函数在C语言中同时具有相同名称的不同参数列表,因此无法定义此函数。@H2CO3为什么?我们已经确保缓冲区大于输入。但是典型的是的,因为有一天你会忘记确保。宁可安全两次也不要失败一次@H2CO3——你不知道我有多肛门~咧嘴笑~但是,我要说的是。@H2CO3是一种元素,通常用C表示。“总有一天你会忘记检查可能的整数溢出”,但你看到有多少人在做这些检查?@undefined_behavior——实际上。。。那会碎的,不是吗。。。因为snprintf()返回写入的大小,不包括null终止符。所以你应该有一个+1
,不是吗char file_spec[snprintf(NULL,0,“%s%s,a,扩展名)+1];
——也就是说,这种方法让我天生就不舒服。“不,fopen()返回一个文件。”*“-是的。但问题不在于它。当然,这样会更安全。变量a
声明为OP代码中的变量时,这是无效的。此外,要实现OP所说的他们打算做的事情,需要的不仅仅是对该声明进行修改,还需要采取类似的方法。
fs = fopen( ("%s", c), "a" )
char c[]="Dayum";
fs = fopen( ("%s.csv",c), "a")
#include <string.h>
int main()
{
char fileName = "a";
FILE *out;
char fileToOpen[strlen(fileName) + 5];
// strlen(fileName)+5 because it's the length of the file + length of ".txt" + char null
strcpy(fileToOpen, fileName);
// Copying the file name to the variable that will open the file
strcat(fileToOpen, ".txt");
// appending the extension of the file
out = fopen( fileToOpen, "w" );
// opening the file with a variable name
if(out == NULL)
{
perror("Error: ");
exit(EXIT_FAILURE);
}
fclose(out);
return EXIT_SUCCESS;
}
strcpy(a, ".txt");
fopen(a, "w");