not sure how to do this in c using ifstream and ostream Re

not sure how to do this in c++ using ifstream and ostream

/**

* Requires: Nothing.

* Modifies: cin, drawer.

* Effects:

* Opens a file

* Start with a blank canvas (drawer)

* Start reading from file. For each line....

* Read the 1st character to determine shape

* Read the shape: L reads a line, C reads a circle, T read a triangle

* R reads a rectangle.

* For any other character, clears drawer and prints

* \"Error in input file: \" << [character already read]

* << [all chars remaining on the line] << endl;

* Draw shape on canvas

* Close file

* Print \"[Loaded filename]\"

*/

void loadFile(Graphics& drawer);

Solution

I am not so sure which solution do you want but I will try to provide basic so that you understand how you could do that. As far as I can know from your question is that you can implement it but don\'t know how to do it using ifstream and ofstream.

#include <iostream>
#include <fstream>
using namespace std;

int main(){

   ifstream fin;
   fin.open(\"input.txt\");
   if(!fin){
       cout << \"Error in reading the file\" << endl;
exit(1);

}
   char x;
   int r;
   fin >> x;
   if(x == \'C\'){
       //as it is a circle and I am guessing you need radius to draw it with taking orgin as center
       fin >> r;
   }

   cout << x << \" is a Circle with radius \" <<r << endl;

   fin.close();
   return 0;
}

Above code read the input from file input.txt(put that in same directory or provide the full path in fin.open(filename)).

I have put two input in the file input.txt in one line -

C 4

Similarly you can do this for ofstream.

#include <iostream>
#include <fstream>
using namespace std;

int main(){

   ifstream fin;
   ofstream fout;
   fin.open(\"input.txt\");
   fout.open(\"output.txt\");
   if(!fin){
       fout << \"Error in reading the file\" << endl;
exit(1);
   }
   if(!fout){
       cout << \"Can\'t write to the specified file\" << endl;
       exit(1);
   }
   char x;
   int r;
   fin >> x;
   if(x == \'C\'){
       //as it is a circle and I am guessing you need radius to draw it with taking orgin as center
       fin >> r;
   }

   fout << x << \" is a Circle with radius \" <<r << endl;

   //dont forget to close the stream.
   fin.close();
   fout.close();
   return 0;
}

Same thing as above just create an object of ofstream class which will write the output to the file output.txt instead of writing to your console or terminal. And remember always close the stream. Now take the input and assign the value to drawer.

Note- always check if file is present or not otherwise you will get exception and do exit(1) or return fromt there. But ofstream create file for you if it is not present except in the weird disk error situation where creating the file not allow.

There is another way in which you can overload the << and >> operator which I don\'t think you need it now. Try to implement it and ask me if you get stuck.

not sure how to do this in c++ using ifstream and ostream /** * Requires: Nothing. * Modifies: cin, drawer. * Effects: * Opens a file * Start with a blank canva
not sure how to do this in c++ using ifstream and ostream /** * Requires: Nothing. * Modifies: cin, drawer. * Effects: * Opens a file * Start with a blank canva

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site