Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中向双数组添加值_C_Arrays_Pointers_Io - Fatal编程技术网

如何在C中向双数组添加值

如何在C中向双数组添加值,c,arrays,pointers,io,C,Arrays,Pointers,Io,我之前写过一个类似的问题。然而,我有一个错误,我现在无法解决 我正在尝试用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在双数组X中写入所有15个值 我的代码为前两个目标工作。但是,我无法使双数组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。我在

我之前写过一个类似的问题。然而,我有一个错误,我现在无法解决

我正在尝试用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在双数组X中写入所有15个值

我的代码为前两个目标工作。但是,我无法使双数组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));
   assignX(int nCol, int nRow, double *X, char *fileName);
   return 0;
}
错误:

readReg.c: In function ‘main’:
readReg.c:40: error: expected expression before ‘int’
readReg.c: In function ‘assignX’:
readReg.c:55: error: incompatible type for argument 1 of ‘strcpy’
/usr/include/string.h:128: note: expected ‘char * __restrict__’ but argument is of type ‘double’
您是否尝试使用atof而不是strcpy将char*值转换为double值

我的意思是改变这个 strcpyX[i],strtokstring

对此 X[i]=atofstrtokstring

这里有一个例子

更新:

这里使用函数AssignX,文件reg2.dat的最后一行必须有一个回车符\n,如果最后一行没有回车符,程序将崩溃,因为行号将是4而不是5。我建议您检测该情况以避免程序崩溃:D

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

  while(fgets(string, sizeof(string), f) != NULL){
    data = strtok(string, " ");
    for (j=0; NULL != data && j<nCol; j++){
        if (data[strlen(data) - 1] == '\n')
            data[strlen(data) - 1] = '\0';
        X[i] = atof(data);
        data = strtok(NULL, " ");
       i++;
    } 
  }  

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

希望这有帮助,正如编译器告诉您的,这一行是错误的:

strcpy(X[i], strtok(string, " "));
就语法而言,strcpy的第一个参数必须是char*。X[i]不是字符*

就功能而言,这不是从字符串中提取double的方式。您需要使用atof或sscanf

下面是一个更新的循环,应该可以工作:

// Declare this at the start of the function.
char* token;

...

while(fgets(string, sizeof string, f) != NULL)
{
   // Use string to start the call to strtok.
   token = strtok(string, " ");

   // Even though each row should have nCol, be safe
   // and make sure that you don't process token if
   // it is NULL.
   for ( j = 0; j < nCol && token != NULL; j++)
   {
      X[i] = atof(token);
      i++;

      // For subsequent calls to strtok, use NULL
      // for the first argument.
      token = strtok(NULL, " ");
   }  
}

您可以这样做,这将从文件中生成一个双精度数组:

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

  while(fscanf(f, "%lf", &(X[i])) != EOF) 
       i++; 
  }

strcpy不起作用,因为X[i]是一个双精度字符,而不是strcpy所期望的字符*。我建议看看strtod,特别是它的第二个参数。谢谢。那你建议我怎么做?我将不得不使用矩阵乘法和这个双数组X。assignX在哪里被调用?我没有在上面看到它。如果你打算做矩阵乘法,那么我会说你想把数组保持为双精度,而不是转换成字符串格式。谢谢@BinaryJudy。我在年添加了这一行,并将其删除以进行调试,尽管我认为这会引入另一个错误。我不确定要做多少工作,如保持数组为双精度,不转换为字符串格式。我在I/O方面非常糟糕,不确定是否有一种简单的方法来重做这些:另一种方法是使用并扫描文件中的值。@CrApHeR。非常感谢。只是一个简单的问题。当我做你做的和我也做的时,我注意到我只从每行中获得第一个标记并重复三次。相反,我想记录每行的所有三个标记。换句话说,我想使用我在你帮助我做的事情周围的行中设置的j索引。特别是这些行:whilefetsstring、sizeof string、f!=空{表示j=0;j@CrApHer.我只希望输出中的前三个字符是5.1 3.5 1.4,而不是现在的5.1 5.1 5.1是,这是因为您没有将已复制数据的偏移量应用于字符串。让我编辑我的答案,以便更好地解释。
strcpy(X[i], strtok(string, " "));
// Declare this at the start of the function.
char* token;

...

while(fgets(string, sizeof string, f) != NULL)
{
   // Use string to start the call to strtok.
   token = strtok(string, " ");

   // Even though each row should have nCol, be safe
   // and make sure that you don't process token if
   // it is NULL.
   for ( j = 0; j < nCol && token != NULL; j++)
   {
      X[i] = atof(token);
      i++;

      // For subsequent calls to strtok, use NULL
      // for the first argument.
      token = strtok(NULL, " ");
   }  
}
  int assignX(double *X, char *fileName){
  int i=0;
  FILE *f;
  f = fopen(fileName, "r");

  while(fscanf(f, "%lf", &(X[i])) != EOF) 
       i++; 
  }