I need this written in C++ that runs in a Linux environment! Please make the cod
ID: 3737528 • Letter: I
Question
I need this written in C++ that runs in a Linux environment! Please make the code easy to understand!
You must write a C++ program that runs in a Linux environment with the following interface myprog.exe exampled? The file "example list" is just an example -the file can be named anything. This file contains a list of filenames, which we call a filenames file file1.txt file2.txt file3.txt Each one of these files will contain a variable amount of ASCII text. For example, one file can contain this text ese engineering spring has a test wow this is hard 2018 and a second file could contain this text: today is Friday I don't like this Your program must do the following things 1. Create a linked list, open argv1], read each filename, store each filename in the list, and close the file pointer used to read these lines. Each node in this list should contain a second linked list to be used below Once you have completed loading all the filenames from your filenames file, dump the contents of the list to standard out. This must be done after you have loaded all the filenames. This demonstrates that you successfully read the filenames file into memory 2.Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct contents{
string line;
struct contents *next_lines;
};
struct List{
string filename;
struct List *next;
struct contents* fileContents;
};
class linked_list{
private:
struct List *fileListHead;
public:
linked_list()
{
fileListHead = NULL;
}
void add_node(string filename);
void add_contents(struct List *list);
void display();
void display_with_contents();
};
void linked_list::add_node(string filename){
List *node = new List;
node->filename = filename;
node->next = NULL;
if (fileListHead == NULL){
fileListHead = node;
fileListHead->next = NULL;
}
else{
List *temp = fileListHead;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = node;
}
// struct contents *line_ptr;
// node->next_lines = line_ptr;
add_contents(node);
}
void linked_list::add_contents(struct List *node){
struct contents *line_ptr =NULL;
ifstream in((node->filename).c_str());
if(!in) {
cout << "Cannot open input file. " << node->filename <<endl;
return ;
}
string strLine;
while(in) {
getline (in, strLine);
if(in) {
struct contents *newline = new contents;
newline->line = strLine;
newline->next_lines = NULL;
if(line_ptr ==NULL){
line_ptr = newline;
}
else{
struct contents *temp = line_ptr;
while(temp->next_lines != NULL){
temp = temp->next_lines;
}
temp->next_lines = newline;
}
}
}
in.close();
node->fileContents = line_ptr;
}
void linked_list::display()
{
List *temp=new List;
temp=fileListHead;
while(temp!=NULL)
{
cout<<temp->filename<<" ";
temp=temp->next;
}
}
void linked_list::display_with_contents(){
List *temp=new List;
temp=fileListHead;
while(temp!=NULL)
{
cout<<"filename " << temp->filename<<" ";
struct contents *tempContents = temp->fileContents;
while(tempContents!=NULL)
{
cout<< tempContents->line<<" ";
tempContents=tempContents->next_lines;
}
temp=temp->next;
}
cout <<endl;
}
bool fexists(const char *filename)
{
ifstream ifile(filename);
return static_cast<bool>(ifile);
}
int main(int argc, char **argv){
linked_list filelist;
// get arguments from command line argruments
if(argc < 2){
cout << "Please provide arguments for filename" <<endl;
exit(1);
}
// read the filenames
// check if filename exists
// if filename exist add to linked list
for (int i = 1; i < argc; i++){
if (fexists(argv[i])){
// for each linked list , fetch the lines from the line and add them to second linked list
filelist.add_node(argv[i]);
}
}
// loop through main linked list: print the filename and its contents
filelist.display();
filelist.display_with_contents();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.