Thread: A little stupid problem with my c code.

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    8

    A little stupid problem with my c code.

    In a part of my program I am trying to use fscanf to read from a file, that has a format as following
    14:37:37.825 0:00:00.020
    14:37:37.845 0:00:00.020
    14:37:37.865 0:00:00.020
    14:37:37.885 0:00:00.020
    14:37:37.905 0:00:00.020
    .. ..
    I used the line
    while(fscanf(fp,"%2d %c %2d %c %5.3f %2d %c %2d %c %5.3f",&h1,&c1,&h2,&c2,&h3,&h4,&c3,&h5,&c4,&h6)!=E OF)
    trying to accomplish the job, but it didn't work. I will be very thankful if someone could suggest me a way out.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by Ironmaniac View Post
    In a part of my program I am trying to use fscanf to read from a file, that has a format as following

    I used the line trying to accomplish the job, but it didn't work. I will be very thankful if someone could suggest me a way out.
    Use fwrite() and fread() instead of fprintf and fscanf() respectively.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    "Doesn't work" doesn't mean anything.
    Code:
    fscanf( fp, "%d:%d:%f %d:%d:%f", ... )
    If it doesn't work print out your variables and see how far you get. Also consider using fgets and then sscanf.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Quote Originally Posted by quzah View Post
    "Doesn't work" doesn't mean anything.
    This is inspiring, thanks.
    Code:
    fscanf( fp, "%d:%d:%f %d:%d:%f", ... )
    Tried this...prints strange values.
    Quote Originally Posted by quzah View Post
    Also consider using fgets and then sscanf.
    Will do that. Thank you.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Thanks chibi.coder. Will try fread() and fwrite().

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by chibi.coder View Post
    Use fwrite() and fread() instead of fprintf and fscanf() respectively.
    That's a terrible idea.

    When in doubt, write a small test case:
    Code:
    #include<stdio.h>
    int main( void )
    {
        char *p[] =
        {
            "14:37:37.825 	0:00:00.020",
            "14:37:37.845 	0:00:00.020",
            "14:37:37.865 	0:00:00.020",
            "14:37:37.885 	0:00:00.020",
            "14:37:37.905 	0:00:00.020",
            NULL,
        };
        int x = 0;
        
        for( x = 0; p[ x ]; x++ )
        {
            int h1 = 0, m1 = 0;
            int h2 = 0, m2 = 0;
            float s1 = 0.0f, s2 = 0.0f;
            int rval = 0;
            
            rval = sscanf( p[ x ], "%d:%d:%f %d:%d:%f",
                &h1, &m1, &s1, &h2, &m2, &s2 );
        
            printf( "string: %s\nrval: %d\n", p[ x ], rval );
            printf( "h1: %d, m1: %d, s1: %f\nh2: %d, m2: %d, s2: %f\n",
                h1, m1, s1, h2, m2, s2 );
        }
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Ironmaniac View Post
    In a part of my program I am trying to use fscanf to read from a file, that has a format as following
    Code:
    while(fscanf(fp,"%2d %c %2d %c %5.3f %2d %c %2d %c %5.3f",&h1,&c1,&h2,&c2,&h3,&h4,&c3,&h5,&c4,&h6)!=E OF)
    I used the line trying to accomplish the job, but it didn't work. I will be very thankful if someone could suggest me a way out.
    1) don't use field sizes in scanf().
    2) watch your spacing it's EOF not E OF

    Scanf works by pattern matching so you need to create a format string that matches the format of your file...
    that is -> number:number:number.number number:number:number.number
    so...
    Code:
    fscanf(fp,"%d:%d:%d.%d %d:%d:%d.%d"...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Ironmaniac View Post
    Thanks chibi.coder. Will try fread() and fwrite().
    Don't waste your time. It won't solve your problem.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Quote Originally Posted by quzah View Post
    That's a terrible idea.

    When in doubt, write a small test case:
    Code:
    #include<stdio.h>
    int main( void )
    {
        char *p[] =
        {
            "14:37:37.825     0:00:00.020",
            "14:37:37.845     0:00:00.020",
            "14:37:37.865     0:00:00.020",
            "14:37:37.885     0:00:00.020",
            "14:37:37.905     0:00:00.020",
            NULL,
        };
        int x = 0;
        
        for( x = 0; p[ x ]; x++ )
        {
            int h1 = 0, m1 = 0;
            int h2 = 0, m2 = 0;
            float s1 = 0.0f, s2 = 0.0f;
            int rval = 0;
            
            rval = sscanf( p[ x ], "%d:%d:%f %d:%d:%f",
                &h1, &m1, &s1, &h2, &m2, &s2 );
        
            printf( "string: %s\nrval: %d\n", p[ x ], rval );
            printf( "h1: %d, m1: %d, s1: %f\nh2: %d, m2: %d, s2: %f\n",
                h1, m1, s1, h2, m2, s2 );
        }
        return 0;
    }
    Quzah.
    Thank you Quzah. Thats a nice clean code.

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    1) don't use field sizes in scanf().
    2) watch your spacing it's EOF not E OF

    Scanf works by pattern matching so you need to create a format string that matches the format of your file...
    that is -> number:number:number.number number:number:number.number
    so...
    Code:
    fscanf(fp,"%d:%d:%d.%d %d:%d:%d.%d"...
    Thank you CommonTater . I tried correcting my program according to your suggestions. I removed the field sizes like you and Quzah pointed out. And it was certainly EOF like you pointed out. Now it still didn't work. I will request you all to please take a look at the following..
    Okay here is
    1.A chunk of my code after I have it edited by removing the field sizes in the scanf line . And

    2.I have a little 7KB file attached to this post that could be used as the input file to this code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    main()
    {     
        int h1,h2,h4,h5;
        float h3,h6;
            FILE *fp,*fp1;
        fp=fopen("in.txt","r");
        fp1=fopen("a.txt","w");
    
    
                        /*  11:57:25.138  0:00:00.020 */
      while(fscanf(fp,"%d %d %f %d %d %f",&h1,,&h2,&h3,&h4,&h5,&h6)!=EOF)
    {
    
    
        if(h6>0.02 && h6<=0.1)
                { 
              fprintf(fp1,"%d:%d:%f\n",h1,h2,h3);     
              
    }    }
    
    
    
    
    
    
            return(0);
    }
    Attached Files Attached Files

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
                     /*  11:57:25.138  0:00:00.020 */
      while(fscanf(fp,"%d %d %f %d %d %f",&h1,,&h2,&h3,&h4,&h5,&h6)!=EOF)
    You can't actually be reading what we've been telling you. Look at your comment, then look at what you are scanning. Where are you taking care of the : :? You aren't. You should also actually be paying attention to what fscanf is returning, instead of just checking for EOF.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Code:
                  /*  11:57:25.138  0:00:00.020 */  while(fscanf(fp,"%d %d %f %d %d %f",&h1,,&h2,&h3,&h4,&h5,&h6)!=EOF)
    No no..Quzah...the above was in my previous code...I have changed it like you wanted me to....which now is
    Code:
    while(fscanf(fp,"%d:%d:%f %d:%d:%f",&h1,,&h2,&h3,&h4,&h5,&h6)!=EOF)
    I wanted to present the whole situation as it was. I am trying this program to read from file similar to the attached one, only much larger. I want the program to select certain lines in the input file depending on the value of the variable h6 in that line printing it to another file.
    Last edited by Ironmaniac; 12-23-2011 at 01:51 AM.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    So what is your current code and problem then? Be specific!

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    8

    Lightbulb

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    main()
    {  int n1,n2,n3;
        int h1,h2,h4,h5;
        float h3,h6;
    
    
        FILE *fp,*fp1,*fp2,*fp3;
        fp=fopen("in.txt","r");
        fp1=fopen("a.txt","w");
        fp2=fopen("b.txt","w");
        fp3=fopen("c.txt","w");
        n1=n2=n3=0;
    
    
           /*      11:57:25.138    0:00:00.020 */
           while(fscanf(fp,"%d:%d:%f %d:%d:%f",&h1,&h2,&h3,&h4,&h5,&h6)!=EOF)
        {
            if(h6>00.020 && h6<=1.00)
            {
            fprintf(fp1,"%d:%d:%5.3f\n",h1,h2,h3);
               n1=n1+1;
            }
    
    
    
    
            if(h6>1.00 && h6<=2.00)
            {
            fprintf(fp2,"%d:%d:%5.3f\n",h1,h2,h3);
               n2=n2+1;
            }
    
    
            if(h5*h6>60)
            {
            fprintf(fp3,"%d:%d:%5.3f\n",h1,h2,h3);
               n3=n3+1;
            }
        }
            fprintf(fp1,"---------  %d",n1);
            fprintf(fp2,"---------  %d",n2);
            fprintf(fp3,"---------  %d",n3);
    }
    Hello everyone, the above is the code I was looking for. I would like to thank everyone who tried to help me. Especially Quzah and CommonTatter. Both provided working solutions. But I found the one by Quzah appropriate for my case.
    There were two problems that I had to figure out, namely
    1. Getting scanf the right format to read from the file supplied. This was solved by Quzah and CommonTatter.
    2.Initially I had named my input file 21marSV19.txt and that somehow made the program useless. I while experimenting re-named it to in.txt, and suddenly things fell in place on their own. Keep it simple, is my lesson.

    And like Quzah said...Doesn't work doesn't mean anything. Cheers!

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    thank you rags-to-riches for your time and interest.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid problem probably has a stupid answer...
    By hardcorpsems in forum C Programming
    Replies: 2
    Last Post: 11-01-2009, 06:15 AM
  2. Stupid problem
    By Adrian20XX in forum C Programming
    Replies: 6
    Last Post: 08-23-2008, 08:16 AM
  3. Does managed code make people stupid?
    By zacs7 in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 07-28-2008, 04:10 PM
  4. stupid problem. answer = 0.0
    By spydrvnm in forum C Programming
    Replies: 6
    Last Post: 09-26-2004, 10:53 AM
  5. Replies: 6
    Last Post: 09-07-2004, 11:06 PM