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 num1, 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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Data.txt is given in the image

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.
Transcribed Image Text: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 <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
if (arge ! 3 ) {
printf("%s input file 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);
12 72
DATA.TXT
}
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
78 29
for (int i=0; i<num_rows; i++) {
// TODO write to the data2.txt file using fprintf
The program will be executed as follows:
$ ./fileio data.txt data2.txt
where data.txt is the name of the file containing the data. data.txt has been provided for
you on
data2.txt is the name of the file that you will be writing to. The data.txt
file has two columns of numbers and you don't know how many rows of data there are. Your
program will need to read in the two columns into two arrays. After you read in the two arrays,
you will need to write to the data2.txt file with the columns flipped.
Transcribed Image Text: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 <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { if (arge ! 3 ) { printf("%s input file 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); 12 72 DATA.TXT } 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 78 29 for (int i=0; i<num_rows; i++) { // TODO write to the data2.txt file using fprintf The program will be executed as follows: $ ./fileio data.txt data2.txt where data.txt is the name of the file containing the data. data.txt has been provided for you on data2.txt is the name of the file that you will be writing to. The data.txt file has two columns of numbers and you don't know how many rows of data there are. Your program will need to read in the two columns into two arrays. After you read in the two arrays, you will need to write to the data2.txt file with the columns flipped.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Array
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education