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);

I am re-posting the code but with it set up to work as a consol app and there is also the txt file attached.

James

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

int main ()
{
    //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("myfile.txt", "r");    //Open the input file with read access
    OutputFile = fopen("output.txt", "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);

    printf("Done");
}

Alright so I have changed the code a little which works as planned for a file with a maximum of 3 lines in it. Can someone please help me get it to work for more than 3 lines,

Thanks
James

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

int main ()
{
    //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 LineCounter = 0;


    InputFile = fopen("myfile.txt", "r");    //Open the input file with read access
    OutputFile = fopen("output.txt", "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++;
        }
    }

    printf("Line count is: %d\n\n", InputFileLineCounter);
    //Set the internal file pointer back to the start of the file
    rewind(InputFile);

    //Read each line and store in InputFileBuf
    for (LineCounter = 0; LineCounter < InputFileLineCounter ; ++LineCounter) {
       fgets(InputFileBuf[LineCounter - 1], 100, InputFile);
       printf("Line %d: %s", LineCounter, InputFileBuf[LineCounter - 1]);
    }

    //close the files
    fclose(InputFile);
    fclose(OutputFile);

    printf("\n\nDone");

After playing around with the code, I found that if InputFileBuffer has its row and column size set there is no problems. So here is the code adjusted (it does not allow files that are greater than 256Kb);

James

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

//This is the max length of a line in the input file
#define LineLength 256

int main ()
{
    //Setup variables
    FILE *InputFile;   //Input file stream variable
    FILE *OutputFile;  //Output file stream variable
    char buf[256];     //Buffer for operations
    int InputFileLineCount = 0;   //Stores the total number of lines in the input file
    int LineCounter = 0;    //Give the current line number - 1, when used in a loop
    long InputFileByteCount;    //Stores the total number of bytes in the input file

    InputFile = fopen("myfile.txt", "r");    //Open the input file with read access
    OutputFile = fopen("output.txt", "w");    //Open te Output file with write access

    //Check if the file exist
    if (InputFile == NULL) {
        puts("There was an error opening the file");
        return 1;
    }

    //Get the size in Bytes of the file
    fseek(InputFile , 0 , SEEK_END);
    InputFileByteCount = ftell(InputFile);
    rewind(InputFile);

    //Make sure file is not too big!
    if (InputFileByteCount > 256 * 1024) { //File should not be greater than 256Kb
        puts("File is greater than 256Kb");
        return 2;
    }

    //Work out the number of lines in the file
    while (!feof(InputFile)) {
        if (fgets(buf, 100, InputFile) != NULL) {
            InputFileLineCount++;
        }
    }

    //Set the internal file pointer back to the start of the file
    rewind(InputFile);

    //Initialise the multidimensional array to store lines from file
    char InputFileBuf[InputFileLineCount][LineLength + 1];

    //Read each line and store in InputFileBuf
    for (LineCounter = 0; LineCounter < InputFileLineCount; LineCounter++) {
        if (fgets(InputFileBuf[LineCounter], 100, InputFile) != NULL) {
            printf("%3.2f%% Complete\r", (((float)LineCounter + 1)/(float)InputFileLineCount)*100);
        }
    }

    //Send data from file array to output file
    for (LineCounter = 0; LineCounter < InputFileLineCount; LineCounter++) {
        fputs(InputFileBuf[LineCounter], OutputFile);
    }

    //close the files
    fclose(InputFile);
    fclose(OutputFile);

    //Output some info to user
    printf("\nFile size is %ld bytes long\nWhich is %ld KBytes long", InputFileByteCount, InputFileByteCount/1024);
    printf("\n\nDone\n\n");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.