在 C 中读取文本文件并将值分配给数组时出错

Errors when Reading text file in C and assigning values to array

我正在尝试用 C 编写代码,这样如果我有这样的文件 reg2.dat:

5.1 3.5 1.4
4.9 3 1.4
4.7 3.2 1.3
4.6 3.1 1.5
5 3.6 1.4

然后,我可以 1) 确定行数(在本例中为 5),2)确定列数(在本例中为 3),3) 将所有 15 个值写入数组 X。

我的代码可以实现前两个目标。但是,我无法让数组 X 包含值 (5.1、3.5、1.4、4.9、3、1.4、4.7、3.2、1.3、4.6、3.1、1.5、5、3.6、1.4)。我也遇到了一些错误。

下面是我的完整有序代码:

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

int getCol(char *myStr);
int getRow(char *fileName);
int assignX(int nCol, int nRow, double *X, char *fileName);

int main(){
   FILE *f;
   char myStr[1000];
   int strL;
   int nCol;
   int nRow;
   char *fileName = "reg2.dat";
   double *X;

   f = fopen(fileName, "r");
   if (f == NULL) perror ("Error opening file");
   else {
     if (fgets(myStr, 1000, f) != NULL )
       puts(myStr);
     fclose(f);
   }

   strL = strlen(myStr);
   nCol = getCol(myStr);
   nRow = getRow(fileName);
   printf("Sample size and number of predictors are %d and %d respectively.\n", nRow, nCol-1);

   X = (double *) malloc(sizeof(double) * (nRow* nCol));

   return 0;
}

不起作用的辅助函数如下...

int assignX(int nCol, int nRow, double *X, char *fileName){
  int i=0;
  int j;
  char *string;
  FILE *f;
  f = fopen(fileName, "r");

  while(!feof(f)){  
    string = fgets(f);
    for (j=0; j<nCol; j++){
       strcpy(X[i], strtok(string, " "));
       i++;
    }  
  }

  for (i=0;i<(nRow*nCol);i++){
    printf("%d\n", X[i]);
  }
}

执行工作的辅助函数如下...

int getCol(char *myStr){
    int length,i,count=0;
    char prev;
    length=strlen(myStr);
    if(length > 0){
      prev = myStr[0];
    }
    for(i=0; i<=length; i++){
      if(myStr[i]==' ' && prev != ' '){
        count++;
      }
      prev = myStr[i];
    }
    if(count > 0 && myStr[i] != ' '){
        count++;
    }
    return count;
}

int getRow(char *fileName){
  char ch;
  int count=0;
  FILE *f;
  f = fopen(fileName, "r");

  while(!feof(f)){
    ch = fgetc(f);
    if(ch == '\n')
    {
      count++;
    }
  }
return count;
}

我已经测试了最后两个辅助函数 getRow 和 getCol 的工作,以及 return nRow = 5 和 nCol = 3 的值。我只是将它们保留在这里以防它可能导致错误(我怀疑)。错误似乎来自第一个辅助函数 assignX。这是我 运行:

时的错误
gcc -ansi -pedantic readReg.c -o readReg -llapack -lblas -lgfortran

错误:

readReg.c: In function ‘assignX’:
readReg.c:52: warning: passing argument 1 of ‘fgets’ from incompatible pointer type
/usr/include/stdio.h:626: note: expected ‘char * __restrict__’ but argument is of type ‘struct FILE *’
readReg.c:52: error: too few arguments to function ‘fgets’
readReg.c:54: error: incompatible type for argument 1 of ‘strcpy’
/usr/include/string.h:128: note: expected ‘char * __restrict__’ but argument is of type ‘double’

感谢您的帮助。

期数

  1. 而不是 char *string; ... while(!feof(f)){ string = fgets(f);,使用 char string[1000]; while(fgets(string, sizeof string, f) != NULL) {。学习者代码很少使用feof()。请改为检查 fgets() return 值。

  2. int getRow(char *fileName) 打开,但不关闭 f。添加 fclose(f).

  3. main() 似乎只读取文件的 1 行。

还有更多,但GTG

还有其他一些问题,但编译器因这一行而失败:

string = fgets(f); 

fgets 不能这样调用,因为有 missing arguments:

char * fgets (char *s, int count, FILE *stream)

编译器抱怨的另一个错误是 assignX 函数中的这一行:

   strcpy(X[i], strtok(string, " "));

您在主程序中有此声明:

double *X;

调用 strcpy 时需要遵循 this 模板:

char * strcpy (char *restrict to, const char *restrict from)

此外,您声明了您的 X to be a pointer not an array,因此尝试访问 X[i] 在任何情况下都会导致任何类型的错误:未定义的行为(如果您处于允许您访问与 X 位于同一块中的内存位置),分段错误和编译时失败。

The array declaration char a[6] requests that space for six characters be set aside, to be known by the name a''. That is, there is a location nameda'' at which six characters can sit. The pointer declaration char *p, on the other hand, requests a place which holds a pointer, to be known by the name ``p''. This pointer can point almost anywhere: to any char, or to any contiguous array of chars, or nowhere