繁体   English   中英

如何在C中解析整数行

[英]How to parse a line of integers in C

我想对以下测试文本文件中的每个整数进行非常基础的读取。

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);                                                       
}

代替:

  // 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++;
    }

另一种使用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);
char *p =line;
int len = 0;
while (sscanf(p, "%d%n", &value, &len)==1) {
    printf("%d\t", value);
    current_col++;
    p += len;
}

不必重复在行上使用sscanf ,而必须使用strtok到达字符串的下一个位置,并在字符串的下一个位置使用sscanfatoi

更换

// 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");

暂无
暂无

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

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