Activity 2: File Reading and Writing There are three small sub-activities and yo
ID: 3596977 • Letter: A
Question
Activity 2: File Reading and Writing
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.
Instructions
a. For this sub-activity, you need to read a text file, line by line, and output the lines. First you need to
open the file, then read the file and finally close the file. You need to refer to lecture slides for syntaxes
to open, read and close a file. You are given a partially completed program "stateData1.c" and the name
of text file to be used is "stateData.txt".
b. For this sub-activity, you need to write the contents of input file into an output file. Your program
should generate an output file "stateDataOutput1.txt". You need to read the input file "stateData.txt",
line by line, and each line should be written into the output file. You are required to add the logic in the
partially completed program "stateData2.c".
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.
stateData1.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void) {
int const size = 200;
int const numStates = 50;
char tempBuffer[size];
char tmp[size];
char fileName[] = "stateData.txt"; // Name of the text file which contains states and its populations
// 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);
}
//TODO: Read the file, line by line and output each line to standard output (use printf() for each line
// Close the file
fclose(instream);
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
stateData2.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void) {
int const size = 200;
int const numStates = 50;
char tempBuffer[size];
char tmp[size];
char fileName[] = "stateData.txt"; // Name of the text file (input file) which contains states and its populations
char outFile[] = "stateDataOutput1.txt"; // Output file name
// Open the input file, quit if it fails...
FILE *instream = fopen(fileName, "r");
if(instream == NULL) {
fprintf(stderr, "Unable to open file: %s ", fileName);
exit(1);
}
//TODO: Open the output file in write ("w") mode
//TODO: Read the file, line by line and write each line into the output file
// Close the input file
fclose(instream);
//TODO: Close the output file
return 0;
}
stateData3.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
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] = '';
//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<numStates; 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;
}
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define size 200
int main(void)
{
int const numStates = 50;
char tempBuffer[size];
char tmp[size];
char fileName[] = "stateData.txt"; // Name of the text file (input file) which contains states and its populations
char outFile[] = "stateDataOutput1.txt"; // Output file name
// Open the input file, quit if it fails...
FILE *instream = fopen(fileName, "r");
/* Output File variable */
FILE *opstream;
if(instream == NULL) {
fprintf(stderr, "Unable to open file: %s ", fileName);
exit(1);
}
//TODO: Open the output file in write ("w") mode
/* Opening output file in write mode */
opstream = fopen(outFile, "w");
//TODO: Read the file, line by line and write each line into the output file
//Reading data from file
while(fgets(tmp, size, instream) != NULL)
{
//Writing data to file
fputs(tmp, opstream);
}
// Close the input file
fclose(instream);
//TODO: Close the output file
/* Closing output file */
fclose(opstream);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.