Need help with input from text file 
Author Message
 Need help with input from text file

Hi, I have a program that reads its commands and arguments from a text
file. The commands call functions in my program that accept the arguments. The
text file is formatted like this:

c_n_e: 123456789      300
r_s: 34       999999999
r_s:          888888888
.
.
.

where c_n_e accepts a long and an int and r_s accepts two longs. The commands
do not exceed 6 characters. My program to test my input routine looks like this:

#include <stdio.h>

int main()
{
   char option[7];          /*to read in the command*/
   int i,j,seats,sum,x=0;   /*seats stores the 2nd arg of c_n_e*/
   long EVNT,ssn;           /*EVNT stores 1st arg of c_n_e or r_
                                  ssn stores 2nd arg of r_s*/
   FILE *fp;
   fp=fopen("data.txt","r");
   do{
      sum=0;
      for(j=0;j<7;j++)
         option[j]=NULL;
      fscanf(fp,"%s", option);
      for(i=1;i<7;i++)                    /*Here I read in the command as a
         switch(option[i]){                 string. I process the string by
            case 'c':                       using assigned values for each
               sum+=1;                      letter. The sum will decide
               break;                       how I will scan in the arguments
            case 'n':                       and later on which function to
               sum+=2;                      call*/
               break;
            case 'e':
               sum+=3;
               break;
            case 'r':
               sum+=4;
               break;
            case 's':
               sum+=5;
               break;
         }
      if(sum==5){
         fscanf(fp,"%ld%d",&EVNT,&seats);       /*This is for c_n_e*/
         printf("%ld\n", EVNT);
         printf("%d\n", seats);
      }
      if(sum==9){
         fscanf(fp,"%ld%ld", &EVNT,&ssn);      /*This is for r_s*/
         printf("%ld\n",EVNT);
         printf("%ld\n",ssn);
      }
      x++;
   }while(x<5);
  return 0;

Quote:
}

My problem is dealing with cases like:
    r_s:               888888888
where my input routine expects 2 arguments but where the text
file sends only 1 argument. I know my routine will read the 888888888 as the
first argument although it is suppose to be the second and read in something
strange for the second argument. Can anyone suggest a better way of getting the
input from the text file or how I should change my routine.
Thanks for any help.

kristopher m. huynh
http://www.*-*-*.com/ ~axs03425/



Wed, 25 Mar 1998 03:00:00 GMT  
 Need help with input from text file


[snip of most of question]

Quote:
>My problem is dealing with cases like:
>    r_s:               888888888
>where my input routine expects 2 arguments but where the text
>file sends only 1 argument. I know my routine will read the 888888888 as the
>first argument although it is suppose to be the second and read in something
>strange for the second argument. Can anyone suggest a better way of getting the
>input from the text file or how I should change my routine.

The following is only one of many approaches, but most will rely on
using fgets() and strtok().  You should retrieve the FAQ from
rtfm.mit.edu, which includes information relevant to this and many other
problems.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* suppose we set aside some space: */
char    input_buffer[256],  /* space for 255 char input line */
        *input_tag,     /* ptr to whatever comes before : */
        *input_arg[2];  /* ptrs to args */
long    x;

/* then you can read the data lines from your input FILE *fp: */
if (!fgets(input_buffer, sizeof input_buffer, fp)) {
    /* handle EOF or input error here */
    }

/* now you can use strtok() */
input_tag = strtok(input_buffer," :");
input_arg[0] = strtok(NULL," \n");
input_arg[1] = strtok(NULL," \n");
/* no error checking is done above.  You can add it if you want */

You can do things like:

if (input_arg[1])   /* with only 1 arg, input_arg[1] will be NULL */
    x = atol(input_arg[1]);
--

* Chicago, IL (USA)    



Thu, 26 Mar 1998 03:00:00 GMT  
 Need help with input from text file

Quote:
>Hi, I have a program that reads its commands and arguments from a text
>file. The commands call functions in my program that accept the arguments. The
>text file is formatted like this:

<SNIP>

Quote:
>      for(j=0;j<7;j++)
>         option[j]=NULL;
>      fscanf(fp,"%s", option);
>      for(i=1;i<7;i++)                    /*Here I read in the command as a
>My problem is dealing with cases like:
>    r_s:               888888888
>where my input routine expects 2 arguments but where the text
>file sends only 1 argument. I know my routine will read the 888888888 as the
>first argument although it is suppose to be the second and read in something
>strange for the second argument. Can anyone suggest a better way of getting the
>input from the text file or how I should change my routine.

For this, and other, reasons.  most text books I've read reccomend
using gets() to read in a line then parsing it with sscanf or strtok.
This should help.


Fri, 27 Mar 1998 03:00:00 GMT  
 Need help with input from text file

Quote:


>>Hi, I have a program that reads its commands and arguments from a text
>>file. The commands call functions in my program that accept the arguments.
The
>>text file is formatted like this:

><SNIP>
>>      for(j=0;j<7;j++)
>>         option[j]=NULL;
>>      fscanf(fp,"%s", option);
>>      for(i=1;i<7;i++)                    /*Here I read in the command as a

>>My problem is dealing with cases like:
>>    r_s:               888888888
>>where my input routine expects 2 arguments but where the text
>>file sends only 1 argument. I know my routine will read the 888888888 as the
>>first argument although it is suppose to be the second and read in something
>>strange for the second argument. Can anyone suggest a better way of getting
the
>>input from the text file or how I should change my routine.

>For this, and other, reasons.  most text books I've read reccomend
>using gets() to read in a line then parsing it with sscanf or strtok.
>This should help.

The c.l.c faq and many (most?) of the regular posters recommend fgets
instead of gets, since it provides a means to avoid overrunning the
bounds of your input array.  The faq is available by ftp from rtfm.mit.edu
/pub/usenet/comp.lang.c.

--
John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:



Fri, 27 Mar 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Need help on save texts format by Textout to a text file

2. Need help with input data from file

3. Need help with linked list & text files

4. Need help with linked list & text files

5. Help--need really fast search to compare two HUGE files of text records

6. Help: Need a function to read Nth line of text file

7. Please help: need to read lines in text file

8. Need help on how to save to a text file

9. Help needed with TEXT FILES!!!!!

10. Need help on how to save to a text file

11. Help: Need to display a large text file

12. Need help parsing flat text file and with CString

 

 
Powered by phpBB® Forum Software