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
This should be
So that you actually have
mediumpointing to some memory you own. As it is now, your pointer is pointing to garbage and you’re expectingfscanfto 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 frommalloc‘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 forfscanf).