在 c 中扫描空白/整行 [英] Scan a white space/whole line in c

查看:43
本文介绍了在 c 中扫描空白/整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我知道之前有人问过这个问题,但我似乎无法解决任何问题.我现在拥有的是:

#includestruct ClothingCustomer{字符名称[20];年龄;双内缝;};struct ClothingCustomer createACustomer(){struct ClothingCustomer aCustomer;printf("请输入客户姓名:");scanf("%s",aCustomer.name);printf("年龄:");scanf("%d",&aCustomer.age);printf("内缝:");scanf("%lf",&aCustomer.inseam);返回一个客户;};int main(){FILE*customersFile = fopen("customers.txt","w");for (int i = 0; i <5; i++){struct ClothingCustomer aCustomer = createACustomer();fprintf(customersFile, "%s %d %lf\n", aCustomer.name, aCustomer.age, aCustomer.inseam);}fclose(customersFile);返回0;}

无论我做什么尝试让它扫描多个单词,比如名字/姓氏或其他东西,它都有效,但这是我在运行它时在控制台中得到的(使用扫描选项尝试跳过下面列出的空格;上面的代码功能正常,但不允许出现空格):

输入客户名称:年龄:内缝:输入客户姓名: 年龄:内缝: 输入客户姓名: 年龄:内缝:输入客户姓名: 年龄:内缝:输入客户姓名: 年龄:内缝:

我怎样才能让它不这样做?我试过使用:

[^\n]fgets(name, sizeof(name), stdin);

每次都会发生同样的事情.

解决方案

This Will Work

 #include#includestruct ClothingCustomer createACustomer(void);struct ClothingCustomer{字符名称[20];年龄;双内缝;};struct ClothingCustomer createACustomer(void){struct ClothingCustomer aCustomer;{//从这里开始您遇到问题的部分.字符 c;国际我;printf("请输入客户姓名:");scanf("%s",aCustomer.name);i = strlen(aCustomer.name);//用户输入到第一个空格的长度做{scanf("%c", &c);aCustomer.name[i++] = c;//读取第一个空格后的字符(包括它)}while (c != '\n');//直到用户点击 EnteraCustomer.name[i - 1] = 0;//字符串终止}printf("年龄:");scanf("%d",&aCustomer.age);printf("内缝:");scanf("%lf",&aCustomer.inseam);返回一个客户;};int main(){FILE*customersFile = fopen("customers.txt","w");int i = 0;for (i = 0; i <5; i++){struct ClothingCustomer aCustomer = createACustomer();fprintf(customersFile, "%s %d %lf\n", aCustomer.name, aCustomer.age,aCustomer.inseam);}fclose(customersFile);返回0;}

我强烈建议你看看这个答案,它会对你有很大帮助,我使用的方法这里提到了上面的答案.请给出answer 如果此方法对您有效.这是您在 中遇到问题的部分的说明,它现在如何工作.

<块引用><块引用>

这是如何工作的?当用户从标准输入输入字符时,它们将存储在字符串变量中,直到第一个空格.之后,其余的条目将保留在输入流中,并等待下一次扫描.接下来,我们有一个 for 循环,它从输入流(直到 \n)中逐个字符获取并将它们附加到字符串变量的末尾,从而形成与用户从键盘输入相同的完整字符串.

So, I know this question has been asked before, but I can't seem to make anything work. What I have right now is this:

#include<stdio.h>

struct ClothingCustomer{
  char name[20];
  int age;
  double inseam;
};

struct ClothingCustomer createACustomer(){
  struct ClothingCustomer aCustomer;

  printf("Enter Customer Name: ");
  scanf("%s",aCustomer.name);

  printf("Age: ");
  scanf("%d",&aCustomer.age);

  printf("Inseam: ");
  scanf("%lf",&aCustomer.inseam);
  return aCustomer;
};

int main(){
  FILE* customersFile = fopen("customers.txt","w");
  for (int i = 0; i < 5; i++){
    struct ClothingCustomer aCustomer = createACustomer();
    fprintf(customersFile, "%s %d %lf\n", aCustomer.name, aCustomer.age,     aCustomer.inseam);
  }

  fclose(customersFile);
  return 0;
}

No matter what I do to try to make it scan more than one word, like a first/last name or something, it works, but here's what I get in the console while running this(with the scan options to try to get past a white space listed below; the above code functions correctly, but doesn't allow white space):

Enter Customer Name:
Age:
Inseam:
Enter Customer Name: Age:
Inseam: Enter Customer Name: Age:
Inseam:
Enter Customer Name: Age:
Inseam:
Enter Customer Name: Age:
Inseam:

How can I make it not do this? I've tried using:

[^\n]
fgets(name, sizeof(name), stdin);

and the same thing happens every time.

解决方案

This Will Work

  #include<stdio.h>
  #include<string.h>
  struct ClothingCustomer createACustomer(void);
  struct ClothingCustomer{
       char name[20];
             int age;
       double inseam;
                    };

       struct ClothingCustomer createACustomer(void){
       struct ClothingCustomer aCustomer;
       {                 //From Here Starts The Part in Which You Are Having Problems.
       char c;
        int i;
       printf("Enter Customer Name: ");
       scanf("%s",aCustomer.name);
       i = strlen(aCustomer.name);      // length of user input till first space
       do{
       scanf("%c", &c);
       aCustomer.name[i++] = c;  // reading characters after first space (including it)
       }while (c != '\n');     // until user hits Enter

       aCustomer.name[i - 1] = 0;       // string terminating
       }
       printf("Age: ");
       scanf("%d",&aCustomer.age);

       printf("Inseam: ");
       scanf("%lf",&aCustomer.inseam);
       return aCustomer;
       };

       int main(){
             FILE* customersFile = fopen("customers.txt","w");
             int i = 0;

             for (i = 0; i < 5; i++){
             struct ClothingCustomer aCustomer = createACustomer();
             fprintf(customersFile, "%s %d %lf\n", aCustomer.name, aCustomer.age,aCustomer.inseam);
             }
             fclose(customersFile);
             return 0;
             }

I Highly Recommend You To Take A Look on this answer , it will help you a lot , the method I used in here is mentioned in the above answer.Please Give That answer Credit If this method works for you. Here is the explanation for the part which you were having problem in , how is it working now.

How this works? When user inputs characters from standard input, they will be stored in string variable until first blank space. After that, rest of entry will remain in input stream, and wait for next scanf. Next, we have a for loop that takes char by char from input stream (till \n) and appends them to end of string variable, thus forming a complete string same as user input from keyboard.

这篇关于在 c 中扫描空白/整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆