Question
Mini Activity 1: You need to read a text file, line by line, and output the lines. First you need to open the file, then
Mini Activity 1: 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. Refer to common syntaxes to open, read, and close a text file. Below is a partially completed program, "stateData1.c" and the name of the text file to be used is "stateData.txt"
#include
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
Mini Activity 2: You need to write the contents of input file into an ouput file. The program should generate an output file "stateDataOutput1.txt". You need to read the input file "stateData.txt" (provided above) 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", (provided below).
#include
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; }
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