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.
C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section8.4: File Streams As Function Arguments
Problem 3E
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
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage