Thread: Rate My Code: Function Usage & File Streaming

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    71

    Rate My Code: Function Usage & File Streaming

    Code:
    #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;
    
    }
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >using std::string;
    You want to include <string> instead of <cstring>. <cstring> holds functions for manipulating C-style strings, <string> declares the std::string class and helper functions.

    >void FilePrint( ofstream& output, string strFile, string strText )
    Why do you pass a stream reference to a function that opens it upon being called and closes it before returning? This strikes me as a good place to use a local variable and shorten your argument lists.

    >exit(1);
    The exit function has three standard arguments, 0, EXIT_SUCCESS, and EXIT_FAILURE. Anything else is implementation-defined, so if you want your programs to be portable, use one of the above.

    >output << strText.c_str() << endl;
    Calling c_str() is not needed, C++ streams are well aware of how to output std::strings.

    >output.close();
    This technically isn't needed since the destructor will flush the stream and close it for you.

    >while( !input.eof() )
    This will bite you eventually. Testing for eof is not meant to be used as a loop condition. See the FAQ for further details about why this is so.

    >int main( int argc, char *argv[] )
    The arguments to main are not used, so you should not include them in the definition.

    >char Buffer[MAX];
    Why use a C-string as the buffer when a C++ string would be better in just about every way?

    Your overuse of empty lines makes the code more difficult to read, but at least the indention is fairly consistent.
    My best code is written with the delete key.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >char Buffer[MAX];
    >>Why use a C-string as the buffer when a C++ string would be >>better in just about every way?
    Can we do this, though??
    Code:
    string str;
    stream.getline(str, MAX);
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    How about some prototypes?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can we do this, though??
    No, the string class has its own getline function:
    Code:
    string str;
    getline ( stream, str );
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    Code:
    #include <iostream>     // For C++ Input/Output Stream
    #include <fstream>      // For File Input/Output Stream
    
    #include <cstdlib>      // Standard C Library
    #include <string>      // For std::string
    
       using std::cout;
       using std::cerr;
       using std::cin;
       using std::endl;
       using std::ofstream;
       using std::ifstream;
       using std::string;
       
       
    void FileOutput( string, string );
    void FileInput( string );
    
    
    
    int main( )
    {
    
         FileOutput( "Example.txt", "Hello World!!\nHow Are You Today?!" );
         FileInput( "Example.txt" );
    
    
         cin.get();
         return EXIT_SUCCESS;
    }
    
    
    void FileOutput( string strFile, string strText )
    {
    
         ofstream output( strFile.c_str() );
    
    
         if( !output.is_open() )
         {
    
            cerr << "Unable To Open " << strFile << endl;
            exit( EXIT_FAILURE );
         }
    
         else
         {
    
            output << strText << endl;
         }
    
    }
    
    
    void FileInput( string strFile )
    {
    
         ifstream input( strFile.c_str() );
         string strBuffer;
    
    
         if( !input.is_open() )
         {
    
            cerr << "Unable To Open: " << strFile << endl;
            exit( EXIT_FAILURE );
         }
    
         else
         {
    
            while( !input.eof() )
            {
    
               getline( input, strBuffer );
               cout << strBuffer << endl;
            }
    
         }
    
    }
    Better?
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    getting there. reread preludes post especially the bit pointing you to the FAQ.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    Code:
    #include <iostream>     // For C++ Input/Output Stream
    #include <fstream>      // For File Input/Output Stream
    
    #include <cstdlib>      // Standard C Library
    #include <string>      // For std::string
    
       using std::cout;
       using std::cerr;
       using std::cin;
       using std::endl;
       using std::ofstream;
       using std::ifstream;
       using std::string;
    
    
    void FileOutput( string, string );
    void FileInput( string );
    
    
    
    int main( void )
    {
    
         FileOutput( "Example.txt", "Hello World!!\nHow Are You Today?!\nFine I hope...." );
         FileInput( "Example.txt" );
    
    
         cin.get();
         return EXIT_SUCCESS;
    }
    
    
    void FileOutput( string strFile, string strText )
    {
    
         ofstream output( strFile.c_str() );
    
    
         if( !output.is_open() )
         {
    
            cerr << "Unable To Open " << strFile << endl;
            exit( EXIT_FAILURE );
         }
    
         else
         {
    
            output << strText << endl;
         }
    
    }
    
    
    void FileInput( string strFile )
    {
    
         ifstream input( strFile.c_str() );
         string strBuffer;
         int line= 0;
    
    
         if( !input.is_open() )
         {
    
            cerr << "Unable To Open: " << strFile << endl;
            exit( EXIT_FAILURE );
         }
    
         else
         {
         
            ++line;
    
            while( getline( input, strBuffer ) != NULL )
            {
    
               cout << "Line " << line << ": " << strBuffer << '\n';
               line++;
            }
    
         }
    
    }
    This is perfect.........I hope...
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by KingZoolerius66
    This is perfect.........I hope...
    It's a program -- they are never perfect
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM