我想知道是否有任何方法可以使用 fscanf 或 fgets 函数忽略空格。我的文本文件每行有两个字符,可能会也可能不会用空格分隔。我需要读取这两个字符并将每个字符放入不同的数组中。
file = fopen(argv[1], "r");
if ((file = fopen(argv[1], "r")) == NULL) {
printf("\nError opening file");
}
while (fscanf(file, "%s", str) != EOF) {
printf("\n%s", str);
vertexArray[i].label = str[0];
dirc[i] = str[1];
i += 1;
}
使用空格 (" "
fscanf 格式中的 ) 会导致它读取并丢弃输入上的空格,直到找到非空格字符,并将输入上的非空格字符保留为下一个要读取的字符。所以你可以做这样的事情:
fscanf(file, " "); // skip whitespace
getc(file); // get the non-whitespace character
fscanf(file, " "); // skip whitespace
getc(file); // get the non-whitespace character
or
fscanf(file, " %c %c", &char1, &char2); // read 2 non-whitespace characters, skipping any whitespace before each
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)