Thread: fscanf with variable number of entries

  1. #1
    Registered User
    Join Date
    Mar 2009

    fscanf with variable number of entries

    Hi there,

    I am using fscanf to read the entries of a file in a sequential way, and in each line of the file there is a variable number of entries;

    imagine a 1-line file made in the following way:

    problem:  prob_name prob_type ...
    where the "..." indicate that the following entries may not exist.

    My original code that can read this line with a fixed/predefined number of entries is posted below, but I'd like some help in modifying it in such a way that it reads only the existing entries

    I hope someone can help. It is for reading an input file full of a code.

    thank you in advance

    #define PROB_ENTRIES 6
    char *problem[PROB_ENTRIES];
    //Allocation of the array of pointers to chars
    for(i=0; i<PROB_ENTRIES; i++)
          problem[i] = (char*) malloc(32 * sizeof(char));
    	FILE *file_ID;
    	if((file_ID = fopen("INPUT_FILE.txt", "r")) == NULL )
    		printf(" The input file could not be open\n");
    		fscanf(file_ID, "%s %s %s %s\n", header, problem[0], problem[1], problem[2]);
           //Where the problem[1] and problem[2] may not exist in the file
    I though of something containing a WHILE, but I don't know how how to read the number of entries before reading them by using fscanf in this way.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    I'd read a line at a time using fgets(), then tokenize this line (not with strtok() ) to extract the fields.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  3. #3
    Registered User
    Join Date
    Mar 2009
    Hi, thanks for replying; I used fgets before together with strtok(), but you say to not use strtok; what should I use instead?

    thank you again

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Use lex [/irony]

    Hand-code it. Hand-coding is often simple but tedious. Scanf is fine, but not for this purpose. I would also suggest a function getline for larger lines if you don't want them to be truncated.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Use strtok() if it does what you need, which sounds like the case. There's nothing inherently bad about strtok() the way there is with, say, gets().

  6. #6
    Registered User
    Join Date
    Mar 2009
    Thank you both for the suggestions; I will go ahead and work on it in that way

    all the best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  4. Variable number of args
    By cboard_member in forum C Programming
    Replies: 2
    Last Post: 09-02-2005, 06:18 AM
  5. Functions with a variable number of arguments
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2002, 01:12 AM
Website Security Test