4. Write a program that creates an array of 100 string objects. Fill the array b
ID: 3842572 • Letter: 4
Question
4. Write a program that creates an array of 100 string objects. Fill the array by having your program open a (text) file and read one line of the file into each string until you have filled the array. Display the array using the format “line #: ,” where # is the actual line number (you can use the array counter for this value) and is the stored string.
5. Create a makefile for one of the exercises in this assignment (your choice) that allows the user to type make for a production build of the program and make debug for a build of the program that includes debugging information.
please answer 4 and 5
Explanation / Answer
Hi,
I have answered both part of the questionswhere in first question file has to be read and stored in array line by line and later to be displayed line by line displaying line number also.
Proper comments are added:-
Code:-fileReadWrite.CPP
=====================================================================================
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string fileData[100];//Will store files data
int l;
int i=0;
std::ifstream is ("input.txt",std::ifstream::in);//I have used input.txt file.You can use your own file.
if (is)
{
//will go to end of file and get the length again set the indicator to start of file.
is.seekg (0, is.end);
l =is.tellg();
is.seekg (0, is.beg);
}
//Read file till it reacches end of file and store in array of strings fileData[]
while(! is.eof())
{
getline (is,fileData[i]);
i++;
}
//Printing the data on the console line by line displaying the line number and line data
for(int j=0;j<=l;j++)
{
cout<<"Line:"<<j<<""<<fileData[j]<<endl;
}
}
=======================================================================================
The Answer for second question is:-
The program name is fileReadWrite.
MAKEFILE:-
=====================================================================================
# the compiler: define as g++ for C++
CC = g++
# compiler flags:
# -g adds debugging information to the executable file
# -Wall turns on most, but not all, compiler warnings
CFLAGS = -g -Wall
# the build target executable:
TARGET = fileReadWrite
all: $(TARGET)
$(TARGET): $(TARGET).c
$(CC) $(CFLAGS) -o $(TARGET) $(TARGET).c
clean:
$(RM) $(TARGET)
=====================================================================================
Please let me know in case any clarification is required.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.