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 8956689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:50:30+00:00 2026-06-15T14:50:30+00:00

I am trying to read from the while questions.txt which has the following format:

  • 0

I am trying to read from the while questions.txt which has the following format:

Question\n
Difficulty:Cost:Prize\n
Correct Answer\n
Answer 1\n
.
.
.
Answer i\n
\n

The following is a function that reads from questions.txt and stores it to a temporary node to be added to a linked list.

pQuestionType loadQuestions(pQuestionType pFirst)
{
    pQuestionType pTemp = malloc(sizeof(pQuestionType));
    FILE* pFile;
    string sFilename, sTemp;
    char cDump;
    int nTemp, n=0;
    gotoxy(0,0);
    system("cls");
    printf("Enter file name: ");
    gets(sFilename);
    pFile = fopen(sFilename, "rt");
    while(!feof(pFile))
    {
        printf("Iteration \n");
        fgets(sTemp, 255, pFile);
        strcpy(pTemp->sQuestion, sTemp);
        pTemp->sQuestion[strlen(sTemp)-1] = '\0';
        printf("Add question success\n");

        fscanf(pFile, "%d%c", &nTemp, &cDump);
        pTemp->nDifficulty = nTemp;
        printf("Add difficulty success\n");

        fscanf(pFile, "%d%c", &nTemp, &cDump);
        pTemp->nCost = nTemp;
        printf("Add cost success\n");

        fscanf(pFile, "%d%c", &nTemp, &cDump);
        pTemp->nWinnings = nTemp;
        printf("Add winnings success\n");

        fgets(sTemp, 255, pFile);
        strcpy(pTemp->sCorrect, sTemp);
        pTemp->sCorrect[strlen(sTemp)-1] = '\0';
        strcpy(pTemp->sAnswers[0], sTemp);
        pTemp->sAnswers[0][strlen(sTemp)-1] = '\0';
        printf("Add answer success\n");

        for(n=1; n<10; n++)
        {
            fgets(sTemp, 255, pFile);
            if(*sTemp == '\n')break;
            strcpy(pTemp->sAnswers[n], sTemp);
            pTemp->sAnswers[n][strlen(sTemp)-1] = '\0'; 
        }
        printf("Add choices success\n");

        printf("\n");   
        pFirst = addQuestion(pFirst, pTemp);        
        if(*sTemp == EOF) break;
    }
    fclose(pFile);

    return pFirst;
}

I crash at the third iteration when the program tries to read the difficulty of my third question. What causes this and what can I do to fix my code?

  • 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-06-15T14:50:30+00:00Added an answer on June 15, 2026 at 2:50 pm
    pQuestionType pTemp = malloc(sizeof(pQuestionType));
    

    If pQuestionType is a pointer type (and it appears to be since neither pFirst nor pTemp have a * character anywhere near their declaration), then its size is going to be the size of a pointer (currently likely to be 4 or 8).

    So this malloc will almost certainly not give you enough memory. You need to use sizeof(*pTemp) instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following up from an earlier question I asked about, I'm trying to read data
I'm trying to read the POST data from a request with Transfer-Encoding: chunked while
I'm trying to read from a file called quiz_questions.txt in my res/raw folder. The
I am trying to read from a file and encrypt the data using AES
I am trying to read from file: The file is multiline and basically i
I am trying to read from a file on the local machine using Ajax.The
I am using Delphi and trying to read from a bar code scanner over
I have an XML file that I'm trying to read from here , and
I'm trying to read friendlist from Facebook and show in TableView but I haven't
I'm trying to read data from an xml file and display it in a

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.