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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:28:10+00:00 2026-05-28T05:28:10+00:00

My Function That writes to the file: Record_t * load_Record(FILE * infile) { Record_t

  • 0

My Function That writes to the file:

Record_t * load_Record(FILE * infile)
{
    Record_t *r;
    char title[TITLE_SIZE];
    char [MEDIUM_SIZE];  
    int ID, rating;
    if ((fscanf(infile,"%d: %s %d", &ID, medium, &rating) != 3 && fgets(title, TITLE_SIZE, infile))){
            return NULL;
    }
    printf("medium is: %s title is: %s\n", medium, title);
    r = create_Record(medium, title);
    set_Record_rating(r, rating);   
    return r;
}

where Record_t is defined as:

typedef struct Record {
    int ID;
    char * title;
    char * medium;
    int rating;
} Record_t;

My main:

#include "Record.h"

#include <stdlib.h>

int main()
{

    char * title = "The Queen";
    char * medium = "DVD";
    FILE * pfile ;
    struct Record *t = create_Record(medium, title);  //creates a record
    struct Record *s;
    set_Record_rating (t, 3);
    print_Record(t);
    pfile = fopen("output.txt", "w");
    save_Record(t, pfile);
    fclose(pfile);
    destroy_Record(t);  //de-allocates memory
    pfile = fopen("output.txt", "r");
    if(!(s = load_Record(pfile))){
        return 1;
    }
    print_Record(s);
    fclose(pfile);
    destroy_Record(s);      
    return 0;
}   

output.txt after being written to file:

1: DVD 3 The Queen    //checked for excess whitespace(has newline however)

Terminal output:

1: The Queen DVD 3
medium is: DVD title is: �  //title being saved inappropriately  
                          @
2: �
    @ DVD 3

now my fgets function is wrong! For some reason, the title is being saved inappropriately

i am compiling with the following flags:
gcc -ansi -std=c89 -pedantic -Wmissing-prototypes -Wall test.c Record.c -o test

where test.c is my main

  • 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-28T05:28:11+00:00Added an answer on May 28, 2026 at 5:28 am
    char * medium;
    

    This should be

    char medium[SOME_CONSTANT]; // or char* medium = malloc(x); if you need the
                                // memory to persist after the function returns
    

    So that you actually have medium pointing to some memory you own. As it is now, your pointer is pointing to garbage and you’re expecting fscanf to save a string in the memory it points to.

    If a function ever appears to return a pointer to some magically created memory, you better check the documentation twice (unless that function happens to be the stupid strdup). The function either actually expects a pointer to some already-allocated memory, or returns a pointer that points to a block of memory allocated with someone from malloc‘s family, in which case you need to take responsibility for deallocating it.

    Only in very rare circumstances do functions return a pointer to memory without taking a preallocated buffer in and without having mallocd it (especially when the string that is returned is of unpredictable size like it is for fscanf).

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

Sidebar

Related Questions

i have a function that writes to a file , how can i using
Here is a function that writes n bits to a binary file. Parameters: Data
I'm currently working on a function that encrypt/decrypts a specific file with a secret
I have a multi-threading Python program, and a utility function, writeLog(message) , that writes
I'm attempting to make a function that writes a string to the last line
I have a Delphi 2006 app that writes to a file and then regularly
Trying to write a couple of functions that will encrypt or decrypt a file
I want to write a function that takes an array of letters as an
I want to write a function that accepts two objects as parameters and compare
I want to write a function that returns the nearest next power of 2

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.