In: Computer Science
language c++(Data structure) You have to read a file and store a data in character array ( you cant initialize a character array, you have to code generically) code must be generic u must read a file onece u cant use built in function etc string, code in classes if u initialized a char array or ur code doesn't run i will dislike and report u
you can use link list to store data
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
#include <iostream>
#include <fstream>
using namespace std;
//struct node for link
template<typename Type>
struct node
{
Type data;
node *next;
};
//single list
template<typename Type>
class list
{
private:
node<Type> *head, *tail;
public:
list()
{
head=NULL;
tail=NULL;
}
void add(Type value)
{
node<Type> *temp=new
node<Type>;
temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
temp=NULL;
}
else
{
tail->next=temp;
tail=temp;
}
}
//display link
void display()
{
cout<<"\nItems : ";
node<Type> *temp=new
node<Type>;
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
};
int main()
{
list<char> myQueue;
ifstream infile;
string line;//for read line
string fileName;
cout<<"Enter file name : ";
cin>>fileName;
cout<<"Loading the
file........"<<endl;
infile.open (fileName); //name of file here. plz
mention Complete path if file is not at root
if (infile.is_open()) //if file opened
{
char c;
while(infile>>c ) { //get row
from text file
myQueue.add(c);
}
infile.close(); //close file
cout<<"File scan
done........"<<endl;
}
else //if file not found show the below message
{
cout << "Sorry, we could not
find the file." << endl;
}
cout<<"*************************************\n\n";
cout<<"Content of the file is :
"<<endl;
myQueue.display();
return 0;
}
numbers.txt
1 2 3
3 4 5
5 6 7
7 8 9
output