Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:02:54+00:00 2026-05-27T19:02:54+00:00

Feel silly asking this question, since this should be easy, but I can’t figure

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:02:55+00:00Added an answer on May 27, 2026 at 7:02 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel silly asking this, but, I just can't work out how to create
I feel really silly for asking this, but what am I doing wrong with
I feel kind of silly asking this question as it seems really simple, but
I feel silly for not being able to figure this out, but how do
I feel a little silly asking this question, but from everything I've read, this
I feel really silly asking this question but how do you add any data
I feel silly asking this because I should know it but I'm in a
I feel a little bit silly asking this, but I haven't been able to
Sorry for such a trivial question, but I'd feel silly building this if it
I feel very very very silly posting this, but I can't find the answer.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.