Thread: problems with structure

  1. #16
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    Code:
    void read_data()
    {
    	  
    	  int store;
    	  int i,j;
    	  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++)
    	  {
    	  	for(j=0;j<=239;j++);
    		{
    			 store = fscanf(pFile,"%d %f %f %d\n",&month[i].Time[j], &month[i].DryTempC[j], &month[i].DewPointC[j],&month[i].RH[j]);
    			printf("%d\n",store);
    			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);
    }

  2. #17
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    Code:
    void read_data()
    {
    	  
    	  int store;
    	  int i,j;
    	  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++)
    	  {
    	  	for(j=0;j<=239;j++);
    		{
    			 store = fscanf(pFile,"%d %f %f %d\n",&month[i].Time[j], &month[i].DryTempC[j], &month[i].DewPointC[j],&month[i].RH[j]);
    			printf("%d\n",store);
    			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);
    }
    
    
    I have tried using the returning values of fscanf but it is still not reading the data.

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The return value of fscanf is the number of items read, so if it is not reading anything, it will be 0. What are you getting? This helps to locate the problem, which is what "debugging" is about.

    Also, try removing the '\n' in the fscanf line.

    This is something you added:
    Code:
    &month[i].Time[j]
    What is the [j] supposed to mean? Time is not an array in your original struct.

    What I would do if I were you is write a simplified version of this where you just read one line into a pair of ints and a pair of floats. Once that works, you can add the loop and struct back in.
    Last edited by MK27; 07-07-2009 at 09:18 AM.
    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

  4. #19
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    Code:
    #include <stdio.h>
    
    typedef struct{
    	int Time;
    	float DryTempC, DewPointC;
    	int RH;
    	}MONTH;
    
    	MONTH month[240];
    
    void read_data();
    
    
    int main()
    {
    
    	
    	read_data();
    
    }
    
    void read_data()
    {
    	  
    	  int store;
    	  int i;
    	  
      	  FILE * pFile;
    	  pFile = fopen ("KBIS_2005.txt","r");
    	  
    	  for(i=0;i<=239;i++)
    	  {
    	  	
    			 store = fscanf(pFile,"%d %f %f %d",&month[i].Time, &month[i].DryTempC, &month[i].DewPointC,&month[i].RH);
    			printf("%d\n",store);
    			printf("%3d %4.1f %4.1f %3d\n",month[i].Time, month[i].DryTempC, month[i].DewPointC, month[i].RH);
    		
    	  }
    	
    	fclose(pFile);
    }
    this is the final code.now u can tell me what should and where should i change the codes

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    C programming is very finnicky. Here is a short program I wrote:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	int one, four, retv;
    	float two, three;
    	char line[256];
    	FILE *in = fopen("test.txt", "r");
    	while (fgets(line,256,in)) {
    		retv=sscanf(line,"%d %f %f %d",&one,&two,&three,&four);
    		printf("%d items read: %d %f %f %d\n",retv,one,two,three,four);
    	}
    	
    	return 0;
    }
    to read a file which looks like this, that I cut n' paste from one of your posts:
    Code:
    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
    Notice there are no column headings in the file, if there are column headings you will have to skip the first line. Anyway, the output is:

    4 items read: 0 0.600000 -5.000000 67
    4 items read: 1 0.000000 -3.900000 75
    4 items read: 2 0.000000 -3.900000 75
    4 items read: 3 0.000000 -3.900000 75
    4 items read: 4 0.000000 -4.000000 75
    -1 items read: 4 0.000000 -4.000000 75


    The final line happens when EOF is reached. The reason I used a combination of fgets() and sscanf() instead of fscanf() is because you only want part of each line.

    Try this program on your file and see if it works, then we can gradually add what's necessary to put the data into a struct array.
    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. #21
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    thnks a lot..now its working..now i have to use the loops to make sure to read only few lines..like i have around 12000 lines in my file. now from that lines i have to read only 240 lines. so i have to now use loops right?

  7. #22
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pokhara View Post
    thnks a lot..now its working..now i have to use the loops to make sure to read only few lines..like i have around 12000 lines in my file. now from that lines i have to read only 240 lines. so i have to now use loops right?
    You could use the same sort of while loop as above, since fgets returns 0 at the end, whether it read 10 lines or 10000. When fgets() returns 0, the while loop is over.

    I just noticed the real reason for the extra "-1" line is because I had a blank line at the end of the file, from which sscanf gets nothing and so returns -1. Ie, you might want to use this check (of sscanf's return value) to prevent that kind of error.

    If you only want the first 240 lines, then use a for loop.
    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. #23
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	int Time, RH, retv;
    	float DewPointC, DryTempC;
    	char line[256];
    	int i;
    	FILE *in = fopen("KBIS_2005.txt", "r");
    	for(i = 0; i<=240; i++)
    	{
    	while (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);
    	}
    	}
    	return 0;
    }
    
    
    this code works fine..but now since i have to read only 240 lines, i used the for loop and limit it till 240 but it is still running the whole file.

  9. #24
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    hello MK27,, i am waiting for ur reply. please help.

  10. #25
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Post the version with the for loop.
    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. #26
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	int Time, RH, retv;
    	float DewPointC, DryTempC;
    	char line[256];
    	int i;
    	FILE *in = fopen("KBIS_2005.txt", "r");
    	
    	for(i = 0; i<=240; i++)
    	{
    	while (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);
    		}
    	}
    	return 0;
    }

  12. #27
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Oops! You want to use the for() loop instead of the while loop, get it? The way it is now, you would read the entire file 240 times (although that will not happen because the file pointer will not reset to the beginning by itself).
    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

  13. #28
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    no i didn't get u...

  14. #29
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    no i do not need to read file 240 times instead read the 240 lines of the file

  15. #30
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Clue:
    Code:
    for(i = 0; i<=240; i++) {
    	fgets(line,256,in);
    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