简体   繁体   English

如何读取和存储数据,其中包括用户在C中使用** scanf **输入的空格键

[英]How to read and store data which includes space bar that user input using **scanf** in C

im having trouble to input data which includes space bar Using C 我无法输入包含空格键的数据,使用C

Clion (C99) 克里昂(C99)

Inputing name of recipient ,final destination and the status of the package 输入收货人姓名,最终目的地和包裹状态

char name_location_status[90];
char recipient[30];
char final_destination[50];
char status[10];


printf("Please enter , 1> Recipient-, 2> Final Destination- and 3>Delivery status :\n");

scanf("%s", name_location_status);


const char upper[2] = "-";
char *token;
token = strtok(name_location_status, upper);
int i=0;
while( i!=3,token != NULL  )
{

    (i==0) ? strcpy(recipient, token) :
    (i==1) ? strcpy(final_destination, token) :
    strcpy(status, token) ;
    i++;
    token = strtok(NULL, upper);
}

The program works fine if inputing (Rat-House-Arrived) which output (Rat House Arrived) But it wont work if the inputs contain spacebar (L Rat-Kitchen House-Not arrived) which output (L ) 如果输入(Rat-House-Arrived)输出(Rat House Arrived),则程序运行良好,但是如果输入包含空格(L'-Rat-Kitchen House-Not到达),则输出无效(L )

So is there a way that using scanf To input data like this ? 那么有没有办法使用scanf来输入这样的数据?

Blockquote(L Rat-Kitchen House-Not arrived), which is in a line 排成一行的 Blockquote(L Rat-Kitchen House-未到)

If no , can u show me da way ?To input that kind of data In a line 如果否,您能告诉我一种方法吗?要在一行中输入此类数据

instead of scanf use getline() function that seems more appropirate here 而不是scanf使用似乎更合适的getline()函数

char *line = NULL;
getline(&line);

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

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