jn1kk
jn1kk

Reputation: 5102

Load File Into Char* Array

Feel silly asking this question, since this should be easy, but I can't figure out whats wrong.

void loadIniIntoMemory() {

    FILE *fp ;
    fp = fopen (iniFile, "r");
    int ch;
    int final_line_num = 0;
    int char_index;

    char* current_line = (char*) malloc(sizeof(char) * MAX_INI_LINE_LENGTH);

    while((ch = fgetc(fp)) != EOF) {

        if(ch == 10) {

            // new line
            *(current_line + char_index) = '\0';
            char_index = 0;
            iniFileData[final_line_num] =  current_line;
            final_line_num++;

        } else {

            // regular char
            *(current_line + char_index) = ch; // CAN'T DO THIS, CRASH
            char_index++;

            if(ch == 13) {
                // carriage return
                continue;
            }

        }

    }

}

Been a little while since I did C, it crashes at this line : *(current_line + char_index) = ch;

Thanks for any help.

--EDIT--

Also, no one noticed, that this code doesn't save the last line. Here is the full, correct, working code which saves a file into an array of pointers.

void loadIniIntoMemory() {

    FILE *fp ;
    fp = fopen (iniFile, "r");
    int ch;
    final_line_num = 0;
    int char_index = 0;

    char* current_line = (char*) malloc(sizeof(char) * MAX_INI_LINE_LENGTH);

    while((ch = fgetc(fp)) != EOF) {

        if(ch == '\n') {

            // new line
            *(current_line + char_index) = '\0';
            char_index = 0;
            iniFileData[final_line_num] = current_line;
            final_line_num++;
            current_line = (char*) malloc(sizeof(char) * MAX_INI_LINE_LENGTH);

        } else if(ch != '\r') {

            // regular char
            *(current_line + char_index) = ch;
            char_index++;

        }

    }

    iniFileData[final_line_num] = current_line;

    fclose(fp);

}

Upvotes: 1

Views: 1911

Answers (4)

Mike Nakis
Mike Nakis

Reputation: 61969

Besides what others have already pointed out, you will also have to move the line buffer allocation call into the loop and immediately after the final_line_num++; statement. Otherwise, each new line you read will be overwriting the previous line.

Upvotes: 2

And some operating systems, like most POSIX compliant ones, in particular Linux, gives you the ability to map a file segment (possibly the entire file) into virtual memory. On Linux, you could consider using the mmap system call

Upvotes: 0

user7116
user7116

Reputation: 64068

For starters, you don't initialize char_index, meaning it will likely have garbage in it. If you don't initialize it, your program will add some unknown number to the current_line pointer.

int char_index = 0; /* initialize to 0 */

Secondly, a bit more "natural" syntax would be:

current_line[char_index] = ...

Thirdly, you can test the characters without using their integer equivalents:

if (ch == '\n') {
/* this is the same as "ch == 10" */

Fourth, you should close the open file prior to leaving the routine:

fclose(fp);

Finally, I'm not sure what the ch == 13 ('\r') and continue is meant to handle, since the continue is effectively a no-op, but you probably don't want to copy it into the data:

if (ch != '\r') {
    current_line[char_index] = ch;
    char_index++;
    /* or on one line: current_line[char_index++] = ch; */
}

As an aside, a powerful feature of C (and many other languages) is the switch statement:

/* substitutes your if...elseif...else */
switch (ch) {
case '\n':
    current_line[char_index] = '\0';
    char_index = 0;
    iniFileData[final_line_num++] =  current_line;
    break; /* <-- very important, C allows switch cases to fall thru */

case '\r':
    /* do nothing */
    break;

default:
    /* any character that is not a newline or linefeed */
    current_line[char_index++] = ch;
    break;
}

Upvotes: 3

Fred
Fred

Reputation: 17085

You didn't initialize char_index. I suppose you want to initialize it to 0. In C, uninitialized variable will contain garbage. Most likely your char_index equals some very large number.

Upvotes: 2

Related Questions