After trying to read/write binaries failed miserably I tried to do the same thing with fscanf and fprintf and it seemed to work fine, but again .. reading doesn't work at all and once again I can't really tell why:
struct + pointer:
typedef struct flug
{
int flugnummer;
char flugziel[50];
enum TAG flugtag;
int flugzeit_stunde;
int flugzeit_minute;
int gateway;
char status[10];
struct flug *next;
}FLUG;
typedef FLUG *ELEM_ZGR;
global variables:
enum TAG {
Sonntag,
Montag,
Dienstag,
Mittwoch,
Donnerstag,
Freitag,
Samstag
};
static ELEM_ZGR first;
char const datei[] = "ddslist.txt";
write:
int fluege_sichern() {
ELEM_ZGR curr;
FILE *fp;
curr = first;
if (fopen_s(&fp, datei,"a+") != 0)
{
printf("\nDatei %s nicht zum Anhaengen zu oeffnen",datei);
PAUSE;
exit(1);
}
while (curr != NULL) {
fprintf(fp,"%d %s %d %d %d ",
curr->flugnummer, curr->flugziel, curr->flugzeit_stunde, curr->flugzeit_minute, curr->gateway);
fclose(fp);
curr = curr->next;
}
}
read:
void fluege_laden() {
ELEM_ZGR curr;
FILE *fp;
int i = 0;
curr = NULL;
if (fopen_s(&fp, datei,"r") != 0)
{
printf("\nDatei %s nicht zum Lesen zu oeffnen",datei);
PAUSE;
exit(1);
}
printf("\n\nArtikelliste\nArtikelnummer Artikelbezeichnung Artikelpreis");
while (fscanf_s(fp,"%d %s %d %d %d ",
&curr->flugnummer, curr->flugziel, &curr->flugzeit_stunde, &curr->flugzeit_minute, &curr->gateway) != EOF)
{
printf("\n%d %s %d %d %d ",
curr->flugnummer, curr->flugziel, curr->flugzeit_stunde, curr->flugzeit_minute, curr->gateway);
if (++i%10==0)
PAUSE;
}
fclose(fp);
}
Please help me out here. Even a little hint would be a huge help. [EDIT1]
void fluege_laden() {
ELEM_ZGR curr;
FILE *fp;
int i = 0;
curr = (ELEM_ZGR)malloc(sizeof(struct flug));
if (fopen_s(&fp, datei,"r") != 0)
{
printf("\nDatei %s nicht zum Lesen zu oeffnen",datei);
PAUSE;
exit(1);
}
printf("\n\nArtikelliste\nArtikelnummer Artikelbezeichnung Artikelpreis");
while (fscanf_s(fp,"%d %s %d %d %d ",
&curr->flugnummer, curr->flugziel, &curr->flugzeit_stunde, &curr->flugzeit_minute, &curr->gateway) != EOF)
{
printf("\n%d %s %d %d %d ",
curr->flugnummer, curr->flugziel, curr->flugzeit_stunde, curr->flugzeit_minute, curr->gateway);
if (++i%10==0)
PAUSE;
}
fclose(fp);
}
Look:
void fluege_laden() {
ELEM_ZGR curr;
FILE *fp;
int i = 0;
curr = NULL; /* NULL! */
When loading, curr
is NULL
. You cannot write to a NULL
pointer, that's undefined behavior. You need to allocate memory.
Also, consider not including the pointer in the typedef
, it's hard to understand and remember that ELEM_ZGR
is a pointer. Pointer semantics are important in C, it's best to make it clear to all readers of the code what's going on.
In your read function curr
is a typedef to a FLUG *
, and you initialized it as NULL
:
curr = NULL;
Then you try to access deference it in your fscanf
:
while (fscanf_s(fp,"%d %s %d %d %d ", &curr->flugnummer,...
That's no good. If you have a pointer and you want to store something to it, you need to give it some memory:
curr = malloc(sizeof(FLUG));
Then you should be able to use it. Once you're done you'll need to call free(curr)
to release that memory as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.