繁体   English   中英

将整数文本文件解析为数组

[英]Parsing a text file of integers into an array

我正在使用strtok()解析每个整数并将其放入int nums[1000] 该文件将始终遵循以下格式:

第一行是数组的数字,每个数字用空格分隔。 在线的号码不会超过 10 个。 第一行之后文件中没有任何内容。

printf("Starting program\n");
char file_name[100];
strcpy(file_name, args[1]);
char number[100];
strcpy(number, args[2]);
FILE *fp;
fp = fopen(file_name, "r");
if (fp == NULL) {
    printf("Error opening file\n");
}
char int_array[1000];//
int nums[1000]; //storing the integers without spaces
int i = 0; //for indexing the array to save the integers
while (fgets(int_array, 1000, fp) != NULL) {
    printf("%s\n", "test");
    puts(int_array); // prints out `1 2 3 4 5...`
    char *token;
    token = strtok(int_array, " ");
    nums[i] = atoi(token);
    while (token != NULL) {
        token = strtok(NULL, " ");
        nums[i] = atoi(token);
        //puts(token); Token gets printed out correctly.
    }
}

printf("%i\n", nums[i]); // this gives segmentation fault
printf(nums) // also gives seg fault

我不明白为什么我会出现段错误。

有你的代码的多个问题,主要的问题是你测试token扫描下一个标记之前

while (token != NULL) {
    token = strtok(NULL, " ");
    nums[i] = atoi(token);
}

你应该这样做:

while ((token = strtok(NULL, " ")) != NULL) {
    nums[i] = atoi(token);
}

其他事宜:

  • 在访问strcpy(file_name, args[1]); argv[1]之前,您不检查argc > 1 ,可能会调用未定义的行为。
  • 您将文件名复制到一个 100 字节的数组中:如果命令行参数超过 99 字节,则会导致缓冲区溢出。 您不需要复制参数,只需将argv[1]传递给fopen或使用指针: char *filename = argv[1];
  • 您检查fopen()失败,但不退出函数... fgets()对于空流指针具有未定义的行为。
  • 即使第一次调用strtok()也可以返回NULL 在将返回值传递给atoi()之前,请务必检查它。
  • 你不检查i变得太大。 如果i达到1000您应该停止读取输入文件。
  • nums数组传递给printf是不正确的: printf(nums)甚至不应该编译,或者至少生成有意义的警告。

请注意,您根本不需要使用strtok() strtol()可以解析一个数字并更新一个指针以指向该数字。

以下是如何使用它:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
    printf("Starting program\n");
    if (argc < 2) {
        fprintf(stderr, "missing command line argument\n");
        return 1;
    }
    FILE *fp = fopen(argv[1], "r");
    if (fp == NULL) {
        fprintf(stderr, "cannot open %s: %s\n", argv[1], strerror(errno));
        return 1;
    }
    char buf[1000];
    int nums[1000];
    int i = 0; 

    while (fgets(buf, sizeof buf, fp)) {
        char *p = buf;
        char *q;
        for (; i < 1000; i++) {
            nums[i] = strtol(p, &q, 0);
            if (q == p) {
                /* no more numbers */
                break;
            }
            p = q;
        }
    }
    fclose(fp);
    for (int j = 0; j < i; j++) {
        printf("%d ", nums[j]);
    }
    printf("\n");
    return 0;
}

您没有检查argc并取消引用args[1]args[2]

你从不使用number

尝试这个:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main( int argc, char * argv[] ) {
   printf("Starting program\n");
   if( argc < 2 ) {
      return 1;
   }
   FILE * fp = fopen( argv[1], "r");
   if( fp == NULL ) {
      perror( argv[1] );
      return 1;
   }
   char int_array[1000];
   if( fgets( int_array, 1000, fp )) {
      int nums[1000];
      int    i = 0; 
      char * token = strtok(int_array, " ");
      while( token ) {
         nums[i++] = atoi(token);
         token = strtok(NULL, " ");
      }
      printf("0: %i\n", nums[0]);
      printf("1: %i\n", nums[1]);
      printf("%d: %i\n", i-1, nums[i-1]);
   }
   return 0;
}

执行:

aubin@Breizh-Atao ~/Dev/C $ gcc parsing.c -o parsing
aubin@Breizh-Atao ~/Dev/C $ echo 1 2 3 4 5 6 7 8 9 10 11 >parsing.txt
aubin@Breizh-Atao ~/Dev/C $ ./parsing parsing.txt 
Starting program
0: 1
1: 2
10: 11
aubin@Breizh-Atao ~/Dev/C $ 

暂无
暂无

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

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