read in from file.

This is a discussion on read in from file. within the C Programming forums, part of the General Programming Boards category; question. I'm trying to read in a line from a file in the form January 5 1 4 5 7 ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    19

    read in from file.

    question. I'm trying to read in a line from a file in the form

    January 5 1 4 5 7 14
    April 7 4 55 67 54 23 8 99

    the above means that the first part is a string and the first number tells how much numbers comes after it. So january has 5 numbers that comes after it and april has 7 numbers that comes after it.

    My question is that being that the amount of data per line will be different, how would i go about coding something like this.

    I was thinking of using fgets to get the entire line but i need to address each individual and then assign them to something inside my program?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Once you read the first number, you know how many times to loop to read the rest.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You could use fscanf() to skip over the month (well store it, but as far as reading the other information is concerned the month name is of little consequence) use strtol() to get the number of arguments, then use strtol() to get the others. Use all the parameters of strtol().

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You could...
    Code:
    scanf() in a string, 
    scanf() in a number into a variable called "count"
    
    loop "count" times { 
        scanf() in a number ; 
    }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Why can't this be done

    while(action[i]!= " ")
    {
    action1[i]=action[i];
    i++;
    }

    with i incrimented each time the loop executes.


    What i did was use fgets to read in entire line then im trying to break it up
    Last edited by sumdude; 10-15-2008 at 08:58 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because the index of action1 needs to start over from 0 all the time -- every time you hit a space -- while i needs to keep on keepin' on to get through action. Plus then you have to convert strings into numbers.

    Edit: Oh and it would need to be ' ', which is a vastly different creature from " ". But as you mentioned, sscanf will be your bestest friend, if you will let it.
    Last edited by tabstop; 10-15-2008 at 09:34 PM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Is this where sscanf comes in?

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    fscanf's input is a file. sscanf's is a string in your program. Both functions take same types formatting strings to define how to interpret numbers or characters from the input.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Question here concerning below. I use fgets to read in an entire line from a file in the format: " text 5 1 2 33 34 52" with the first number telling how many number follows it. However how do i go about working with the a number greater than 10.

    for(x=0; x<num_events; x++)
    {
    fgets(action,1024,open);


    sscanf(action,"%s ", action1);

    originallinesize=strlen(action);
    wordsize=strlen(action1);

    for(i=wordsize+1;i<originallinesize;i++)
    {
    if( isdigit(action[i])>1)
    {
    printf("%c\n", action[i]);
    }

    else
    {
    // when it finds the space seperating the numbers it prints below.
    printf("no sir\n");
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I believe this
    Code:
    if( isdigit(action[i])>1)
    is not what you want. (I would not expect isdigit to ever return something bigger than 1, although it is theoretically possible.) You probably just want isdigit by itself.

    Anyway, if you feel the need to not use
    Code:
    sscanf(action+wordsize, "%d", &first_number);
    but want to read in character by character, you can do so: as you read in a digit, multiply what you have by 10 and add in the value of the new digit.

    And excitingly, you can use %n to find out how many characters were read by sscanf, so that you can work out the new position (as in my example above).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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