Confused with pointers and structs

This is a discussion on Confused with pointers and structs within the C Programming forums, part of the General Programming Boards category; I am writting a prohram that declares a struct with 3 ints and 1 float called weather_t. I must read ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    Confused with pointers and structs

    I am writting a prohram that declares a struct with 3 ints and 1 float called weather_t. I must read the data in from a txt file for 12 months of data which would be 48 numbers. I need 2 functions one that load the stats- int loadstats and one that prints the stats. Main is declaring the array and open and closing the data. My problem is I am using a loop to take the data from the file and trying to load it into the array using fread with a file pointer.
    I would think reading it in 4 peices at a time would be appropiate because there is 4 per month. The next step is trying to pass that data and loaded into the struct with the function- it doesdn't like (fp, weather_t) and for print purposes it doesn't like weather_t wdata

    Code:
    #include <stdio.h>
    	typedef struct {
    	int avg_high_temp;
    	int avg_low_temp;
    	float avg_precip;
    	int avg_wind_speed;
    	} weather_t;
    	
    	//int loadstats(fp, weather_t );  prototype
    
    	//void printstats(weather_t wdata[]);  protoyype	
    	
    int main(int argc, char *argv[])
    {
    	weather_t wdata [48];
    
       	FILE *fp;
    	fp=fopen("wfile.txt", "r");
    	
    for(i = 0; i < weather_t wdata; i+=4)
    	{
    	fread(&weather_t wdata [i], sizeof(int),48,fp);
    		
    }
       
     return 0;
    }
    	int loadstats(fp, weather_t );
    
    
    	void printstats(weather_t wdata[]);

    Any help is appreciated

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    fread and fwrite are intended for binary files. You're using a text file, so try some other I/O functions.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    So, you are having problems trying to come up with the correct function signatures?

    Incidentally, you should properly indent your code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    17
    fgetc is a way to input a character at a time but the problem is the data is 3 ints and 1 float so how do I get mixed data in? I see that fread is for binary only- my mistake

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Perhaps fscanf()

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    fscanf

    Here is my new code:
    Code:
    #include <stdio.h>
    	typedef struct {
    	int avg_high_temp;
    	int avg_low_temp;
    	float avg_precip;
    	int avg_wind_speed;
    	} weather_t;
    	
    	int loadstats(FILE*wfile, weather_t  wstats[]);
    
    	void printstats(weather_t wstats[]);
    
    	
    	int main(int argc, char *argv[])
    	{
    	weather_t wstats [12];
    	int i;
    	FILE*fp;
    		fp=fopen("wfile","r");
    	for (i=0; i < 48;i+=4)
    	{
    	fscanf(fp, "%i\n%i\n%f\n%i\n",&weather_t wstats[i]);
    
    			
    	}
    	
      	
    
     return 0;
    }
    on the fscanf I want to read 2 ints,a float then another int from my file to the array however it states illigal use of weather_t. Can I not use the struct name to load the array

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    Can I not use the struct name to load the array
    You should not pass the struct type as an argument. Rather, you should just pass pointers to the current struct's members.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:35 AM
  2. Confused with Structs and pointers
    By pc_doctor in forum C Programming
    Replies: 4
    Last Post: 03-27-2009, 07:08 PM
  3. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 06:14 PM
  4. pointers to pointers within structs
    By Lord_azrael99 in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 04:29 AM
  5. structs like pointers?
    By mart_man00 in forum C Programming
    Replies: 5
    Last Post: 03-14-2003, 02:16 AM

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