用C中的空格分割每行 [英] Spliting each line by spaces in C

查看:55
本文介绍了用C中的空格分割每行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其行格式为:

I have a file with lines of the format:

<string><spaces><string><spaces><string>

我不知道每个字符串之间的空格数.我想解析该行并将每个字符串插入一个变量(第一个字符串代表一个名称,第二个姓氏和第三个id).我看到我可以使用 strtok ,但是我不想使用它,而是使用循环遍历该行的循环.

I don't know the number of spaces between each string. I would like to parse the line and insert each string into a variable (the first string represents a name, the second last name, and the third id). I saw that I can use strtok but I prefer not to use it, rather a loop which iterates over the line.

我还发现我可以使用:

if(fscanf(party_data,"%s %s %s",name,last,id) != 3){
   break;
}

但是我认为使用while循环会更好.while循环的问题是我不知道每个字符串之间的空格数量.我的目标是创建一个函数 parseLine ,该函数获取三个指针(名称,姓氏和ID)并解析该行.该函数应如何显示?

but I think a while loop is better. The problem with the while loop is the fact I don't know the amount of spaces between each string. My goal is to create a function parseLine which gets three pointers (name, last and id) and parses the line. How should the function look like?

推荐答案

格式为 scanf (或其堂兄,例如 fscanf sscanf vfscanf 等)可以匹配输入中任意数量的空格(不仅包括空格,还包括制表符,垂直制表符和换行符),因此您的 fscanf 调用现在可能还不错.哦,除了一个细节:您通常希望避免进行%s 的裸转换,而应使用类似以下内容的方法:

A single space character in a format string for scanf (or its cousins such as fscanf, sscanf, vfscanf, and so on) can match an arbitrary amount of white space in the input (including not just spaces, but also tabs, vertical tabs, and new-lines), so your fscanf call is probably fine as it stands now. Oh, except for one detail: you generally want to avoid a bare %s conversion, and use something like:

char dest[16];
scanf("%15s", dest);

也就是说,您总是要指定最大大小,该大小应该比要提供的缓冲区的大小小一个.

That is, you always want to specify the maximum size, which should be one smaller than the size of buffer you're supplying.

如果您不想使用 scanf 和公司,则有两种选择.您可以以 strspn strcspn 开头,也可以仅使用带有 isspace 的while循环:

If you don't want to use scanf and company, you have a couple of choices. You could start with strspn and strcspn, or you could just use while loops with isspace:

char *line = /* whatever*/;

while (!isspace(*line))
   *first++ = *line++;
*first = '\0';

while (isspace(*line))
    ++line;

while (*isspace(*line))
    *second++ = *line++;
*second = '\0';

while (isspace(*line))
    ++line;

while (*isspace(*line))
    *third++ = *line++;
*third = '\0';

在实际使用中,您还希望跟踪目标缓冲区的长度,并且仅将其实际可容纳的数量复制到该缓冲区中(或者计算出每种需要的大小,并进行相应分配).

In real use, you'd also want to keep track of the length of the destination buffer, and only copy as much into it as it can actually hold (or else figure up the size each needs, and allocate accordingly).

哦,还有一个次要的细节:当调用 isspace 时,您应该将其操作数强制转换为 unsigned char .如果不进行强制转换,则将其用于某些非英语字符(例如带有重音符号,反引号等)可能会产生不确定的行为.

Oh, and one other minor detail: when you call isspace, you should really cast its operand to unsigned char. Without casting, using it for some non-English characters (e.g., with accents, unlauts, etc.) can give undefined behavior.

这篇关于用C中的空格分割每行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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