Thread: fscanf---segmentation fault?

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    linked lists maybe???

    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.

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Post some code plz.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    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.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    easier this way

    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.

  5. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    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

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    28
    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.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    help

    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.

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    uh...

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-29-2008, 12:11 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM