Member Avatar for Griff0527

I just started this program a few hours ago, and I am running into an error that I do not understand. I tried finding an answer on Google as well. First off, this is the bare bones of a code right now. I have not started any of the sub-functions yet. All my goal is right now is to input the .txt file I created, and then print it back on the console so I can ensure the data is stored properly. The error I am getting is "error: expected primary-expression before '->' token" in my fscanf lines (Lines 26-29). I'm getting the same error in regards to the "." in my printf line (Line 32). My best guess is that my structure "measured_data_t" is created improperly, or I am referencing it improperly. Can someone assist me in better understanding my error?

#include <stdio.h>
 #include <string.h>

 #define ID_LEN     4
 #define SPEED_LEN  2
 #define DAY_LEN    2
 #define TEMP_LEN   2

 typedef struct {
     int site_id_num[ID_LEN];   /* The site ID number for the station */
     int wind_speed[SPEED_LEN]; /* The daily wind speed as measured at noon on the day_of_month */
     int day_of_month[DAY_LEN]; /* The numerical day of the month measurements are made */
     int temperature[TEMP_LEN]; /* The temperature as measured at noon on the day_of_month */
 } measured_data_t;

 int
 main(void)
int i, MAX;

    FILE *inp;              /* File pointer to read text file */

    /* Read data file into structure */
    inp = fopen("weather_data.txt", "r");

    printf("%-10s%-6s%-17s%-20s", "ID", "Day", "Wind Speed (knots)", "Temperature (deg C)");
        fscanf("%d%d%d%d", measured_data_t->site_id_num,
                           measured_data_t->day_of_month,
                           measured_data_t->wind_speed,
                           measured_data_t->temperature);
        for (i = 0; i <= MAX; i++)
        {
            printf("%-10d%-6d%-17d%-20d", measured_data_t.site_id_num, measured_data_t.day_of_month, measured_data_t.wind_speed, measured_data_t.temperature);
            i++;
        }
}

The overall goal of the program is to pull all the integer data from the text file, perform various calculations in sub functions, and then print them out. My concern is ensuring that I understand how to reference my structure properly prior to writing the rest of the code erroneously. Any help would be appreciated.

If your using fscanf then it should read like

fscanf(stdin, "format string", ....);

I say this is the most helpful way...Get an intro book on C programming and read. You have so many errors in your code that I'm not sure where I should begin...

Your structure. Why do you have integer arrays for things like site_id_num, wind_speed, day_of_month and temperature?

Member Avatar for Griff0527

Thanks. I appreciate the insight. i have integer arrays set up now because as the code expands, I will need them. I went ahead and declared them now before I have the code written. The text file I am importing should fill those arrays if I understand the chapter on Structure and Union types. I take it from your post that I may have misunderstood the chapter, so I am going to read it a third time. I didn't take any offense at you telling me to get an intro to C book. I'm currently taking an online college course (8 weeks long, so it is a rush through course). I'm trying to learn as quickly as possible without the benefit of an actual instructor. For anyone else that posts on this thread... I don't take offense easily. After 14 years in the military (11 of them in combat arms) I'm pretty thick skinned. :-)

I say this is the most helpful way...Get an intro book on C programming and read. You have so many errors in your code that I'm not sure where I should begin...

Your structure. Why do you have integer arrays for things like site_id_num, wind_speed, day_of_month and temperature?

Because I haven't read your materials I can only guess at what you require but the basic setup of your program is wrong...or at least inconsistent...I think this is what your after.

#include <stdio.h>
#include <string.h>

#define ARR_SIZE 4

typedef struct  
{
	int site_id_num;   
	int wind_speed; 
	int day_of_month;
	int temperature; 
} measured_data_t;

int main(void)
{             
	int i = 0;
	measured_data_t thestr[ARR_SIZE];

	
	for (i = 0; i < ARR_SIZE; ++i)
	{
	printf("%-10s%-6s%-17s%-20s", "ID", "Day", "Wind Speed (knots)", "Temperature (deg C)");
	fscanf(stdin, "%d%d%d%d", 	&thestr[i].site_id_num,\
					&thestr[i].day_of_month,\
					&thestr[i].wind_speed,\
					&thestr[i].temperature);
	}

	for (i = 0; i < ARR_SIZE; ++i)
	{
	fprintf(stdout, "our data - id->%d, day->%d, wnd->%d, temp->%d\n", 	thestr[i].site_id_num,\
										thestr[i].day_of_month,\
										thestr[i].wind_speed,\
										thestr[i].temperature);
	}
	return 0;
}
Member Avatar for Griff0527

I'll try to explain a little better on what I am working on then. Basically, I have a text file that has a variable amount of lines, no more than 70. The concept is, a maximum of 10 different sites, each one being measured one day a week. All I am doing right now, is trying to create a fscanf to read from the .txt file, then generate a print line to print the headings (outside the loop - I had placed it inside from oversight), then a print loop to print the data inside the file and insure that it has read in properly. The data looks something like this in the file:
2001 10 11 30
2001 11 5 22
3345 10 9 26
3345 11 14 23
etc . The first number is the ID, second is the Day, third is wind speed, fourth is temperature. There are 7 lines for each ID, and anywhere between 2 and 20 ID's before <EOF> is reached. All I am wanting to do is prove to myself that the file is scanning properly. This is not part of the assignment, it is simply me trying to understand C better than I do. Being the neophyte I am at C, even I can tell that my code is badly written, but I cannot see my own specific errors. I am attempting to rewrite what I had to make more sense and combing over my book again as it doesn't tell how to print out a file in it's entirety. Thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.