fscanf with variable number of entries

This is a discussion on fscanf with variable number of entries within the C Programming forums, part of the General Programming Boards category; Hi there, I am using fscanf to read the entries of a file in a sequential way, and in each ...

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

    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:

    Code:
    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
    CFD

    Code:
    #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");
    	else{
    		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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    I'd read a line at a time using fgets(), then tokenize this line (not with strtok() ) to extract the fields.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    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
    Posts
    476
    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
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    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
    Posts
    114
    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, 01:56 PM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 04:33 AM
  3. Replies: 10
    Last Post: 09-27-2005, 01:49 PM
  4. Variable number of args
    By cboard_member in forum C Programming
    Replies: 2
    Last Post: 09-02-2005, 07:18 AM
  5. Functions with a variable number of arguments
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2002, 02:12 AM

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