May I just ask why this piece of code is resulting to a segmentation fault. I’m trying to get input from a text file and I can’t figure out what is the problem.
using namespace std;
using namespace cv;
int main()
{
char str[50];
FILE *trainfile;
int k, n, maxval1, maxval2, classnum;
char dataArray[n][3];
trainfile = fopen("training.txt", "r+");
if(trainfile == NULL){
perror("Cannot open file.\n");
}else{
while(!feof(trainfile)){
fscanf(trainfile, "%s", str);
}
}
fclose(trainfile);
return 0;
}
One problem is that your buffer might not be big enough.
You should get the size of the file first, then make a dynamic buffer of that size, and then finally read the file.