The program opens up the data.txt file using the fopen function and will read the data into two arrays called column1 and column2. Since we don't know how big the file is, we can't just declare arrays of a fixed size. Instead, we will allocate memory using the malloc function. Initially, we allocate space for 10 rows of integers. The next step is where you write code to read in the file. This will be a loop in which you read each line of the file one by one. To read a line of the file, use the fscanf function to read in the two columns as follows int numl, num2; int retval= fscanf(fp,"%d %d\n", &numl, &num2) You will need to put the num1 and num2 variables into the appropriate array location of column1 and column2. Every time you read a line, increment the num_rows variable. fscanf will return an EOF when it has reached the end of the file. So, when retval==EOF, you can exit the loop. Since the file may have more than 10 rows, you may need to change memory allocation. So, check if the number of rows is a multiple of 10 (which you can do by checking if num_rows % 10 == 0), and if so, reallocate the memory to 10 more than num_rows. column1 = realloc (column1, (num_rows + 10) *sizeof (int)); At the end of the program, iterate through the rows and write the columns to the data2 file with the columns switched. 1. The following program is the start of a program that will read a file consisting of two columns of data. It is unknown how many rows of data there are. #include #include int main(int argc, char **argv) { if (arge ! 3 ) { printf("%s inputfile outfile\n", argv[0]); exit(0); } FILE *fp = fopen (argv[1], "r"); if (fp == NULL) { perror ("Could not open file"); exit(0); } FILE *fp2 = fopen (argv [2], "w"); if (fp2 == NULL) { perror ("Could not open file"); exit(0); } int num_rows = 0; int *column1 = malloc (10*sizeof(int)); int *column2 = malloc (10*sizeof(int)); // TODO read from the data.txt file with a loop using fscanf for (int i=0; i
The program opens up the data.txt file using the fopen function and will read the data into two arrays called column1 and column2. Since we don't know how big the file is, we can't just declare arrays of a fixed size. Instead, we will allocate memory using the malloc function. Initially, we allocate space for 10 rows of integers. The next step is where you write code to read in the file. This will be a loop in which you read each line of the file one by one. To read a line of the file, use the fscanf function to read in the two columns as follows int numl, num2; int retval= fscanf(fp,"%d %d\n", &numl, &num2) You will need to put the num1 and num2 variables into the appropriate array location of column1 and column2. Every time you read a line, increment the num_rows variable. fscanf will return an EOF when it has reached the end of the file. So, when retval==EOF, you can exit the loop. Since the file may have more than 10 rows, you may need to change memory allocation. So, check if the number of rows is a multiple of 10 (which you can do by checking if num_rows % 10 == 0), and if so, reallocate the memory to 10 more than num_rows. column1 = realloc (column1, (num_rows + 10) *sizeof (int)); At the end of the program, iterate through the rows and write the columns to the data2 file with the columns switched. 1. The following program is the start of a program that will read a file consisting of two columns of data. It is unknown how many rows of data there are. #include #include int main(int argc, char **argv) { if (arge ! 3 ) { printf("%s inputfile outfile\n", argv[0]); exit(0); } FILE *fp = fopen (argv[1], "r"); if (fp == NULL) { perror ("Could not open file"); exit(0); } FILE *fp2 = fopen (argv [2], "w"); if (fp2 == NULL) { perror ("Could not open file"); exit(0); } int num_rows = 0; int *column1 = malloc (10*sizeof(int)); int *column2 = malloc (10*sizeof(int)); // TODO read from the data.txt file with a loop using fscanf for (int i=0; i
Oh no! Our experts couldn't answer your question.
Don't worry! We won't leave you hanging. Plus, we're giving you back one question for the inconvenience.
Submit your question and receive a step-by-step explanation from our experts in as fast as 30 minutes.
You have no more questions left.
Message from our expert:
Our experts need more information to provide you with a solution. where is data1.txt ? Please resubmit your question, making sure it's detailed and complete. We've credited a question to your account.
Your Question:
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage