C语言 输入要读取的自定义文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2795382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enter custom file name to be read?
提问by NLed
I want to allow users to type the name of any .txt file to be read/written.
我想允许用户键入要读/写的任何 .txt 文件的名称。
This is my code :
这是我的代码:
printf("Enter .txt file name\n");
scanf("%s",&fname);
FILE *inputf;
inputf=fopen(&fname,"w");
Problem is this method does not work (having &fname) as a parameter.
问题是此方法不起作用(具有 &fname)作为参数。
I can imagine its because C needs "filename.txt" for it work ... even if I enter for example : "custom.txt", the program returns an error of "Storage block not big enough for this operation"
我可以想象它是因为 C 需要“filename.txt”才能工作......即使我输入例如:“custom.txt”,程序也会返回“存储块不够大,无法进行此操作”的错误
What is the correct method to accomplish this ?
完成此操作的正确方法是什么?
Im using C and im pretty much using basic commands .. (not too advanced)
我使用 C 并且我几乎使用基本命令..(不太高级)
Thanks alot !!!
非常感谢 !!!
回答by Sukanto
The scanf statement will try to store the filename entered as input into the memory, starting from the address passed as its 2nd argument. So you have to allocate/reserve some memory and pass its address to scanf.
scanf 语句将尝试将作为输入输入的文件名存储到内存中,从作为其第二个参数传递的地址开始。因此,您必须分配/保留一些内存并将其地址传递给 scanf。
As you have not mentioned the type of fname, let me list the possibilities and then answer you.
由于您没有提到fname的类型,让我列出可能性然后回答您。
char fname;
char fname;
The 2nd argument of scanf and the 1st argument of fopen, both need to be char *. So, passing address of fname or &fname is valid. But it has a problem.
scanf的第二个参数和fopen的第一个参数,都需要是char *。因此,传递 fname 或 &fname 的地址是有效的。但它有一个问题。
When you declare 'char fname' you are reserving memory for only 1 char. When scanf tries to store the input filename, it will have to write more than 1 char. So eventually you end up overwriting some other memory.
当您声明 'char fname' 时,您只为 1 个字符保留了内存。当 scanf 尝试存储输入文件名时,它必须写入 1 个以上的字符。所以最终你最终会覆盖其他一些内存。
char *fname;
char *fname;
In this case pass fname to both scanf and fopen, instead of '&fname'. But you have to allocate some memory (e.g. using malloc), before using fname. Otherwise fname will contain some garbage address and scanf will try to overwrite some random memory.
在这种情况下,将 fname 传递给 scanf 和 fopen,而不是 '&fname'。但是您必须在使用 fname 之前分配一些内存(例如使用 malloc)。否则 fname 将包含一些垃圾地址并且 scanf 将尝试覆盖一些随机内存。
So either declare fname as char fname[N]or char *fname = malloc(N+1);(where N is the maximum possible length of filename you would be entering).
因此,要么将 fname 声明为char fname[N]或 char *fname = malloc(N+1);(其中 N 是您将输入的文件名的最大可能长度)。
And then, pass fname to both scanf and fopen as follows:
然后,将 fname 传递给 scanf 和 fopen,如下所示:
scanf("%s",fname);
inputf = fopen(fname,"w");
回答by Péter T?r?k
Defining fnameas a char array, and assuming you expect the filename (without extension) as input (which means you need to append the extension to it):
定义fname为字符数组,并假设您希望文件名(不带扩展名)作为输入(这意味着您需要将扩展名附加到它):
char fname[128];
printf("Enter .txt file name\n");
scanf("%123s",fname);
strcat(fname,".txt");
FILE *inputf;
inputf=fopen(fname,"w");
Note that an input length check is added to avoid buffer overflow errors in scanf.
请注意,添加了输入长度检查以避免scanf.
回答by swegi
Try inputf = fopen(fname,"w");.
试试inputf = fopen(fname,"w");。
回答by Leo
I think this can help
我认为这可以帮助
#include <stdio.h>
void read_name(char *);
int main(void)
{
char name[BUFSIZ];
char line[BUFSIZ];
FILE *f;
printf("Name ");
read_name(name);
if ( (f=fopen(name,"r"))==NULL)
return -1;
else
return 0;
fclose(f);
}
void read_name(char *s)
{
int i;
fgets(s,BUFSIZ,stdin);
for (i=0; s[i]!='\n'; i++);
s[i]='##代码##';
return;
}

