So I have written some code which reads a file line by line, breaking each line into characters and storing into a multidimensional array. However the program freezes and crashes at lines 31-34. When debugging the code runs successfully through the for loop once, then fails at 32 the second time round. I have an idea that it could be to do with fgets() requiring buf to be 'char *' but is changed by strcpy() to 'const char *'. Could someone help me out here, I am not sure what to do or if I am overlooking something. Thanks
James
//Setup variables
FILE *InputFile; //Input file stream variable
FILE *OutputFile; //Output file stream variable
char buf[256]; //Buffer
char InputFileBuf[][256] = {};
int InputFileLineCounter = 0;
int LineCount = 0;
InputFile = fopen((InputFileTxt->GetValue()).mb_str(), "r"); //Open the input file with read access
OutputFile = fopen(((OutputFileTxt->GetValue()).mb_str()), "w"); //Open te Output file with write access
//Work out the number of lines in the file
while (!feof(InputFile)) {
if (fgets(buf, 100, InputFile) != NULL) {
InputFileLineCounter++;
}
}
//Set the internal file pointer back to the start of the file
rewind(InputFile);
// //Read each line and save it to
// for (LineCount = 0; LineCount < InputFileLineCounter; LineCount++) {
// fgets(buf, 100, InputFile); //Get a string from the input file storing it in buffer
// fputs(buf, OutputFile); //send the string in buffer to the output file
//
// }
//Read each line and store in InputFileBuf
for (LineCount = 0; LineCount < InputFileLineCounter; LineCount++) {
fgets(buf, 100, InputFile); //Get a string from the input file storing it in buffer
strcpy(InputFileBuf[LineCount], buf); //Copy the line into file buffer
}
//close the files
fclose(InputFile);
fclose(OutputFile);