将一串输入存储到C中的结构中

时间:2022-01-06 11:12:10

was wondering how I would be able to store a user inputted string in the format "string,character,integer,integer" into a struct. For example storing "apple,c,5,10" into

我想知道如何将用户输入的字符串以“字符串,字符,整数,整数”格式存储到结构中。例如,将“apple,c,5,10”存储到

typedef struct {
char item[80];
char letter;
int x,y;
}information;

information apple;

I am trying to avoid going through using scanf and a long piece of code to make the comma into a delimiter so wondering if there was any other way to quickly read from scanf and chucking this information into the struct

我试图避免使用scanf和一长段代码使逗号成为分隔符,因此想知道是否有任何其他方法可以快速读取scanf并将此信息放入结构中

3 个解决方案

#1


4  

You can specify complex formats using scanf, like:

您可以使用scanf指定复杂格式,例如:

scanf("%79[^,],%c,%d,%d", apple.item, &apple.letter, &apple.x, &apple.y);

%79[^,] means scan anything that is not a comma character, up to 79 characters.

%79 [^,]表示扫描任何非逗号字符的内容,最多79个字符。

Note that this does no error handling if the user enters a poorly formatted string, like "aaa;b;1;2". For that, you'll need to write a lot more code. See strtok

请注意,如果用户输入格式不正确的字符串,例如“aaa; b; 1; 2”,则不会进行错误处理。为此,您需要编写更多代码。见strtok

#2


1  

You can use multiple format specifiers in the format string to scanf() to scan all the input at once, through a comma-seperated user input, like

您可以在格式字符串中使用多个格式说明符scanf()通过逗号分隔的用户输入一次扫描所有输入,如

int ret = -1;
if ((ret = scanf("%79[^,],%c,%d,%d", apple.item, &apple.letter, &apple.x, &apple.y)) != 4)
                       //always check the return value of scanf()                       
{
   printf("scanf() failed\n");
   //do something to avoid usage of the member variables of "apple"
} 

However, I'll recommend the long way, like

但是,我会推荐很长的路,比如

  • read the line using fgets()
  • 使用fgets()读取行

  • tokenize using strtok() and , as delimiter
  • 使用strtok()和,作为分隔符进行标记

  • use the token (or convert using strtol(), as required).
  • 使用令牌(或根据需要使用strtol()转换)。

Much safe and robust.

非常安全和强大。

#3


0  

Try to read using the func read() then just split the string using strtok()

尝试使用func read()读取然后使用strtok()拆分字符串

Here's some references :

这里有一些参考:

strtok : http://man7.org/linux/man-pages/man3/strtok.3.html

strtok:http://man7.org/linux/man-pages/man3/strtok.3.html

read : http://linux.die.net/man/2/read

阅读:http://linux.die.net/man/2/read

#1


4  

You can specify complex formats using scanf, like:

您可以使用scanf指定复杂格式,例如:

scanf("%79[^,],%c,%d,%d", apple.item, &apple.letter, &apple.x, &apple.y);

%79[^,] means scan anything that is not a comma character, up to 79 characters.

%79 [^,]表示扫描任何非逗号字符的内容,最多79个字符。

Note that this does no error handling if the user enters a poorly formatted string, like "aaa;b;1;2". For that, you'll need to write a lot more code. See strtok

请注意,如果用户输入格式不正确的字符串,例如“aaa; b; 1; 2”,则不会进行错误处理。为此,您需要编写更多代码。见strtok

#2


1  

You can use multiple format specifiers in the format string to scanf() to scan all the input at once, through a comma-seperated user input, like

您可以在格式字符串中使用多个格式说明符scanf()通过逗号分隔的用户输入一次扫描所有输入,如

int ret = -1;
if ((ret = scanf("%79[^,],%c,%d,%d", apple.item, &apple.letter, &apple.x, &apple.y)) != 4)
                       //always check the return value of scanf()                       
{
   printf("scanf() failed\n");
   //do something to avoid usage of the member variables of "apple"
} 

However, I'll recommend the long way, like

但是,我会推荐很长的路,比如

  • read the line using fgets()
  • 使用fgets()读取行

  • tokenize using strtok() and , as delimiter
  • 使用strtok()和,作为分隔符进行标记

  • use the token (or convert using strtol(), as required).
  • 使用令牌(或根据需要使用strtol()转换)。

Much safe and robust.

非常安全和强大。

#3


0  

Try to read using the func read() then just split the string using strtok()

尝试使用func read()读取然后使用strtok()拆分字符串

Here's some references :

这里有一些参考:

strtok : http://man7.org/linux/man-pages/man3/strtok.3.html

strtok:http://man7.org/linux/man-pages/man3/strtok.3.html

read : http://linux.die.net/man/2/read

阅读:http://linux.die.net/man/2/read

相关文章