Rate My Code: Function Usage & File Streaming

Started by
21 comments, last by Leiarchy9 20 years, 3 months ago

#include <iostream>
#include <fstream>

#include <cstdlib>
#include <cstring>

#define MAX        256

   using std::ofstream;
   using std::ifstream;
   using std::cout;
   using std::cerr;
   using std::cin;
   using std::endl;
   using std::string;



void FilePrint( ofstream& output, string strFile, string strText )
{

     output.open( strFile.c_str() );
     
     
     if( !output.is_open() )
     {
     
        cerr << "Unable To Open: " << strFile.c_str() << endl;
        exit(1);
     }
     
     else
     {
     
        output << strText.c_str() << endl;
     }
     
     
     output.close();
}


void FileGet( ifstream& input, string strFile, char *cBuffer, const int SIZE )
{

     input.open( strFile.c_str() );
     
     
     if( !input.is_open() )
     {
     
        cerr << "Unable To Open: " << strFile.c_str() << endl;
        exit(1);
     }
     
     else
     {
     
        while( !input.eof() )
        {
        
           input.getline( cBuffer, SIZE );
           cout << cBuffer << endl;
        }
        
     }
     
     
     input.close();
}


int main( int argc, char *argv[] )
{

     ofstream fout;
     ifstream fin;
     
     char Buffer[MAX];
     

     FilePrint( fout, "Example.txt", "Hello World!!\nHow Are You Today?!" );
     FileGet( fin, "Example.txt", Buffer, MAX );


     cin.get();
     return EXIT_SUCCESS;

}
On a scale of 1 - 10. If you see something that *disturbs* you then tell me what I should improve on. -------- "Do you believe in Ghost?"
--------"Do you believe in Ghost?"
Advertisement
I don''t understand your need for others to rate your code.
You should stick to your own style, not have 50 random people telling you different things and confusing you.
quote: exit(1);


:/
I agree, you should stick with yor own style until you get a job in the field where you have to work with a team. Then you will have to adapt to their coding style to stick with the comapny standards. As long as your code is clean, formatted nicely, and works good, then there should be no problem.

-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"
Too much whitespace, try sticking everything on one line like this.


int x=12;if(x!=13){printf("donuts are great\n");if(x!=v)break;};printf("end of the function");


EDIT: actualy, it looks pretty good. Keep up the good work.

[edited by - paulcesar on December 26, 2003 5:30:49 PM]
quote:
I agree, you should stick with yor own style until you get a job in the field where you have to work with a team. Then you will have to adapt to their coding style to stick with the comapny standards. As long as your code is clean, formatted nicely, and works good, then there should be no problem.


What if I develop poor coding style during my amatuer years and carry that over to my professional years? I need some expert(or intermediate) coders to rate my code and give me some suggestions that could possibly better my coding and erase some of my poor habits.

--------
"Do you believe in Ghost?"
--------"Do you believe in Ghost?"
I''d say the major thing missing from your code is comments, and that is what will really bother people as your code complexity advances.
i agree on the commments thing...
i''d suggest giving MAX a more meaningful name and making it a constant (integer, ie) instead of a #define.

but i''d also say that your example code is too contrived and meaningless to really pass a judgement.
Just curious as to why you are passing references to streams when you open them in the function.

EDIT: Another thing I noticed, is you read in using a c-string, but output using a stl string...

[edited by - Krumble on December 27, 2003 2:17:18 AM]
Kevin.
ismycodehotornot.com

lol

This topic is closed to new replies.

Advertisement