Like Tree2Likes
  • 2 Post By jimblumberg

This program with fscanf keeps crashing

This is a discussion on This program with fscanf keeps crashing within the C Programming forums, part of the General Programming Boards category; So this program I have to make keeps spitting out an error report when I try and run it. I ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    1

    This program with fscanf keeps crashing

    So this program I have to make keeps spitting out an error report when
    I try and run it. I am using eclipse C kepler.

    The file it is supposed to read has the following text;

    R1 N001 N003 20
    R2 N002 N001 5
    R3 N001 0 10
    R4 N002 N003 10
    R5 N003 N000 5
    I1 0 N002 10
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,755
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Comp{
    	char *component;
    	char *node_1;
    	char *node_2;
    	float value;
    }ComponentType;
    
    
    int main(){
    
    	int index = 5;
    	ComponentType *rm;
    	rm = malloc(index * sizeof(ComponentType));
    
    
    	FILE *fPtr;
    
    	fPtr = fopen("H:\\mydata.dat", "r");
    
    	if(fPtr == NULL){
    		printf("file not found");
    	}
    
    	else{
    		rewind(fPtr);
    		while(!feof(fPtr)){
    
    			fscanf(fPtr, "%s %s %s %f", rm->component, rm->node_1, rm->node_2, &rm->value );
    			printf("%s%s%s%f", rm->component, rm->node_1, rm->node_2, rm->value );
    
    			rm++;
    		}
    
    	}
    		fclose(fPtr);
    
    
    	return 0;
    }
    Where are you allocating memory for all those pointers in your structure?

    Also in future please post your code inside code tags in your post instead of attaching the file.

    Jim
    vart and stahta01 like this.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why is this program crashing.
    By juice in forum C Programming
    Replies: 4
    Last Post: 12-21-2011, 08:38 AM
  2. Program keeps crashing
    By Charak in forum C Programming
    Replies: 5
    Last Post: 03-23-2011, 07:07 AM
  3. Program crashing
    By bijan311 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2010, 08:55 AM
  4. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  5. Program Crashing - please help!
    By Surfin_Bird in forum C Programming
    Replies: 6
    Last Post: 03-23-2005, 10:34 AM

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