繁体   English   中英

C编程:将文本文件的行转换为整数数组

[英]C programming: lines of text file to integer array

我想将input.txt文件更改为整数数组。 但是可悲的是,每当遇到换行符时,我总是会丢失一个整数。

以下是我的main()

int main(int args, char* argv[]) {
  int *val;

  char *STRING = readFile();

  val = convert(STRING);

  return 0;
}

以下是我的文件输入功能

char *readFile() {
    int count;
    FILE *fp;

    fp = fopen("input.txt", "r");
    if(fp==NULL) printf("File is NULL!n");

    char* STRING;
    char oneLine[255];
    STRING = (char*)malloc(255);
    assert(STRING!=NULL);

    while(1){
        fgets(oneLine, 255, fp);
        count += strlen(oneLine);
        STRING = (char*)realloc(STRING, count+1);
        strcat(STRING, oneLine);
        if(feof(fp)) break;
    }
    fclose(fp);
    return STRING;
}

以下是我的整数数组函数

int *convert(char *STRING){
    int *intarr;
    intarr = (int*)malloc(sizeof(int)*16);
    int a=0;
    char *ptr = strtok(STRING, " ");


    while (ptr != NULL){
        intarr[a] = atoi(ptr);

        printf("number = %s\tindex = %d\n", ptr, a);
        a++;
        ptr = strtok(NULL, " ");
    }

    return intarr;
}

有很多问题。

这是您程序的正确版本,所有注释均为我的。 为简便起见,进行了最小限度的错误检查。 intarr = malloc(sizeof(int) * 16); 如果文件中的数字超过16个,将是一个问题,应该以某种方式进行处理,例如,通过使用realloc intarr ,这与在readFile执行的操作类似。

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

char *readFile() {
  FILE *fp;

  fp = fopen("input.txt", "r");
  if (fp == NULL)
  {
    printf("File is NULL!n");
    return NULL;      // abort if file could not be opened
  }

#define MAXLINELENGTH 255      // define a constant rather than hardcoding "255" at several places

  char* STRING;
  char oneLine[MAXLINELENGTH];
  STRING = malloc(MAXLINELENGTH);
  int count = MAXLINELENGTH;   // count mus be initialized and better declare it here
  assert(STRING != NULL);
  STRING[0] = 0;          // memory pointed by STRING must be initialized

  while (fgets(oneLine, MAXLINELENGTH, fp) != NULL)   // correct usage of fgets
  {
    count += strlen(oneLine);
    STRING = realloc(STRING, count + 1);
    strcat(STRING, oneLine);
  }

  fclose(fp);
  return STRING;
}


int *convert(char *STRING, int *nbofvalues) {   // nbofvalues for returning the number of values
  int *intarr;
  intarr = malloc(sizeof(int) * 16);
  int a = 0;
  char *ptr = strtok(STRING, " \n");   // strings may be separated by '\n', or ' '

  *nbofvalues = 0;

  while (ptr != NULL) {
    intarr[a] = atoi(ptr);

    printf("number = %s\tindex = %d\n", ptr, a);
    a++;
    ptr = strtok(NULL, " \n");  // strings are separated by '\n' or ' '
   }                            // read the fgets documentation which
                                // terminates read strings by \n

  *nbofvalues = a;    // return number of values 
  return intarr;
}


int main(int args, char* argv[]) {
  int *val;

  char *STRING = readFile();

  if (STRING == NULL)
  {
    printf("readFile() problem\n");   // abort if file could not be read
    return 1;
  }

  int nbvalues;
  val = convert(STRING, &nbvalues);  // nbvalues contains the number of values

  // print numbers
  for (int i = 0; i < nbvalues; i++)
  {
    printf("%d: %d\n", i, val[i]);
  }

  free(val);    // free memory
  free(STRING); // free memory
  return 0;
}

我不确定您的要求是什么,但这可以简化很多,因为不需要将文件读入内存,然后将字符串转换为数字。 您可以在阅读数字时即时转换数字。 正如评论中已经提到的那样,为每行调用realloc效率很低。 还有更多改进的空间。

暂无
暂无

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

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