Results 1 to 6 of 6

Thread: Is it possible to use one file handle to read and write in different files in c++?

  1. #1
    Join Date
    Aug 2009
    Posts
    56

    Is it possible to use one file handle to read and write in different files in c++?

    Hello friends,
    I am working on small c++ program. In this program I want to read something from one file and I want to write that into another file. I used handle to that particular file, from which I want to read something. Is it possible to use one file handle to read and write in different files in c++? It means can I use only one file handle for both file. Please help me.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Is it possible to use one file handle to read and write in different files in c++?

    No, it is not possible to use one file handle to read and write in different files in c++. A file handle can be used to represent only one file. It means one file handle can not be used on two files at a same time. This is not possible even if you are doing same operation i.e reading information from different file. In this case you have to use different file handle for different files.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Is it possible to use one file handle to read and write in different files in c++?

    You have to use two class such as ofstream and ifstream to perform such type of operation. ofstream Stream class is used to writing on file and ifstream is used for reading from the file. Just look at following example:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
      ofstream file1;
      file1.open ("roman.txt");
      file1 << "Writing this to a another file.\n";
      file1.close();
      return 0;
    }
    In above program I have use file1 for reading information.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Is it possible to use one file handle to read and write in different files in c++?

    It is not possible to use one file handle to read and write in different files in c++. We can only use one file handle to perform reading and writing in the same file. We can not use same file handle on different file. In this case you have to create different file handle for each file. It means create one file handle on one file from which you want to read and create another file handle on other file on which you want to write something.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Is it possible to use one file handle to read and write in different files in c++?

    Hey there is no need to create file handle to read and write in different files in c++. You can easily do this using wrapper function. It is used to write and read the file. It is very easy process. In this when you call wrapper function, this function automatically open the file from which you want to read and after reading it automatically closed. After this it open other file where you want to write something.

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Is it possible to use one file handle to read and write in different files in c++?

    There is no need to use one file handle to read and write in different files. You can use fstream function to do this. Just look at following two steps.

    Step 1 : First Open the file -> now Read the data -> now Close the file.
    You can do this using following code.
    Code:
     #include<fstream.h>
        int main()
        {
            char strs[32500];
            fstream file_op("c:\\yogesh.txt",ios::in);
            while(file_ops >> strs)
            cout <
    < strs ;
     
            file_ops.close();
    
            return 0;
        }
    Step 2 :Now open another file ->now write something to a file -> now close the file. You can do this using following code.

    Code:
    #include <fstream.h>
        int main()
        {
            fstream file_ops("c:\\another.txt",ios::out);
    
            file_ops<<"something Write to file";
            file_ops.close();
            return 0;
        }

Similar Threads

  1. Read and Write a .txt file with Java
    By Mulan in forum Software Development
    Replies: 4
    Last Post: 28-07-2010, 02:03 AM
  2. How to read 2 text files and write it into one
    By Juaquine in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 06:05 PM
  3. File read-write mode in java
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 10:02 AM
  4. How to read and write files in Java
    By BansiJ in forum Software Development
    Replies: 3
    Last Post: 02-09-2009, 08:52 PM
  5. How to read and write Excel files in PHP ?
    By Sudra in forum Software Development
    Replies: 3
    Last Post: 06-05-2009, 05:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,623,927.65774 seconds with 17 queries