Bad input, from a text file. 
Author Message
 Bad input, from a text file.

===================================================================

QUESTION:

        I want to read integer values in from a text file,
  from variable length lines, and push them onto an array
  of linked lists, one for each new line in the input file,
  --without locking up my machine if I encounter bad input.

  "Correct" input might look like this:

 33 44 56
 1 2 3 4 5 77 88 99
 3 4
 2 33 44

 I could read this data with something like:

 while(fscanf(inFile,"%d", &x) != EOF ){
    /* push */
 }

 but this code will crash if the input file contains anything
 other than ascii numbers.

 The following code (or something like it) will work, (and it won't
 crash on any non-numeric data that may have worked its way into
 the input file). But it only works if I know how many numbers will
 appear in each line, in advance. For instance, if I knew in
 advance that each line contained exactly 4 numbers:

 char line[80];

 while(fgets(line, 80, inFile) != EOF){
  sscanf(line,"%d %d %d %d", a,b,c,c);
  /* push */
 }
 if(!feof(inFile)) fprintf(stderr,"Bad input data!");

    What I would like to do, is to either exit gracefully, or,
 perhaps, substitute a blank or a zero in place of a stray character,
 if I encounter something like:

 1 2 33
 33 x5 2
 22 3 5 78
 2 3
 11 22 33

 How do I do it? :-)
/*=======================================================================*/

/*=======================================================================*/



Sat, 12 Aug 1995 11:45:02 GMT  
 Bad input, from a text file.
: ===================================================================
:  
: QUESTION:
:  
:       I want to read integer values in from a text file,
:   from variable length lines, and push them onto an array
:   of linked lists, one for each new line in the input file,
:   --without locking up my machine if I encounter bad input.
:  
:   "Correct" input might look like this:
:  
:  33 44 56
:  1 2 3 4 5 77 88 99
:  3 4
:  2 33 44
:  
:  I could read this data with something like:
:  
:  while(fscanf(inFile,"%d", &x) != EOF ){
:     /* push */
:  }
:  
:  but this code will crash if the input file contains anything
:  other than ascii numbers.
:  
:  
:  The following code (or something like it) will work, (and it won't
:  crash on any non-numeric data that may have worked its way into
:  the input file). But it only works if I know how many numbers will
:  appear in each line, in advance. For instance, if I knew in
:  advance that each line contained exactly 4 numbers:
:  
:  char line[80];
:  
:  while(fgets(line, 80, inFile) != EOF){
:   sscanf(line,"%d %d %d %d", a,b,c,c);
:   /* push */
:  }
:  if(!feof(inFile)) fprintf(stderr,"Bad input data!");
:  
:     What I would like to do, is to either exit gracefully, or,
:  perhaps, substitute a blank or a zero in place of a stray character,
:  if I encounter something like:
:  
:  1 2 33
:  33 x5 2
:  22 3 5 78
:  2 3
:  11 22 33
:  
:  How do I do it? :-)
: /*=======================================================================*/

: /*=======================================================================*/

The list management is up to you :-)

#include <stdio.h>
#include <string.h>
main()
{
        FILE *inFile;

        char line[80];
        char *atoken;
        char *tokens[80]; /* Tokens for this line */
        int ntokens;

        if (!(inFile = fopen("infile","r"))) {
                perror("Cannot open input file");
                exit(-1);
        }

        while (fgets(line, 80, inFile) != NULL) { /* NOT EOF */

                strip_newline(line);

                /* first token */

                atoken = strtok(line, " ");
                ntokens = 0;
                while (atoken != NULL) {
                        tokens[ntokens] = (char *)malloc(strlen(atoken+1));
                        tokens[ntokens] = atoken;
                        printf("Token number %d Value %s\n",ntokens,atoken);
                        ntokens++;
                        atoken = strtok(NULL," ");
                }
                printf("\n",atoken);
        }

Quote:
}

strip_newline(char *buffer)
{
        while (*buffer != '\n') {
                buffer++;
        }
        *buffer = '\0';
Quote:
}      



Sat, 12 Aug 1995 21:43:08 GMT  
 Bad input, from a text file.

Quote:
>===================================================================

>QUESTION:

>    I want to read integer values in from a text file,
>  from variable length lines, and push them onto an array
>  of linked lists, one for each new line in the input file,
>  --without locking up my machine if I encounter bad input.

Look at function atoi.  You may also want isdigit.

---

Eager Consulting                (415) 325-8077
1960 Park Boulevard, Palo Alto, CA 94306-1141



Tue, 15 Aug 1995 02:03:28 GMT  
 Bad input, from a text file.

Quote:
>    I want to read integer values in from a text file,
>   from variable length lines, and push them onto an array
>   of linked lists, one for each new line in the input file,
>   --without locking up my machine if I encounter bad input.
>  The following code (or something like it) will work, (and it won't
>  crash on any non-numeric data that may have worked its way into
>  the input file). But it only works if I know how many numbers will
>  appear in each line, in advance. For instance, if I knew in
>  advance that each line contained exactly 4 numbers:

[ ... deleted ... ]

Quote:
>     What I would like to do, is to either exit gracefully, or,
>  perhaps, substitute a blank or a zero in place of a stray character,
>  if I encounter something like:

>  1 2 33
>  33 x5 2

Well, let's see... if you are willing to lose the 5 because of the x,
and get a variable # of input items per line, you could say:

char szBuf[ MAXLINE ];
FILE *pfIn = fopen("somefile","r");

while (fgets(szBuf, sizeof(szBuf), pfIn) {
        int     iVal;
        char    *pcTok = strtok(szBuf, " ");

        while (pcTok) {
                if (sscanf(pcTok,"%d%*s",&iVal)==1)
                        process(iVal);  /* do something with input */
                pcTok = strtok(0, " ");
        }

Quote:
}

fclose(pfIn);

Let's look at my sscanf format string for a moment: %d%*s

Get a base 10 integer, then try to get a string but don't put it anywhere.
This handles the case of 5x, where the 'x' matches the %*s, so sscanf
returns 2 and you know you had a malformed token. If you don't care about
that, then just use "%d".

There are more elaborate ways of processing the input, of course, but
this ought to give you a good start.


Fido: The University of Woolloomooloo 1:272/38.473



Fri, 18 Aug 1995 00:15:26 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Invalid input in text file

2. Invalid input in text file

3. Inputting data as text files.

4. Search a input string in a text file

5. Input a Text file and SORT

6. Need help with input from text file

7. Progress bar on inputted text file

8. state of input stream after bad scanf ?5

9. Bad Input

10. Bad Include (was bad library)

11. input file is an .avi file

12. Count words from an input file via file redirection

 

 
Powered by phpBB® Forum Software