【问题标题】:Not able to accept white spaces in string in C [duplicate]无法接受 C 中字符串中的空格 [重复]
【发布时间】:2017-10-02 02:05:42
【问题描述】:

如果我能得到这方面的帮助,我会很高兴。我无法接受字符串中的空格。我正在使用 C 进行套接字编程。我尝试了以下操作。请帮帮我。

  1. char name[100]; fgets (name, 100, stdin);

我尝试在 fgets 之前使用fflush(stdin);

  1. fgets (name, 100, stdin);

  2. scanf("%[^\n]s",name); (参考 1,2&3:Reading string from input with space character?

  3. scanf("%[^\t]s",name); 为我工作,但我必须在输入后按 Tab+Enter。因此,我在名称后输入的所有内容都将打印在服务器的下一行。我想创建类似表的结构。如何接受带有空格的字符串输入?提前谢谢你

client.c

#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#define SERVER_PORT 5432
#define MAX_LINE 256

struct course{
  char name[100];
  char id[20];
  int registered;
  int empty;
  char time[20];
};

int main(int argc, char * argv[])
{
    struct course c;
    FILE *fp;
    struct hostent *hp;
    struct sockaddr_in sin;
    char *host;
    char buf[MAX_LINE];
    int s, new_s,n;
    int len;
    int rec=1;
    char data[1000];
    int choice;
    char search[20], del[50];
    char add[1000]="\n";
    char avail[10],ocu[10],sem[10],id[10];


    if (argc==2) {
        host = argv[1];
    }
    else {
        fprintf(stderr, "usage: simplex-talk host\n");
    exit(1);
    }

    hp = gethostbyname(host);
    if (!hp) {
        fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
        exit(1);
    }

    bzero((char *)&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
    sin.sin_port = htons(SERVER_PORT);

    if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
        perror("simplex-talk: socket");
        exit(1);
    }
    if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
        perror("simplex-talk: connect");
        close(s);
        exit(1);
    }


    do{
        printf("\n\n*************************************************\n");
        printf(" 1.List All \n 2.Search \n 3.Add \n 4.Delete \n 9.Quit \n");
        printf("\nPlease enter your choice: \n");
        scanf("%d",&choice);


        switch(choice)
        {
        case 1: send(s, &choice,sizeof(int), 0);
            printf("\n==============Requesting All Records..=======================\n");
            len = recv(s, &data,sizeof(data), 0);
            fputs(data,stdout);
            break;

        case 2: send(s, &choice,sizeof(int), 0);
            printf("\nPlease Enter Course Name/ID: ");
            scanf("%s",&search);
            send(s, &search,sizeof(search), 0);
            printf("\n======================Searching for The Record..================\n");
            len = recv(s, &data,sizeof(data), 0);
            fputs(data,stdout);
            break;

        case 3: send(s, &choice,sizeof(int), 0);
            printf("Press Tab And Enter After Entering Course Name...\n");
            printf("Please Enter Course ID: ");
            scanf("%s",&c.id);
            printf("Please Enter Course Name: ");
            scanf("%[^\t]s",&c.name);
            printf("No of Students Registered: ");
            scanf("%d",&c.registered);
            printf("No of Vacancies: ");
            scanf("%d",&c.empty);
            printf("Course Time: ");
            scanf("%s",&c.time);
            send(s, &c,sizeof(struct course), 0);
            printf("\nInserting New Record..\n");
            break;

        case 4: send(s, &choice,sizeof(int), 0);
            printf("Press Tab And Enter After Entering Course Name...\n");
            printf("Please Enter Course Name/ID To Drop: ");
            //scanf("%[^\t]s",&data);
            scanf("%100[^\n]", del);
            //fgets(del,sizeof(del),stdin);
            send(s, del,sizeof(del), 0);
            break;

         default: printf("Please enter valid choice...\n");
        }

    }while(choice!=0);
}

【问题讨论】:

  • fgets() 有什么问题?所有其他选择都是危险的。而且,fflush(stdin) 是严格的未定义行为
  • 不让我输入名字。
  • 这与sockets完全无关。
  • scanf("%[^\n]s",name); 中不应有 s。使用scanf("%100[^\n]", name);scanf("%99[^\n]%*c", name);
  • “不让我输入名字。”是先前未发布的代码的结果。发布足够多的代码,这些代码本身就说明了问题。

标签: c input whitespace


【解决方案1】:

要从字符串中去除空格,循环遍历字符串直到isspace 返回false。您还可以通过使用strlen 跳转到字符串的末尾并使用isspace 向后工作来修剪尾随空格。在此处显示的示例中,我们修剪任何前导或尾随空格,并打印结果。

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

int main(){
    printf("Please enter your name: ");
    fflush(stdout);

    char buf[128];
    fgets(buf, 128, stdin);


    int start = 0;
    // remove any leading whitespace.
    while(isspace(buf[start])) start++;

    int end = strlen(buf);
    // find the first character from the end that is not whitespace
    while(end > start && isspace(buf[end - 1])) end--;

    buf[end] = '\0'; // terminate the string to trim the whitespace at the end.

    char * name = &buf[start];
    printf("Name = '%s'\n", name);
}

【讨论】:

  • 非常感谢您提供此代码 sn-p。它可以很好地删除字符串中的空格。但是,我想删除字符串之间的空格。当我传递结构时,在给出 ID 后,名称出现在新行上。有什么办法可以去掉这样的空白?
  • 在你想读取带空格的字符串的问题中。没有其他地方您说过要删除该字符串中的空格
  • @Akshay C \n 算作空格,因此上述函数还会从您的输入中删除尾随换行符,您说这是服务器的问题。
  • 所以如果你输入“我的名字\n”(因为fgets包含了回车),它会返回“我的名字”
  • @LưuVĩnhPhúc 很抱歉造成混乱。我想我可以通过删除额外的 '\n' 在服务器端解决这个问题
猜你喜欢
  • 1970-01-01
  • 2014-03-25
  • 2015-12-13
  • 2016-08-20
  • 2012-04-05
  • 2017-04-08
  • 1970-01-01
  • 2014-02-13
  • 1970-01-01
相关资源
最近更新 更多