problems with structure

This is a discussion on problems with structure within the C Programming forums, part of the General Programming Boards category; "Time" "DryTempC" "DewPointC" "RH" "Day" "Year" "Month" "Jday" ??? You are getting 0 back from sscanf. You could add this: ...

  1. #46
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "Time" "DryTempC" "DewPointC" "RH" "Day" "Year" "Month" "Jday"

    ???

    You are getting 0 back from sscanf. You could add this:
    Code:
    printf("->%s<-\n%d items read: %d %f %f %d\n",line,retv,Time,DewPointC,DryTempC,RH);
    which will show you want that first unread line is
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #47
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    yes i have the title for each column. But How to skip it?Because i need to read the files according to the title.

  3. #48
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    it is still reading the first lines.how to ignore that?is there any way that we can just start by ignoring the first line.

  4. #49
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pokhara View Post
    yes i have the title for each column. But How to skip it?Because i need to read the files according to the title.
    Use the return value of sscanf(). You can use "continue" to skip to the next iteration of the loop:
    Code:
    if (!retv) continue;
    !retv means the same thing as "ret == 0" or "retv == NULL.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #50
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    Code:
    FILE *in = fopen("KBIS_2005.txt", "r");
    	if(!retv)
    	{
    	for(i = 0; i<=240; i++) 
    		{
    			
    			fgets(line,256,in);
    			retv=sscanf(line,"%d %f %f %d",&Time,&DewPointC,&DryTempC,&RH);
    			printf("%d items read: %d %f %f %d\n",retv,Time,DewPointC,DryTempC,RH);
    			continue;
    this is code i have written.

  6. #51
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    MK27 ..r u there? can u go through out this code...and check what is the problem?

  7. #52
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Not like that! Look:
    Code:
    for(i = 0; i<=240; i++) {
    			fgets(line,256,in);
    			retv=sscanf(line,"%d %f %f %d",&Time,&DewPointC,&DryTempC,&RH);
                            if (!retv) continue;
    			printf("%d items read: %d %f %f %d\n",retv,Time,DewPointC,DryTempC,RH);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #53
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    i tried this but still it is giving the first line

  9. #54
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by pokhara View Post
    yes i have the title for each column. But How to skip it?Because i need to read the files according to the title.
    Well that's different than what you've been saying all along. But it can be done, assuming you have tokens somehow to determine what needs to be read. Oh, and a place to store it would be good.
    Code:
    fgets titleline
    for each word in the title
        token  = lookup this word
        add token to input list
    
    for each remaining line in the file
        for each token in the token list
            variables,line,word = read variable from file based on token
    Now this gets simpler if you know you have a set number of title styles. For example:

    int int float
    word float float int


    Then you get to do:
    Code:
    fgets title
    title style = title lookup, title you just read
    
    for each remaining line in the file
        switch title style
            read line ( line format )

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

  10. #55
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pokhara View Post
    i tried this but still it is giving the first line
    Are you sure you have the continue line in the right place, *before* the printf() line? This was what you posted earlier:

    Code:
    0 items read: 0 0.000000 0.000000 4196133
    4 items read: 0 0.600000 -5.000000 67
    4 items read: 1 0.000000 -3.900000 75
    So retv was 0 for the first line. If you insert "if (!retv) continue;" BEFORE the printf() line, you should now get:
    Code:
    4 items read: 0 0.600000 -5.000000 67
    4 items read: 1 0.000000 -3.900000 75
    ...etc
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #56
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    hello MK27..i have now another problem..please be online

  12. #57
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    Code:
    int main(){
    char line[256];
    	int i, VPD;
    	float vapourp, setvapp;
    	float val;
    	FILE *in = fopen("KBIS_2005.txt", "r");
    	for(i = 0; i<=240; i++) 
    		{
    			
    			fgets(line,256,in);
    			retv=sscanf(line,"%d %f %f %d",&Time,&DewPointC,&DryTempC,&RH);
    			if (!retv) continue;
    			printf("%d items read: %d %f %f %d\n",retv,Time,DewPointC,DryTempC,RH);	
    
    vapourp = pow(611, (7.5 * DewPointC)/(237.7 + DewPointC));
    setvapp = pow(611,(7.5 * DryTempC)/(237.7 + DryTempC));
    			printf("setvapp is %s",setvapp);
    			float VPD;
    			VPD = setvapp - vapourp;
    }
    printf("VPD IS %s",VPD);
    
    }
    
    now i can read the data..now i need to calculate the above formula taking each value of time, dewpointc, drypointc and RH. i tried to run this code but don;t know it is giving the result.

  13. #58
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't print floats with %s, you know that.

  14. #59
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    oh yah....thnks...how can i do such a silly mistake...in the above code i have mention % f and here i am doing %s...thnks

  15. #60
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    hello MK27..thnks a lot for ur help...now i solved the previous problem...now i am working on next step...thnks

Page 4 of 6 FirstFirst 123456 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 05:17 AM
  2. structure ...solution plz????
    By hegdeshashi in forum C Programming
    Replies: 4
    Last Post: 07-24-2006, 09:57 AM
  3. structure problems in windows program.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2004, 06:18 PM
  4. Realloc problems with sturcture array inside structure
    By daveyand in forum C Programming
    Replies: 2
    Last Post: 03-29-2004, 05:48 AM
  5. Structure problems...
    By MillaTime in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 09:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21