Question
There are three small sub-activities and you are given three partially completed programs. Read the following instructions carefully and write your logic at TODO: line
There are three small sub-activities and you are given three partially completed programs. Read the following instructions carefully and write your logic at "TODO:" line of each program.
c. Open the "stateData3.c" program and try to understand how the tokenization works. If you open the input file "stateData.txt", you can clearly see that a comma separates the state name and its population. The tokenizer portion of the programs separates each line into two tokens and stores them into two different arrays. You need to display the array elements such that each line has a state and it's population. Also calculate the total population of USA. Then examine the portion of the code how it writes the array elements into a binary datafile. You need to write similar logic where it writes the array elements into a text file "stateDataOutput2.txt". Please check the syntax and usage of fprintf(); and use that here.
>>>"stateData3.c"<<<
#include
#include
#include
int main(void) {
int const size = 200;
int const numStates = 50;
char fileName[] = "stateData.txt";
char outFile[] = "stateDataOutput2.txt";
char tempBuffer[size];
char tmp[size];
char states[numStates][35]; // string array to store state names
int states_pops[numStates]; //integer array to store population of each state
//open the file, quit if it fails...
FILE *instream = fopen(fileName, "r");
if(instream == NULL) {
fprintf(stderr, "Unable to open file: %s ", fileName);
exit(1);
}
//read the file, line by line
int i=0;
while(fgets(tempBuffer, size, instream) != NULL) {
//remove the endline character from the line
tempBuffer[strlen(tempBuffer)-1] = '\0';
//tokenize the line, breaking along commas:
char *stateToken = strtok(tempBuffer, ",");
strcpy(states[i], stateToken); // store state names in states[] array
char *popToken = strtok(NULL, ",");
states_pops[i] = atoi(popToken); // store population in the population array
i++;
}
// Close the input file
fclose(instream);
//TODO: output the state names and populations to the standard output and calculate total population
// (Hint: use states[] and states_pop[] arrays , you don't need to read the file again )
// Output to a binary datafile
FILE *outstream = fopen("stateData.dat", "wb");
for(i=0; i //limit the state name to 15 characters fwrite(states[i], sizeof(char), 15, outstream); //output a single int type fwrite(&states_pops, sizeof(int), 1, outstream); } fclose(outstream); //TODO: Output to the output file ("stateDataOutput2.txt") // (Hint: Similar to the above piece of code i.e., writing to binary data file. You can use "fprintf()" instead of "fwrite()" ) return 0; } >>>>stateData.txt<<< Alabama , 4779736 Alaska , 710231 Arizona , 6392017 Arkansas , 2915918 California , 37253956 Colorado , 5029196 Connecticut , 3574097 Delaware , 897934 Florida , 18801310 Georgia , 9687653 Hawaii , 1360301 Idaho , 1567582 Illinois , 12830632 Indiana , 6483802 Iowa , 3046355 Kansas , 2853118 Kentucky , 4339367 Louisiana , 4533372 Maine , 1328361 Maryland , 5773552 Massachusetts , 6547629 Michigan , 9883640 Minnesota , 5303925 Mississippi , 2967297 Missouri , 5988927 Montana , 989415 Nebraska , 1826341 Nevada , 2700551 New Hampshire , 1316470 New Jersey , 8791894 New Mexico , 2059179 New York , 19378102 North Carolina , 9535483 North Dakota , 672591 Ohio , 11536504 Oklahoma , 3751351 Oregon , 3831074 Pennsylvania , 12702379 Rhode Island , 1052567 South Carolina , 4625364 South Dakota , 814180 Tennessee , 6346105 Texas , 25145561 Utah , 2763885 Vermont , 625741 Virginia , 8001024 Washington , 6724540 West Virginia , 1852994 Wisconsin , 5686986 Wyoming , 563626
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started