Question

In: Computer Science

C++ Write a program with the following elements: in main() -opens the 2 files provided for...

C++

Write a program with the following elements:

in main()

-opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt)

-calls a global function to determine how many lines are in each file

-creates 2 arrays of the proper size

-calls a global function to read the file and populate the array (call this function twice, once for each file/array)

-calls a global function to write out the 'merged' results of the 2 arrays

*if there are multiple entries for a person, these should be merged and should appear as a single entry in the resulting file

*resulting file should be named 'merged_output.txt'

program should check to see if a name is already present in one file or another. If the name is present in both files display the name once in the merged file and add the numbers.

Example:

1st File:

Carlos 7

Tina 3

2nd File:

Lena 2

Carlos 3

Merged File:

Carlos 10

Tina 3

Lena 2

Solutions

Expert Solution

The following will explain you how to open files in C++:
  1. These are a must headers for I/O files programming.
    #include <fstream>
    using namespace std;
  2. This is an infut file stream varialbe.
    ifstream inFile;
  3. To open a file the following syntax must be followed.
    inFile.open("filename.txt"); //A proper path can also be mentioned.
  4. This is a basic way to check whether the file that is being opened does not exists or is being used by some other user, etc.
    if (!inFile) {
        cerr << "Unable to open file filename.txt";
        exit(1);   // call system to stop
    }
  5. To read a file we make use of the while loop.
    
  6. This is important. Once a file is opened it is necessary to close it.

inFile.close();

The following is the brief line of code that allows you to count the number of lines in the file that is opened.

ifstream file("filename.txt");
    while (getline(file, l))  //l is a string variable.
        count++;
    cout << "Numbers of lines in the file : " << count << endl;
The following is a code that explains the opening and closing of files and counting the number of line in the file.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main() 
{
    int sum = 0;
    int x;
    ifstream inFile;
    
    inFile.open("Lab_HW9_2Merge1.txt");
    if (!inFile) {
        cout << "Unable to open file";
        exit(1);
    }
    Count1();
    inFile.close();
    inFile.open("Lab_HW9_2Merge1.txt"); 
    if (!inFile) 
    { 
     cout << "Unable to open file"; 
     exit(1); 
    }
    Count2();
    return 0;
}
int Count1()
{
    int count = 0;
    string line; 
    ifstream file("Lab_HW9_2Merge1.txt");
    while (getline(file, line))
        count1++;
    cout << "1st File : " << count1 << endl;   
return 0; 
}
int Count2()
{
    int count = 0;
    string line; 
    ifstream file("Lab_HW9_2Merge2.txt");
        while (getline(file, line))
             count2++;
       cout << "2nd File : " << count2 << endl;

}

This is a way to create arrays in files:

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
const int SIZE = 20;
int arrayname[SIZE];
int count;
ifstream inputFile;
inputFile.open("filename.txt");
for(count = 0; count < SIZE; count++);
inputFile >> arrayname[count];
inputFile.close();

cout << "The content is: \n";
for(count = 0; count < SIZE; count++);
cout << arrayname[count] << "";
cout << endl;
system("pause");
return 0;
}


Related Solutions

C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
Write a program in C++ using the following 2 Files: Book.h and Source.cpp 1.)   In Book.h,...
Write a program in C++ using the following 2 Files: Book.h and Source.cpp 1.)   In Book.h, declare a struct Book with the following variables: - isbn:   string - title:    string - price: float 2.)   In main (Source.cpp), declare a Book object named: book - Use an initialization list to initialize the object with these values:           isbn à 13-12345-01           title à   “Great Gatsby”           price à 14.50 3.)   Pass the Book object to a function named: showBook - The...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
Question 1: Write a program in C++ using multi filing which means 3 files ( main...
Question 1: Write a program in C++ using multi filing which means 3 files ( main file, header file, and functions file) and attached screenshots as well. Attempt the Question completely which contain a and b parts a) Write a program that takes a number from the user and checks whether the number entered validates the given format: “0322-5441576”, xxxx-xxxxxxx where “x” implies the digits only and first two digits are 0 and 3 respectively. The program also identifies the...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements b) Inputs an integer n from 1-30 from the keyboard. If n < 1 set n = 1. If n > 30 set n = 30. the program should keep asking the user the input n one by one, followed by printing of the value of n (n=n if bigger than 1 and smaller than 30, 1 if smaller than 1 and 30 if...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each of 2 integer arrays. Multiply corresponding array elements, that is, arrayOne[0] * arrayTwo[0], etc. Save the product into a third array called prodArray[ ]. Display the product array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT