Help with scanning in strings

This is a discussion on Help with scanning in strings within the C Programming forums, part of the General Programming Boards category; Hey guys im having some trouble scanning in a few strings from a text file. Here is a sample of ...

  1. #1
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49

    Help with scanning in strings

    Hey guys im having some trouble scanning in a few strings from a text file.

    Here is a sample of the text file:
    Command number1
    Command2 number2 number3
    Command3 number4 number5
    Command4 number6

    Here is my code for this part of the program
    Code:
    fscanf(data,"%s%s%s",&inCommand, &inOperand1, &inOperand2); 
    
    printf( "%s %s %s \n",inCommand, inOperand1, inOperand2);
    How do I make it skip to the next line when it reaches EOL.
    So it scans in Command, number1 , and EOL on the first time around. And if the command has two numbers scan in both on the second time around.

    Thanks
    Chris

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    How about something like this?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       const char filename[] = "test.txt";
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          char line [ 100 ];
          while ( fgets(line, sizeof line, file) )
          {
             char a [ 3 ] [ 32 ];
             int items;
             fputs(line, stdout);
             items = sscanf(line, "%31s%31s%31s", a[0], a[1], a[2]);
             if ( items >= 2 )
             {
                int i;
                for ( i = 0; i < items; ++i )
                {
                   printf("a[%d] = \"%s\"\n", i, a[i]);
                }
             }
          }
       }
       return 0;
    }
    
    /* my output
    Command number1
    a[0] = "Command"
    a[1] = "number1"
    Command2 number2 number3
    a[0] = "Command2"
    a[1] = "number2"
    a[2] = "number3"
    Command3 number4 number5
    a[0] = "Command3"
    a[1] = "number4"
    a[2] = "number5"
    Command4 number6
    a[0] = "Command4"
    a[1] = "number6"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    take a look at how scanf functions work again. You dont' need the "address of" operator. in C a string constant's quotaion marks acts as a pointer to the strings address. implicity stating this in your scanf's argument is bad practice. possibly more.

  4. #4
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    Thanks for the quick reply.

    I dont want to print the commands and numbers, those where there just as a test. Sorry for not making that clar. I want scan in the which command it is and then send the numbers to a function via a switch depending on the command.

    Thanks
    Chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. Scanning Strings till no white space is found?
    By HomerJ in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2002, 03:02 PM
  5. Scanning characters & strings
    By rain_e in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 01:43 AM

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