Question

In: Computer Science

language c++(Data structure) You have to read a file and store a data in character array...

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

Solutions

Expert Solution

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


Related Solutions

C++ question: When cleaning a file the existing data is often read by character. Once a...
C++ question: When cleaning a file the existing data is often read by character. Once a file has been cleaned it is rarely read in by character. Explain Why?
Create a c file to read from an existing file one character at a time and...
Create a c file to read from an existing file one character at a time and print that character to the console Create a c file to read a character from the standard input and write it to a file. please help me
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
Write a C program that will read different data types from the following file and store...
Write a C program that will read different data types from the following file and store it in the array of structures. Given file: (This file have more than 1000 lines of similar data): time latitude longitude depth mag magType nst gap dmin 2020-10-19T23:28:33.400Z 61.342 -147.3997 12.3 1.6 ml 12 84 0.00021 2020-10-19T23:26:49.460Z 38.838501 -122.82684 1.54 0.57 md 11 81 0.006757 2020-10-19T23:17:28.720Z 35.0501667 -117.6545 0.29 1.51 ml 17 77 0.1205 2020-10-19T22:47:44.770Z 38.187 -117.7385 10.8 1.5 ml 15 100.22 0.049 2020-10-19T22:42:26.224Z...
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
C++ Language Write a program that reads the numbers.txt file and stores it into an array...
C++ Language Write a program that reads the numbers.txt file and stores it into an array of integers. Use the sample functions in the text and in the notes from chapter 8 to sort the array in ascending order (low to high). You can use either method (bubble or selection sort) to accomplish this. Then store the sorted array into an output file sorted Numbers.txt. There are 100 values both positive and negative integers in this file.
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
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...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT