用户输入(字符串)打开文件 - ç [英] Open file with user input (string) - C
本文介绍了用户输入(字符串)打开文件 - ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找出用户的输入如何被用作C.文件名下面是简单的程序,我写的。
I'm trying to figure out how user input can be used as a filename in C. Here's the simple program I wrote.
#include <stdio.h>
#define MAX 100
int main()
{
FILE *fp;
char name[MAX];
printf("Enter filename: ");
fgets(name, MAX, stdin);
if((fp = fopen(name, "w")) == 0)
printf("File cannot be opened!");
return 0;
}
它总是打印文件无法打开。
It always prints "File cannot be opened".
推荐答案
与fgets
保留输入换行符。
这篇关于用户输入(字符串)打开文件 - ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文