fscanf Issue

This is a discussion on fscanf Issue within the C Programming forums, part of the General Programming Boards category; I am trying to get a function to work that reads the top two strings of the file: Example: Dave, ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    fscanf Issue

    I am trying to get a function to work that reads the top two strings of the file:

    Example:

    Dave,
    Joe
    1
    0
    1

    when i only read in the first line with a single fscanf the program executes perfectly, but when I use a second fscanf the program crashes, outputting Segmentation Fault. I am confused as to why this occurs.

    Code:
    void loadCus()
    {
            char firstName[50];
    	char lastName[50];
    	char part1[100];
    	char part2[100];
    
           ptnew = fopen("customer.dat", "r");
    	
    	printf("Enter Your First Name\n");
    	scanf("%s", firstName);
    	printf("Enter Your Last Name\n");
    	scanf("%s", lastName);		
    	strcat(name, lastName);
    	strcat(name, ", ");
    	strcat(name, firstName);
    	
    	// White Space Override
    	fscanf(ptnew, "%s[^\n]", part1);
    	printf("%s", part1);
    	fscanf(ptnew, "%s[^\n]", part2);
    	printf("%s", part2);
    	printf("Done");
    	fscanf(ptnew, "%d[^\n]", currentCus.food);
    	fscanf(ptnew, "%d[^\n]", currentCus.seatRow);
    	fscanf(ptnew, "%d[^\n]", currentCus.seatCol);
    	
    	fclose(ptnew);	
    }
    BTW, using gcc compiler.

    Any help is appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since you're reading one line at a time anyway, why don't you just use fgets? Then combine it with sscanf. I suspect your problems is that "seatRow" and such aren't pointers, which is what the *scanf functions expect their arguments to be. There are multiple FAQs on getting input from users, try reading up on those.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    2
    Thanks for the help. The missing pointers were the problem. Out of curiosity, if the code is read linearly, why does the execution of the program crash prior to printing printf("%s", part2); if the problematic code is further down?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You never can tell what's going to happen when you start playing around in memory you're not supposed to be playing with.


    Quzah.
    Hope is the first step on the road to disappointment.

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-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fscanf() issue...
    By smoking81 in forum C Programming
    Replies: 12
    Last Post: 09-08-2008, 06:56 AM
  4. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  5. FSCANF format string issue
    By INFERNO2K in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2005, 05:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21