alice smith
alice smith

Reputation: 67

read a text file into a struct

is this the correct way to read a file into a struct? i ve tried doing test prints but nothing prints out? when i run and compile the program, there's no errors or set faults.

`int main(){
FILE* f;
linkedList* list = NULL;
house* house;

f = fopen("house.txt", "r");

list = createLinkedList();
if (f == NULL)
{
    printf("Error: File could not be opened.\n");
}  
while (!EOF)
{
    fscanf(f, "%d", &house->price);
    fscanf(f, "%d", &house->bed);
    fscanf(f, "%d", &house->bath);

    insertStart(list, character);
    printf("%d test data %d %d", house->bed, house->price, house->bath);
}
fclose(f);
freeLinkedList(list);
return 0;`

Upvotes: 1

Views: 46

Answers (1)

Yaroslav Stetsyk
Yaroslav Stetsyk

Reputation: 160

You declared house* house but didn't create actual object. Your pointer is dangling, so most likely your program is writing to and printing from undefined memory.

Upvotes: 1

Related Questions