why would calling fscanf return a segmentation fault???
i also got an error that said "bus error"...i've never seen that before, can anyone explain?
why would calling fscanf return a segmentation fault???
i also got an error that said "bus error"...i've never seen that before, can anyone explain?
Last edited by breanne; 08-23-2002 at 02:23 AM.
eat, drink and be merry. for tomorrow: we party.
Post some code plz.
Try not.
Do or do not.
There is no try.
- Master Yoda
A 'Segmentation Fault' is caused when you try to access memory not in your program's address spaces. The most common cause for this with a fscanf() is if you haven't checked for the success of fopen(). The other reason maybe you have not allocated memory to some pointer variables and are passing these variables to this function. Will be better in debugging if you could post some code.
i think that it would be easier if i stated my desired outcome, cause im turning into a real grouch trying to figure out these bugs.
I have an array of students:
struct student
{
char name[25];
char address[80];
int yearofbirth;
char telno[15];
}
i need to be able to read and write the array to the file, but i am having trouble with storing the variables in the file and then reading them. getting the file open is fine.
im not even sure how i should store the array in the file...
...*pulling hairs*
eat, drink and be merry. for tomorrow: we party.
Here's a quick example on how to accomplish this:
Code:#include <stdio.h> #include <string.h> typedef struct student { char name[25]; char address[80]; int yearofbirth; char telno[15]; }STUDENT; int main() { int i; FILE *fp; STUDENT write[2], read[2]; fp = fopen("test.txt", "wb"); if(fp == NULL) { printf("error\n"); return 0; } strcpy(write[0].name, "John"); strcpy(write[0].address, "123 Fake Street"); write[0].yearofbirth = 1952; strcpy(write[0].telno, "555-1234"); strcpy(write[1].name, "Ken"); strcpy(write[1].address, "456 Fake Street"); write[1].yearofbirth = 1970; strcpy(write[1].telno, "555-4321"); fwrite(write, sizeof(STUDENT), 2, fp); fclose(fp); fp = fopen("test.txt", "rb"); if(fp == NULL) { printf("error\n"); return 0; } fread(read, sizeof(STUDENT), 2, fp); fclose(fp); for(i = 0; i < 2; i++) { printf("%s\n", read[i].name); printf("%s\n", read[i].address); printf("%d\n", read[i].yearofbirth); printf("%s\n\n", read[i].telno); } return 0; }
Try not.
Do or do not.
There is no try.
- Master Yoda
the complication arises when i try to split the main function into two separate functions (readfile and writefile)...but those are minor bugs....i've just gotta format the user interface better....
...here's the hard part: now, storing the students as a linked list in the file....your code actually gave me a lot of help, thanks so much!!!
*hair re-growing*
eat, drink and be merry. for tomorrow: we party.
okay, can someone tell me why my code doesn't write more than one record to the file??? it'll write one okay, but if i want more than one, then it chokes and prints a whole bunch of crap to the file.
and im also having loads of trouble with linked lists...can someone help me out there too???
eat, drink and be merry. for tomorrow: we party.
sorry, no need to download:
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #include "student.h" void readfile(struct student[], int*, char[]); void writefile(struct student[], int, char[]); int main() { struct student ics212[5]; int numstudents; int index; char fname[15]; printf("Please enter the number of students: "); scanf("%d", &numstudents); /*printf("The number of students is: %d\n", numstudents);*/ index = 0; while(index<numstudents) { gets(ics212[index].name); printf("Please enter the student's name:\n"); gets(ics212[index].name); printf("Please enter the student's address (followed by *): \n"); gets(ics212[index].address); printf("Please enter the student's yearofbirth: \n"); scanf("%d", &(ics212[index].yearofbirth)); printf("Please enter the student's phone number: \n"); gets(ics212[index].telno); gets(ics212[index].telno); index++; } printf("Please enter the name of the file that you would "); printf("like to write to: "); scanf("%s", fname); readfile(ics212, &numstudents, fname); writefile(ics212, numstudents, fname); return -1; } void readfile(struct student sarray[], int *nums, char fname[]) { FILE *fp; int index; fp=fopen(fname, "r"); if((!fp)||(fp==NULL)) { printf("readfile: File not opened.\n"); } else { fread(sarray, sizeof(struct student), 5, fp); for(index=0; index<(*nums); index++) { printf("%s\n", sarray[index].name); printf("%s\n", sarray[index].address); printf("%d\n", (sarray[index].yearofbirth)); printf("%s\n", sarray[index].telno); } }/*close else*/ fclose(fp); } void writefile(struct student sarray[], int nums, char fname[]) { FILE *fp; int index; struct student thisarray[5]; fp=fopen(fname, "w"); for(index=0; index<nums; index++) { strcpy(thisarray[index].name, sarray[index].name); strcpy(thisarray[index].address, sarray[index].address); thisarray[index].yearofbirth=sarray[index].yearofbirth; strcpy(thisarray[index].telno, sarray[index].telno); } fwrite(thisarray, sizeof(struct student), 5, fp); fclose(fp); }
eat, drink and be merry. for tomorrow: we party.