Thread: read mix file: fscanf fgets

  1. #1
    Registered User
    Join Date
    Mar 2009

    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,


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

    Code to read it:
    	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:
    		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
    Northern Virginia/Washington DC Metropolitan Area
    const char *problem[2];
    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, 12:44 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. archive format
    By Nor in forum A Brief History of
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

Tags for this Thread

Website Security Test