我想对以下测试文本文件中的每个整数进行非常基本的读取。
1 2 3 4 5
6 7 8 9 10
但在正确打印第一行后,以下代码无限打印1。
FILE* f;
uint32_t current_row = 0;
uint32_t current_col = 0;
char line[1024];
int value; // issues with printing uint32_t right now
// Open the file
f = fopen("blah.txt", "r");
if (f == NULL) {
fprintf(stderr, "file could not be opened.\r\n");
exit(ERROR_FILEPATH);
}
// Read in each row
while (NULL != fgets(line, sizeof(line), f)) {
printf("%d: %s\r\n", current_row, line);
// Read in each integer in the current row
while (sscanf(line, "%d", &value) {
printf("%d\t", value);
// TODO: How to read a single integer in at a time?
current_col++;
}
current_col = 0;
current_row++;
}
// Close the file
if (fclose(f)) {
fprintf(stderr, "file could not be closed.\r\n");
exit(ERROR_FILECLOSE);
}
答案 0 :(得分:2)
而不是:
// Read in each integer in the current row
while (sscanf(line, "%d", &value) {
printf("%d\t", value);
// TODO: How to read a single integer in at a time?
current_col++;
}
就个人而言,我并不是“扫描”的粉丝。函数系列:
// Read in each integer in the current row
char *cp = line;
while(*cp && *cp != '\n')
{
/* skip leading white space. */
while(isspace(*cp))
++cp;
/* Read the next number. */
value = strtol(cp, &cp, 10);
printf("%d\t", value);
current_col++;
}
答案 1 :(得分:1)
使用strtol()
的另一种解决方案,但使用它的正确方法是检查其返回值和errno
:
const char *end = line + strlen(line);
char *endp = NULL;
const char *p = line;
while (p < end) {
errno = 0;
long value = strtol(p, &endp, 10);
if (errno && (value == 0 || value == LONG_MIN || value == LONG_MAX)) {
break;
}
p = endp;
printf("%ld\t", value);
current_col++;
}
}
另外,如果您尝试打印C99样式的固定宽度整数:
#include <stdint.h>
#include <inttypes.h>
uint32_t foo = 42;
printf("%" PRIu32 "\n", foo);
答案 2 :(得分:1)
char *p =line;
int len = 0;
while (sscanf(p, "%d%n", &value, &len)==1) {
printf("%d\t", value);
current_col++;
p += len;
}
答案 3 :(得分:0)
您必须使用sscanf
来到字符串中的下一个位置,并使用strtok
或sscanf
,而不是在行上重复使用atoi
字符串中的下一个位置。
替换
// Read in each integer in the current row
while (sscanf(line, "%d", &value) {
printf("%d\t", value);
// TODO: How to read a single integer in at a time?
current_col++;
}
通过
// Read in each integer in the current row
if (sscanf(line, "%d", &value))
{
current_col++;
nextToken = strtok(line, " ");
while (nextToken != NULL && sscanf(nextToken, "%d", &value))
{
current_col++;
printf("%d\t", value);
nextToken = strtok(NULL, " ");
}
}
printf("\n");