简体   繁体   English

在while循环中将scanf用于char数组

[英]Using scanf for char array in while loop

I am new to C programming. 我是C编程新手。 I was curious as to see how much I have learnt C. Therefore I thought of creating a program in which I could simply create a file and write in it. 我很想知道我学了多少C语言。因此,我想到了创建一个程序的方法,可以在其中简单地创建文件并写入其中。 The name of the file, I thought, should be less that 100 chars. 我认为文件名应少于100个字符。 But it doesn't matter if it is a string or one word or a letter. 但是,它是一个字符串还是一个单词或一个字母都没有关系。 I couldn't complete because I was stuck on fact that how to input a string for a file name(eg, Project work, New Doc1, etc) 我无法完成,因为我坚持如何为文件名输入字符串(例如,Project work,New Doc1等)

So I wrote this; 所以我写了这个

int main()
{

    int a = 0;

    while(a != 5)
    {
        puts("Put a number: ");
        scanf("%i", &a);
        if(a == 1)
        {
            char name[30];
            printf("Put a name: ->>");
            for(int i = 0;i < 31 && name[i] != '\n';i++)
            {
                name[i] = getchar();

            }
            char ex[50] = ".txt";

            strcat(name,ex);
            printf("%s",name);

        }
    }

    return 0;
}

The problem is while inputting the name, it doesn't stop at the next (when I press enter) and some how it is not printing the right file name either. 问题是在输入名称时,它不会在下一个(当我按Enter键时)停止,并且在某些情况下它也不会打印正确的文件名。

There's a lot of problems with you approach. 您的方法有很多问题。

  1. It's not good to mix scanf with another input primitives, you must flush stdin from any remaining characters. 将scanf与其他输入原语混合使用不是很好,您必须从所有剩余字符中清除标准输入。
  2. Any string must end in '\\0' in order to mark that string as complete. 任何字符串都必须以“ \\ 0”结尾,以将该字符串标记为完整。 So you must reserve space to this character. 因此,您必须为此字符保留空间。
  3. When you concat you must obey the limit of the string. 连接时,您必须遵守字符串的限制。
  4. If you use printf, the string will be only displayed after flushing the stdout (when you put '\\n' in the end of the string, flush is done) 如果您使用printf,则仅在刷新标准输出后才显示该字符串(当在字符串末尾添加“ \\ n”时,将完成刷新)

Try this: 尝试这个:

#include <stdio.h>
#include <string.h>   

int main()
{

    int a = 0;

    while(a != 5)
    {
        int ch;
        puts("Put a number: ");
        scanf("%d", &a);
        /* flush any remaining characters */
        while ((ch=getchar()) != EOF && ch != '\n'); /* issue 1 */
        if(a == 1)
        {
            int i = 0;
            char name[30];
            printf("Put a name: ->>");
            fflush(stdout); /* issue 4 */

            while ((ch=getchar()) != EOF && ch != '\n' && i < 25) /* issue 3 */
                name[i++] = ch;
            name[i] = '\0'; /* issue 2 */

            /* flush any remaining characters [if input > 25 chars] */
            if (ch != EOF && ch != '\n') while ((ch=getchar()) != EOF && ch != '\n');

            char ex[50] = ".txt";

            strcat(name,ex); /* issue 3 */
            printf("%s\n",name);

        }
    }

    return 0;
}

Also, consider use getline and atoi instead of getchar and scanf 另外,考虑使用getlineatoi代替getcharscanf

#include<stdio.h>
main()
{
    static char stop_char='y';
    char input=0;
    do{
        printf("please input a character\n");
        scanf("\n%c",&input);
    }while(input!=stop_char);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM