Results 1 to 10 of 10

Thread: Open a file

  1. #1

    Thread Starter
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    Ok, i fugured out how to create a .txt file, but what i can't figure out is how to open one. I created the text file by using fstream.

    Just something simple, if that is possible, i just would like to be able to open a .txt file from a path that the user enters, and then save the contents of the text file as a string so that i can print it out with cout.

    Thanks

  2. #2
    Guest
    PHP Code:
    #include <fstream>
    #include <cstdio>
    #include <string>
    using namespace std;
    const 
    READ 0;
    const 
    WRITE 1;
    /*
    ios::app -- Opens the file, and allows additions at the end
    ios::ate -- Opens the file, but allows additions anywhere
    ios::trunc -- Deletes everything in the file
    ios::nocreate -- Does not open if the file must be created
    ios::noreplace -- Does not open if the file already exists
    */
    const ate 4;
    const 
    app 8;
    const 
    trunc 16;
    const 
    nocreate 32;
    const 
    nereplace 64;
    class 
    file


        
    {
        public:
        
    file();
        ~
    file();
        
    void openfile(string filenameint readwriteint mode);
        
    void closefile(int readwrite);
        
    void writefile(string text);
        
    void renamefile(string fromstring to);
        
    void deletefile(string filename);
        
    string getcontents();
        protected:
        
    ifstream ifile;
        
    ofstream ofile;
    };
    file::file()


        {
    }
    file::~file()


        {
    }
    void file::openfile(string filenameint readwriteint mode nocreate)


        {
            switch (
    readwrite)


                {
                    case 
    READ:
                        
    ifile.open(filename.c_str(), mode);
                        break;
                    case 
    WRITE:
                        
    ofile.open(filename.c_str(), mode);
                        break;
                }
        }
        
    void file::closefile(int readwrite)


            {
                switch (
    readwrite)


                    {
                        case 
    READ:
                            
    ifile.close();
                            break;
                        case 
    WRITE:
                            
    ofile.close();
                            break;
                    }
            }
            
    string file::getcontents()


                {
                    
    string str "";
                    
    char f;
                    while(
    ifile.get(f))
                        
    str += f;
                    return 
    str;
            }
            
    void file::writefile(string text)


                {
                    
    ofile << text;
            }
            
    void file::renamefile(string fromstring to)


                {
                    
    rename(from.c_str(), to.c_str());
            }
            
    void file::deletefile(string filename)


                {
                    
    remove(filename.c_str());
            } 
    I wrote this to helpp me :)

    use it like this:

    PHP Code:
    file thefile;
    string thestr;
    cout << "filename: ";
    cin >> thestr;
    thefile.openfile(thestrREAD);
    cout << thefile.getcontents();
    thefile.closefile(READ); 

    :D

  3. #3

    Thread Starter
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    thanks dennis. By the way, you saud in a previous post that you had borland c++ builder 5, how can you simply create a console c++ source file using that?

    I try creating a new c++ source file, but it needs to be a part of project to compile, and when i add it to a project, even a console wizard, it always creates it as a windows program. Using winmain(). I just want to use main().

  4. #4

    Thread Starter
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    so like in vc++ you would go file->new->c++ Source File

    what would be the equivilant to that in borland?

  5. #5

    Thread Starter
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    also, is the cstdio include in vc++, because it said that it couldn't find it.

  6. #6
    Guest
    borland would be new->console wizard, and make sure the "console" box is checked...

    and try adding .h to cstdio(btw, cstdio is the same as stdio)(btw again, I looked in borlands include folder, and it's cstdio.h, in msvc++ it's cstdio)

  7. #7

    Thread Starter
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    ahhh... Thanks a lot dennis

  8. #8

    Thread Starter
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    hey dennis, i was just fiddling around, and i found this very simple way to open a text file:

    PHP Code:

    #include <fstream.h>
    #include <stdlib.h>
    const char *FILENAME "FILE.txt";


        
    int main() {

            
    ifstream fin(FILENAME);
            
    char ch;
            while (
    fin.get(ch))
                
    cout << ch;
            
    fin.close();
            
    system("pause");
            return 
    0;

    What i am wondering is what is the difference between this short code, and the long code with the class that you gave me? Don't get me wrong, thanks a lot for the help, i am just wondering like what the actual difference is, and if that short code that i got looks OK?

  9. #9
    Guest
    not much difference at all,

    if you look at the getcontents() function, you will see that the code is very very similar.

    mine just handles the whole shebang rather than just reading

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    In this case, Microsoft are definitely correct, and Borland are wrong.

    cstdio is the correct name for that header - all the c* headers do is to import them into the std namespace along with the STL.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width