Mini Activity 1: You need to read a text file, line by line, and output the line
ID: 3807911 • Letter: M
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 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<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
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<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;
}
Explanation / Answer
Mini Activity 1:
//TODO: Read the file, line by line and output each line to standard output (use printf() for each line
while ((fscanf(instream, "%[^ ]", tempBuffer)) != EOF)
{
fgetc(instream); // Reads in ' ' character and moves file
// stream past delimiting character
printf(" %s ", tempBuffer);
}
MiniActivity 2:
//TODO: Open the output file in write ("w") mode
FILE *outstream = fopen(outFile, "w");
if (outstream == NULL)
{
printf("Cannot open file %s ", filename);
exit(1);
}
//TODO: Read the file, line by line and write each line into the output file
while ((fscanf(instream, "%[^ ]", tempBuffer)) != EOF)
{
fgetc(instream);
fputc(tempBuffer, outstream);
}
printf(" Contents copied to %s", outFile);
//TODO: Close the output file
fclose(outstream);