I'm a beginner in C and I have a problem with my code.
My C code isn't running and I can't understand why, and where is my fault. In my exercise the structs are given in this way.
Asked to read the data from the file (.txt) and to store them into the linked structs.
The exercise is much more but I want to practice and I try to do a part of it.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char *title;
char *description;
int idNumber;
char *tvProducer;
} EmmissionType;
struct TVNodeStruct {
EmmissionType *anEmmission;
struct TVNodeStruct *next;
};
typedef struct TVNodeStruct *TVNodeType;
//This is the file: dokimi.txt
//<4><Program 1><First program><John F. Doe>
//<5><Program 3><Third program><Kostantinos Panagiotou>
int main(void)
{
int i;
FILE *fp;
TVNodeType ptr,head;
fp = fopen("dokimi.txt", "r");
//head =(TVNodeType) malloc(sizeof(TVNodeType));
head= NULL;
ptr = (TVNodeType) malloc(sizeof(TVNodeType));
for(i=1; i<3; ++i)
{
fscanf(fp, "<%d>" , &(ptr->anEmmission->idNumber));
printf("%d\n", ptr->anEmmission->idNumber);
fscanf(fp,"<%[^>]>", ptr->anEmmission->title);
printf("%s\n", ptr->anEmmission->title);
fscanf(fp,"<%[^>]>", ptr->anEmmission->description);
printf("%s\n", ptr->anEmmission->description);
fscanf(fp,"<%[^>]>", ptr->anEmmission->tvProducer);
printf("%s\n", ptr->anEmmission->tvProducer);
ptr->next=head;
head=ptr;}
return(0);
}
Aucun commentaire:
Enregistrer un commentaire