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...
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,...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards of each type of card are in the file. credit_cards.txt contains the following data: John Smith, Discover Helen Jones, Visa Jerry Jones, Master Card Julio Jones, Diners Club Fred Jones, Diners Club Anthony Rendon, Platinum Visa Juan Soto, Platinum Visa George Jones, American Express Brandon Allen, Visa Henry Beureguard, Visa Allen Jackson, Master Card Faith Hill, Platinum Visa David Smith, Master Card Samual Jackson,...
Write a program that will read in from input file one line at a time until...
Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces, commas and periods....
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...
C Programming file.c takes in one input argument that denotes a file to be read. It...
C Programming file.c takes in one input argument that denotes a file to be read. It needs to convert the contents of that file into a character array (char *) and then into a an unsigned character array (unsigned char *). Please fix and or complete the program, and explain any of the changes too: ---- #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { FILE *f; f = fopen(argv[1], "r"); if( !f ) { exit(1); }...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT