Thread: problems with structure

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    53

    problems with structure

    I need to read certain columns of a data file for certain lines only. I have used the structure to read the files but I am getting problem with it. I don't have any errors but the main problem is that the data are read but all with null values. I know I should copy the data but don't know how. So please help me out.

    Code:
    typedef struct{
    	int Time;
    	float DryTempC, DewPointC;
    	int RH;
    	}MONTH;
    
    	MONTH month[240];
    
    void read_data();
    
    int main()
    			{
    				read_data();
    	
    			}
    
    			void read_data()
    			{
    				  
    				  int i;
    				  int Jday;
    			  	  FILE * pFile;
    				  pFile = fopen ("KBIS_2005.txt","r");
    				  printf("HD\t""T20\t""VPD05\t""TR2RH70\n\n");
    				  for(i=0;i<=239;i++)
    				  {
    				  	
    					fscanf(pFile,"%d\t""%f\t""%f\t""%d\t\n",&month[i].Time, &month[i].DryTempC, &month[i].DewPointC,&month[i].RH);
    					printf("%3d\t""%4.1f\t""%4.1f\t""%3d\n",month[i].Time, month[i].DryTempC, month[i].DewPointC, month[i].RH);
    
    				  }
    				fclose(pFile);
    }.
    and the file is something like
    Time  DewPointC DryPointc RH
    0	0.6	-5	67
    1	0	-3.9	75
    2	0	-3.9	75
    3	0	-3.9	75
    4	0	-4	75

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    try
    Code:
    fscanf(pFile,"%d %f %f %d",&month[i].Time, &month[i].DryTempC, &month[i].DewPointC,&month[i].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

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Your code makes my eyes bleed. Format your code properly in order for us to help you.

    Code:
    "%d\t""%f\t""%f\t""
    Why do you have various string constants in that line? It should all be within one pair of quotes.

    @MK27: Are you sure that fscanf() translate a space into any whitespace character?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by abraham2119 View Post
    Your code makes my eyes bleed. Format your code properly in order for us to help you.

    Code:
    "%d\t""%f\t""%f\t""
    Why do you have various string constants in that line? It should all be within one pair of quotes.
    It's legal, if bizarre (consecutive strings are merged into one during preprocessing. It reminds me of machine-generated code (like p2c or something), except the variable names make sense. Of course, that's not a style you want to copy as such.
    @MK27: Are you sure that fscanf() translate a space into any whitespace character?
    He's sure.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by abraham2119 View Post
    @MK27: Are you sure that fscanf() translate a space into any whitespace character?
    Yeah. You don't need to specify '\t', a space will catch it. But '\t' probably won't catch a space.
    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

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    '\t' works as well as anything else.

    Quote Originally Posted by man fscanf
    White space (such as blanks,
    tabs, or newlines) in the format string match any amount of white space,
    including none, in the input. Everything else matches only itself.

  7. #7
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by abraham2119 View Post
    Your code makes my eyes bleed.
    Don't tempt me, I'm just getting accustomed to my new avatar
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    yah i deleted the "\t" but the problem is there. please help me. I know i should copy the data into an array but how i don't know.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pokhara View Post
    yah i deleted the "\t" but the problem is there. please help me. I know i should copy the data into an array but how i don't know.
    I actually did cut n' paste this to a file yesturday:
    Code:
    0	0.6	-5	67
    1	0	-3.9	75
    2	0	-3.9	75
    3	0	-3.9	75
    4	0	-4	75
    and then use the fscanf line I recommended to you to read it, and it worked, so you will have to locate more specifically where something is going wrong.

    Have you tried checking the return value of the fscanf call, eg:
    Code:
    int retv;
    retv=fscanf(pFile,"%d %f %f %d", &month[i].Time, &month[i].DryTempC, &month[i].DewPointC,&month[i].RH);
    printf("%d\n",retv);
    Post the updated code you are using.
    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

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    yah i tried it..bt now it is displaying -1 value for only one column.where this -1 came from?and also we can ignore the printf("HD\t""T20\t""VPD05\t""TR2RH70\n\n");.this line is not in use right now. I just need to read those data from the files. there are more columns in this file like there are not only 4 columns. there are around 8 columns but I have to read only these 4 columns. any more suggestion please.it is not mandatory to use structure.we can use array too.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pokhara View Post
    yah i tried it..bt now it is displaying -1 value for only one column.where this -1 came from?and also we can ignore the printf("HD\t""T20\t""VPD05\t""TR2RH70\n\n");.this line is not in use right now. I just need to read those data from the files. there are more columns in this file like there are not only 4 columns. there are around 8 columns but I have to read only these 4 columns. any more suggestion please.it is not mandatory to use structure.we can use array too.
    The structure/array/whatever is irrelevant if you cannot read the data from the file correctly, so forget about that for now. Do you mean fscanf returned -1? If so, it reached eof without taking any data.

    Like I said last time, post the current version of the code you are using.
    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

  12. #12
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    Code:
    #include <stdio.h>
    
    typedef struct{
    	int Time[240];
    	float DryTempC[240], DewPointC[240];
    	int RH[240];
    	}MONTH;
    
    	MONTH month[240];
    
    void read_data();
    
    
    int main()
    {
    
    	
    	read_data();
    
    }
    
    void read_data()
    {
    	  
    	  int store;
    	  int i,j;
    	  int Jday;
      	  FILE * pFile;
    	  pFile = fopen ("KBIS_2005.txt","r");
              for(i=0;i<=239;i++)
    	  {
    	  	fscanf(pFile,"%d""%f""%f""%d\n",&month[i].Time[j], &month[i].DryTempC[j], &month[i].DewPointC[j],&month[i].RH[j]);
                    printf("%3d""%4.1f""%4.1f""%3d\n",month[i].Time[j], month[i].DryTempC[j], month[i].DewPointC[j], month[i].RH[j]);
    	  }
    
              fclose(pFile);
    }
    
    the file is something like this:
    "Time"	"DryTempC"	"DewPointC"	"RH"	"Day"	"Year"	"Month"	"Jday"
    0	0.6	-5	67	1	2005	5	121
    1	0	-3.9	75	1	2005	5	121
    2	0	-3.9	75	1	2005	5	121
    3	0	-3.9	75	1	2005	5	121
    4	0	-4	75	1	2005	5	121
    5	-1.1	-3.9	82	1	2005	5	121
    6	0	-3.9	75	1	2005	5	121
    Last edited by pokhara; 07-07-2009 at 08:22 AM. Reason: missing lines

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This:
    Code:
    fscanf(pFile,"%d""%f""%f""%d\n"
    does not look like
    Code:
    fscanf(pFile,"%d %f %f %d"
    And again: have you checked the return value of fscanf and what is it?
    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

  14. #14
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    no I haven't returned the value of fscanf and i have posted my codes. How should I do this?

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pokhara View Post
    no I haven't returned the value of fscanf and i have posted my codes. How should I do this?
    Hmmm. Well, I made some suggestions but you did not apply them. If I answer this question again, I will only answer it the same way, so you can try reading my previous posts again, or just ignore me and try something else.
    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

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, 06: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, 06:48 AM
  5. Structure problems...
    By MillaTime in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 09:27 PM