Thread: Read/open file

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    Question Read/open file

    I know I'm a little slow but what is the difference between a readfile and infile.open. I think I know the answer but Here is what I'm trying to do: I want to open a file that has data in it then I need take a hunk of the data and do a cout.
    I think I want to do a readfile(text.tx) then infile.open(text.txt) or something......please help me cuz this book is not helping me!

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I'm not clear about what exactly you mean by a "readfile" and "infile.open", but I'll just tell you a little bit about opening a file with an ifstream object.

    You can open the file either of two ways:
    Code:
    //1:
    ifstream fin("text.txt");
    
    //2:
    ifstream fin;
    fin.open("text.txt");
    Hth.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2
    What is the difference?
    Why would I do one vs the other?

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Well the answer is quite simple. Consider if you were to write an app that accesses more than a single file at different times.
    Code:
    // good
    ifstream fin("fileA.txt");
    //...
    fin.close(;
    fin.open("fileB.txt");
    //...
    fin.close();
    
    // not so good
    ifstream fin("fileA.txt");
    //...
    fin.close();
    // do stuff
    ifstream finAgain("fileB.txt");
    //...
    finAgain.close();
    This is a very simple example that you may not encounter, but consider the case where the ifstream object is a data member of your class. You could explicity open the file with the constructor of your parent class, but what if you want to ask the user for the name of the file? Then it's only possible to call open().

    In short, there is no difference between using the constructor and using open(). It's just a matter of convenience in your app.
    (Side Note: Keep in mind, however, that if you open a file after closing a different one with the same object, you should insure that it is .good() first.)

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I think what Lucky is explaining might be a bit to complicated for the level at which the question is at. This is what I do to open a file:
    Code:
    ifstream inStream;
    inStream.open(textfile.txt);   //opens file in inStream
         //check if file opened correctly:
    if ( inStream.fail() ){
             cout << "error opening file....";
             exit(-1);
    }
    hope this helps...also look at the FAQ it has a section about I/O with files,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    Greetings.

    I'm also a beginner too, but I found some handy functions in C++.

    These are FileOpen, FileCreate, etc. I think if you only want to read from a text file into a string you should use something like this.
    Code:
    int iFileHandle;
    try { iFileHandle = FileOpen(filename, fmOpenRead);
          }
         catch(...)
            { Showmessage("Error");
               goto end;   //weird huh ? :)
              }
           iFileLength=FileSeek(iFileHandle,0,2);
          FileSeek(iFileHandle,0,0); //go to the beginning of the file
          buffer=new char[iFileLength];
          FileRead(iFileHandle,buffer,iFileLength); //reads the file onto the buffer
    It is like this. The buffer has to be a specific type (I don't remember it just now), I think void *char, or something like this, but from the buffer you can put your stream into a String one by one in a for cycle.
    I do it this way, and it works fine. However in the recent past I have the problem that it reads an extra " ' " character to the end of the buffer. I don't know why it does this, but I'm on solving this little piece of problem.
    Good luck!

    Han

  7. #7
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28

    I am doing it like this:

    Code:
    char get;
    ofstream file_ptr;
    file_ptr.open(file, ios::out);
    while(file_ptr.get(get))
    {cout << get;}
    file_ptr.close();
    Hope that was what you was looking for

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM