Thread: trouble reading input

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    trouble reading input

    I have this input file:

    Code:
    John LongLastName
    143
    
    Ann Iaeiouyhfsdf.
    103
    
    Ann X.
    11
    
    Jim C.
    106
    
    Ann B.
    123
    
    John LongLastName
    143
    
    class size 3
    
    drop
    11
    103
    106
    I need to read this data in, and am trying to print it up on screen. At most the strings at the outputs shuld only have 9 characters... so John Longlastname... would be cut down to John Long for output.

    I made this loop to read it in:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    main(){
    
    char temp_name[30];
    char dummy[30];
    int ID;
    
    while( strstr("class siz",temp_name) == NULL) {
    
      fgets(temp_name,30,fin);        
           
            if(strlen(temp_name)>9){
            
               temp_name[9] = '\0' ; }
    
    fscanf(fin," %d",&ID); 
                                  
    printf("%s\n",temp_name);
    printf("%d\n",ID); 
    
    }
    but the output it gives me is:

    Code:
    John Long
    143
    
    143
    Ann Iaeio
    103
    
    103
    Ann X.
    
    11
    
    11
    Jim C.
    Could anyone tell me what I'm doing wrong? It looks like it's trying to scan in the blank line in the input.... I don't know how to get around this....

    I've tried putting in a dummy fgets to take in the blank line but it didn't work


    Thank You

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I've modified my loop a bit into this:

    but it is still not working right, it is not stopping at class size like the while loop says and it is also printing blank lines... I don 't know why.

    Code:
    do {
    
      fgets(temp_name,30,fin);
      fgets(temp_num,30,fin);
      sscanf( temp_num, "%d", &ID );
      fgets( dummy,30,fin);
                 
            if(strlen(temp_name)>9){
            
               temp_name[9] = '\0' ; }
    
    
        printf("%s\n",temp_name);
        printf("%d\n",ID); 
    
    
    } while( strstr("class siz",temp_name) == NULL);

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    if you want just to show the content of your file on the screen, just use one fgets. If the nuber will never be longer than 9 chars, you can still use your check "if(strlen..." on each line you read.
    If you want to enter a number if the number-line is read, you could check each line with isdigit, and when there are numbers, use the "sscanf(..."

    if you don't want to show the blank line in the output, jump over the "printf(..." if in the readed string is just a '\n'
    if(input_string[0] == '\n')

    mfg

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're aware that fin is undeclared? (I assume it's FILE *fin.)

    From that code there, it looks like you're trying to limit the user's input to 10 characters in length? Yes . . .
    At most the strings at the outputs shuld only have 9 characters
    fgets() will do that for you if you specify the size as 11 (9 + '\n' + NULL). Then you could discard the rest of the input:
    Code:
    #include <stdio.h>
    
    int main(void) {
        int c;
        size_t len;
        char name[11];
        FILE *fin;
    
        if((fin = fopen("./thefile.ext", "rt")) == NULL) {
            fprintf(stderr, "\nCan't open file \"./thefile.ext\"\n");
            return 1;
        }
    
        while(fgets(name, sizeof(name), fin)) {
            len = strlen(name);
            if(name[len-1] != '\n') {
                while((c = getc(fin)) != '\n' && c != EOF);
            }
    
            name[len-1] = 0;
    
            /* Now you have a name in name which is < 10 chars long */
            /* Do whatever you want with it (I just print it) */
            puts(name);
        }
    
        fclose(fin);
    
        return 0;
    }
    I think that should work -- I don't have a compiler handy, though, so I can't tell you.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading and processing input from keyboard
    By papagaio in forum C Programming
    Replies: 1
    Last Post: 11-12-2008, 03:59 PM
  2. reading input problems
    By gonzad in forum C Programming
    Replies: 1
    Last Post: 08-26-2008, 04:00 AM
  3. Reading Input From stdin
    By EvilGuru in forum C Programming
    Replies: 5
    Last Post: 11-25-2005, 01:48 AM
  4. reading an input line using scanf() with %c
    By linucksrox in forum C Programming
    Replies: 6
    Last Post: 04-04-2004, 03:10 PM
  5. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM