226k views
3 votes
Open the"stateData3.c" program and try to understand how the tokenization works. If you open the input file "stateData.txt", you can clearly seethat 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 arrayelements such that each line has a state and itspopulation. 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.

User Benjimin
by
4.9k points

1 Answer

5 votes

Answer:

Kindly see explaination

Step-by-step explanation:

Code

#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;

}

User Skyost
by
5.1k points