小编New*_*ner的帖子

如果用户输入空字符串,如何提示输入和退出?

我是Perl的新手,我正在编写一个程序,我想强制用户输入一个单词.如果用户输入空字符串,则程序应退出.

这是我到目前为止:

print "Enter a word to look up: ";

chomp ($usrword = <STDIN>);
Run Code Online (Sandbox Code Playgroud)

perl

10
推荐指数
1
解决办法
3万
查看次数

C - 如何提示用户输入文件名

C 编程的新手。我正在编写一个程序,我想提示用户输入要打开以供阅读的文件的名称。在我下面显示的代码中,如果它没有打开或文件不存在,我想抛出一个错误,但是当我运行它时,我的代码爆炸了,我必须关闭程序(DOS)

/*ask user for the name of the file*/
  printf("enter file name: ");
  gets(fname);


//Opens the file from where the text will be read. 
fp=fopen(fname, "r");

//Checks if the file us unable to be opened, then it shows the error message 
if (fp == NULL)
    {
     printf("\nError, Unable to open the file for reading\n");
    }
Run Code Online (Sandbox Code Playgroud)

// 您可以通过创建 name.txt 文件来测试这一点。如果您需要其他信息,请告诉我。

c printf

0
推荐指数
1
解决办法
3万
查看次数

标签 统计

c ×1

perl ×1

printf ×1