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.
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
Related questions
Question
Data.txt is given in the image
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
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
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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education