Question

In: Computer Science

Done in c++ Read this file one time to determine how many records it contains. Be...

Done in c++

  1. Read this file one time to determine how many records it contains. Be sure to check for a successful file open here.
  2. Close the file.
  3. Allocate memory dynamically to store the data from step 1. This dynamically allocated memory should be for an array of strings. The array size should be exactly the number of records from step 1.
  4. Reopen the file and read it a second time, storing each record into the array of strings from step 3.
  5. Print the first and last element in the array.
  6. Release the allocated memory.
  7. Your program output should look like this:
four score and seven years ago our fathers brought forth on this continent a new
earth

The file that you should read:

four score and seven years ago our fathers brought forth on this continent a new
nation conceived in liberty and dedicated to the proposition that all men are
created equal
now we are engaged in a great civil war testing whether that nation or any nation
so conceived and so dedicated can long endure we are met on a great battlefield of
that war we have come to dedicate a portion of that field as a final resting place
for those who here gave their lives that that nation might live it is altogether fitting
and proper that we should do this
but in a larger sense we can not dedicate we can not consecrate we can not
hallow this ground the brave men living and dead who struggled here have
consecrated it far above our poor power to add or detract the world will little
note nor long remember what we say here but it can never forget what they did
here it is for us the living rather to be dedicated here to the unfinished work which
they who fought here have thus far so nobly advanced it is rather for us to be here
dedicated to the great task remaining before us that from these honored dead we
take increased devotion to that cause for which they gave the last full measure of
devotion that we here highly resolve that these dead shall not have died in vain
that this nation under god shall have a new birth of freedom and that
government of the people by the people for the people shall not perish from the
earth

Solutions

Expert Solution

Source Code:

#include<iostream>
#include<fstream>

using namespace std;

int main () {
//Step 1:
    ifstream file("InputFile.txt");//opening input file first time. Note: change the input file name as you want.
    if(!file.is_open())//Checking whether the file is opened correctly or not.
    {
        cout<<"Error opening File."<<endl;
        exit(1);
    }
    string str;
    int lineCount=0;//variable to keep the count of number of lines in the file.
    while(getline(file, str))//reading each line with str variable until end of file is reached.
    {
        lineCount++;// incrementing lineCount while reading each line at a time.
    }
    file.close();//closing the file.
//Step 2:
    string *fileData=new string[lineCount]; // dynamically allocating memory of string array  of the size of lineCount.
//Step 3:
    ifstream file1("InputFile.txt");//opening input file second time. Note: change the input file name as you want.
    if(!file1.is_open())
    {
        cout<<"Error opening File."<<endl;//gives error message when the file is not present.
        exit(1);
    }
    int index=0; //index to store the current line.
    while(getline(file1,str) && index<lineCount)// read each line from the file until reaches the end of file and also checking the index whether it exceeds the line lineCount.
    {
        fileData[index]=str; // storing the current line to the fileData at the position of index. 
        index++;//incrementing index to point the next memory location.
        str.clear();// clearing str data.
    }
    file1.close();//closing the file.
//Step 4:
    cout<<fileData[0]<<endl;// printing first line.
    cout<<fileData[lineCount-1]<<endl;// printing last line.
//Step 5:
    delete[] fileData;//deallocating the fileData to freeup space.
    return 0;
}

Input File Data:

Output:


Related Solutions

Done in c++, Read an unsorted keywords file once to determine how many words are in...
Done in c++, Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check...
Read an unsorted keywords file once to determine how many words are in the file. Allocate...
Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check your sorted array...
I need C++ program that Read an input file of text.txt one word at a time....
I need C++ program that Read an input file of text.txt one word at a time. The file should consist of about 500 words. The program should remove all punctuations,keep only words. Store the words in a built-in STL container, such as vector or map.Can someone help with any additional comments that I can understand the logic?thank you
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
Using Python. A file exists on the disk named students.txt. The file contains several records, and...
Using Python. A file exists on the disk named students.txt. The file contains several records, and each record contains two fields: (1) the student’s name, and (2) the student’s score for the final exam. Write code that deletes the record containing “John Perz”as the student name. This the code i have so far but it's not working: import os def main(): found=False search='John Perz' student_file = open('student.txt','r') temp_file = open('temp_students.txt','w') name=student_file.readline() score='' while name !='': score=student_file.readline() name=name.rstrip('/n') score=score.rstrip('/n') if name...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file has unknown number of records of inventory items, but no more than 100; one record per line in the following order: item ID, item name (one word), quantity on hand , and a price All fields in the input file are separated by a tab (‘\t’) or a blank ( up to you) No error checking of the data required Create a menu which...
How to read the given structure from a random CSV file separated by commas(which contains no...
How to read the given structure from a random CSV file separated by commas(which contains no headers only the values of the contents of the structure) and then insert in a binary search tree using one of the structure contents as a key i.e. datetime and handle duplicates in binary search tree by implementing link_list.Please develop a C code for this. struct data{ char biker_id[200]; char distance_bike_travelled[200]; char datetime[200]; char count_tripr[200]; }
Read from a file that contains a paragraph of words. Put all the words in an...
Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will contain a sentence with words separated...
Write a program that processes numbers, corresponding to student records read in from a file, and...
Write a program that processes numbers, corresponding to student records read in from a file, and writes the required results to an output file (see main ( )). Your program should define the following functions: double read_double (FILE *infile) — Reads one double precision number from the input file. Note: You may assume that the file only contains real numbers. int read_integer (FILE *infile) - Reads one integer number from the input file. double calculate_sum (double number1, double number2, double...
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT