簡體   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