C语言路径中能引用变量吗?

2025-03-13 15:37:03
推荐回答(2个)
回答1:

路径设为字符串变量,你可以用字符串输入和运算改变字符串内容。
char path[100];
gets(path); 可以输入。
=========
char a[]="abc";
sprintf(path,"d:\\\\%s.txt",a);

回答2:

#include 

//可以使用字符串格式化命令sprintf 
//将多个字符串链接起来
//函数包含在 
//使用和printf基本一样
//只是多了一个char *类型的参数
//指向链接后的字符串所保存的位置 

int main(void)
{
char a[] = "test";
char b1[] = "d:\\";
char b2[] = ".txt";
char loc[50] = {0};//保存连接后的字串,大小随意 
sprintf(loc,"%s%s%s",b1,a,b2);
if(fopen(loc,"w"))
printf("success\n");
else
printf("error\n");
return 0;
}