I am trying to write a code in C so that if I have a file reg2.dat as such:
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
Then, I can 1) determine the number of rows (in this case 5), 2) determine the number of columns (in this case 3), 3) write all 15 values in an array X.
I have my code working for the first two goals. However, I cannot get the array X to contain the values (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). I am getting a few errors as well.
Below is my complete and ordered code:
#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;
}
The helper function that does not work follows...
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]);
}
}
The helper functions that do work follows...
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;
}
I have tested that the last two helper functions getRow and getCol work, and return values of nRow = 5 and nCol = 3. I am only keeping them in here in case it could possibly cause the error (which I doubt). The errors appear to come from the first helper function assignX. Here are the errors when I run:
gcc -ansi -pedantic readReg.c -o readReg -llapack -lblas -lgfortran
Errors:
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’
Thank you for your help.