read mix file: fscanf fgets

This is a discussion on read mix file: fscanf fgets within the C Programming forums, part of the General Programming Boards category; Hi there, I am having trivial problem in reading a simple file (see below). This file is not a list ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    read mix file: fscanf fgets

    Hi there, I am having trivial problem in reading a simple file (see below).

    This file is not a list of numbers or list of strings, but is a combination of the two.
    All is fine when I read it line by line and assign the numbers to a certain variable by using fscanf, but when I try to do the same for the strings, I obtain a bus error.

    I hope someone can help;
    thank you in advanced,

    CFD


    My code looks as it follows, and the input file is the next:

    input.txt
    Code:
    problem_type
    problem_name
    24.5
    53.1
    44.0
    Code to read it:
    Code:
    			
    	FILE *file_ID;
            const char *problem[2]; // After reading the file this variable would be something like this:
                                                  // problem[0] = "problem_type"
                                                  // problem[1] = "problem_name"
    	
    	int i,j,k=0;
            float, x,y,z;
    	
    	file_ID = fopen("input.txt", "r");
    	
    	//Read the content of the input file:
    		fgets(problem[0],..4,..file_ID);
    		fgets(problem[1],..4,..file_ID);
    		
    		fscanf(file_ID, "%f\n", &x);
    		fscanf(file_ID, "%f\n", &y);
    		fscanf(file_ID, "%f\n", &z);

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    const char *problem[2];
    
    ...
    
    fgets(problem[0],..4,..file_ID);
    fgets(problem[1],..4,..file_ID);
    Your two pointers don't point anywhere, at least not anywhere valid where you've allocated space for those strings. You'd need to either point those two pointers to an existing buffer of some kind or dynamically allocate some space for them. You could also just use a couple of char arrays to store the data as well.

    A second problem is that the pointers are const which means that you'd not be able to modify the text they point to using them.
    "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. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM

Tags for this Thread


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