Thread: Stream is reset but still getting bogus results.

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    Stream is reset but still getting bogus results.

    all operations are on the file (stream). I am reading in the file, checking for one value, if found, report back, (reset stream) keep programming running. if not found report back ( reset stream) keep programming running.

    there is no criteria to have an option to quit.
    I added ftell to tell me what it is doing, it is showing 0 (zero) and still I get file not found when the information is valid. it is sporadic for the most part.
    so I am not sure why it is not working all of the time.

    Code:
    #include <stdio.h>
    
    int your_choice(void);
    int item_number(void);
    
    
    int main (int argc, const char **argv)
    {
        
        if(argc < 2)
        {
            printf("no file\n");
            return -1;
        }
        
        FILE *fp;
        
        if ( (fp = fopen(argv[1], "r") ) == NULL )
        {
            printf("File < %s > no open, yo!\n", argv[1]);
            return -1;
        }
        
        char buffer[1024];
        int inputID = 0, flag = 0, ItemId = 0, quantity= 0, price = 0;  
        
        while (1)
        {
            your_choice();
            inputID = item_number();
                
            while (( fgets(buffer, sizeof buffer, fp)) != NULL)
            {
                    //if found kick it back one
                    //more loop and start over. 
                    if ( flag == 1)
                    {
                        fseek(fp,0,SEEK_SET);
                        printf("reset flag\ntell me %ld\n", ftell(fp) );
                        flag = 0;
                        break;
                    }
                //run it through the entire file, or until a match
                //is found. 
    
    //either way this loop is done same results are gotten
    
                //while ( sscanf(buffer, "%d", &ItemId) == 1 )
                while ( sscanf(buffer, "%d%d%d", &ItemId, &quantity, &price) == 3 )
                {
             
    //used with other commented out while above
                //    sscanf(buffer, "%d%d%d", &ItemId, &quantity, &price);
                    printf("running in inner loop\ntell me %ld\n", ftell(fp) );
                    
                    if ( inputID == ItemId )
                    {
                        
                        printf("\n\n\nItemID Quantity Price Per Unit(SAR)\n");
                        printf("%d %10d %10d\n", ItemId,quantity,price);
                        getchar();
                        printf("\n\n\nPress Enter key to continue ...\n");
                        
                        //set stream back to start
                        fseek(fp,0,SEEK_SET);
                        
                        printf("inner loop\ntell me %ld\n", ftell(fp) );
                        
                        flag = 1;
                        getchar();
                        break;
                    }        
                else
                    {
                        //keeps it going until NULL, or EOF
                        break;
                    }
                
             } //end inner loop    
             
              
         } //end outer loop
         //when buffer reaches NULL, to 
         //and to prevent it from chaning whenever
         // the it matchs, on break
        if ( inputID != ItemId)
        {
                getchar();
                //set stream back to start
                fseek(fp,0,SEEK_SET);
                printf("error not found\ntell me %ld\n", ftell(fp) );
                printf("\n\nError: Invalid item ID\n\n\n"
                "Press Enter key to continue ...\n");
                flag =1;
                getchar(); 
        }
            //set stream back to start
            //fseek(fp,0,SEEK_SET);
     }//end outer 3rd loop    
    return 0;    
    }
    
    
    int your_choice(void)
    {
        printf("Please Select your choice: ");
        
        int choice = 0;
        scanf(" %d", &choice);
        return choice;
    }
        
    int item_number(void)
    {
        printf("Enter itemID: ");
        int item_number = 0;
        scanf(" %d", &item_number);
        return item_number;
    }

    test file
    Code:
    1000    3    10
    1001    1    30
    1002    6    23
    1003    9    32
    1004    42    55
    some results
    Code:
    reset flag
    tell me 0
    Please Select your choise: 2
    Enter itemID:     4222
    running in inner loop
    tell me 10
    running in inner loop
    tell me 20
    running in inner loop
    tell me 30
    running in inner loop
    tell me 40
    running in inner loop
    tell me 51
    error not found
    tell me 0
    
    
    Error: Invalid item ID
    
    
    Press Enter key to continue ...
    
    Please Select your choise: 1
    Enter itemID: 10004
    reset flag
    tell me 0
    error not found
    tell me 0
    
    
    Error: Invalid item ID
    
    
    Press Enter key to continue ...
    
    Please Select your choise: 1
    Enter itemID: 1004
    reset flag
    tell me 0
    Last edited by userxbw; 12-09-2017 at 03:10 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    sscanf doesn't work the way you think. It doesn't use the buffer as a stream. It always reads from the start to the end, it doesn't remember where it was since the previous call of sscanf. To do what you want, you're better off using strtok.

    EDIT: Actually, never mind, you use it correctly. I'm the one who misunderstood what the code is doing...
    Last edited by GReaper; 12-09-2017 at 03:18 PM.
    Devoted my life to programming...

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by GReaper View Post
    sscanf doesn't work the way you think. It doesn't use the buffer as a stream. It always reads from the start to the end, it doesn't remember where it was since the previous call of sscanf. To do what you want, you're better off using strtok.

    EDIT: Actually, never mind, you use it correctly. I'm the one who misunderstood what the code is doing...
    string token? Well at least it's good to know it wasn't strictly me per se' but that function.
    thanks

    edit fgets buffer is what is getting the "string" in the stream then sending it to sscanf, just thinking out loud.
    Last edited by userxbw; 12-09-2017 at 03:25 PM.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Nope , got it , it was one more flag reset I needed to add is all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is a stream? What really is a stream object?
    By Ramcin Oudishu in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2014, 01:33 PM
  2. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM
  3. how to reset getline?
    By MKashlev in forum C++ Programming
    Replies: 6
    Last Post: 08-11-2002, 08:51 AM
  4. Is the origin reset...
    By fletch in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2002, 05:59 PM
  5. help why does cnt not reset to 1??
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-17-2002, 12:56 AM

Tags for this Thread