ifstream to FILE*

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vermarajeev
    New Member
    • Aug 2006
    • 180

    ifstream to FILE*

    Hi,
    Is there a way to convert ifstream to FILE*;

    Something like this

    Code:
    ifstream ifs( " test.txt ");
    if( !ifs.is_opsn() )
       return 0;
    
    FILE* f = ifs; //how can I convert ifs to FILE*
    Thanks
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Originally posted by vermarajeev
    Hi,
    Is there a way to convert ifstream to FILE*;

    Something like this

    Code:
    ifstream ifs( " test.txt ");
    if( !ifs.is_opsn() )
       return 0;
    
    FILE* f = ifs; //how can I convert ifs to FILE*
    Thanks
    Convert it to a file descriptor using .fd() and then to a FILE stream using fdopen().

    See here for more detailed information.


    Adrian

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Why???

      an ifstream is already a FILE*. If you look deep enough in the template you will find it.

      The whole point of streams is to hide the FILE* so the stream can be used with the various operators in an object-oriented fashion.

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by weaknessforcats
        Why???

        an ifstream is already a FILE*. If you look deep enough in the template you will find it.

        The whole point of streams is to hide the FILE* so the stream can be used with the various operators in an object-oriented fashion.
        There are many reasons, including (but not limited to) interfacing with legacy code.


        Adrian

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Good point. I forgot about the legacy code. Time for more coffee.

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by weaknessforcats
            Good point. I forgot about the legacy code. Time for more coffee.
            Ha,ha.memory leak I would say.

            PS:Subscribing( Intresting thread)

            Savage

            Comment

            • vermarajeev
              New Member
              • Aug 2006
              • 180

              #7
              Originally posted by AdrianH
              Convert it to a file descriptor using .fd() and then to a FILE stream using fdopen().

              See here for more detailed information.


              Adrian
              Did you try the code, it dont work for me. I tried this

              Code:
              int main ( )
              {
              	fstream fs( "test.txt", ios::in );
              	if( fs.is_open() )
              	  return 0;		
              	FILE* f = fs.fd(); 
              	return 0;
              }
              I get this error
              error C2039: 'fd' : is not a member of 'std::basic_fst ream<_Elem,_Tra its>'
              I need FILE pointer from a C++ stream because, I need to lock the file and I came across this function
              Code:
              void _lock_file(
                 FILE* file
              );
              which takes FILE* but in my application I make use of C++ stream to read and write to a file which I cannot change.

              Can you correct me if I'm doing something wrong above....
              Thanks for your understanding

              Comment

              • vermarajeev
                New Member
                • Aug 2006
                • 180

                #8
                So far so good.
                I have written a sample to lock a file on windows. On linux there are some problems. Please help me to make the below code work on linux too.

                Code:
                      int main ( )   
                      {   
                          FILE    *pFile = NULL;   
                          char    *fileName = "Logfile.log";   
                   
                          // Open the file in write mode   
                         #ifdef win32   
                          fopen_s(&pFile, fileName ,"r");   
                         #else  
                          pFile = fopen( fileName ,"r");     
                  
                          if (!pFile)  
                          {  
                              printf("Error opening file %s!\n", fileName);  
                              exit(1);  
                          }    
                  
                          // Lock the file.  
                         #ifdef win32  
                             _lock_file(pFile);  
                         #else  
                             //lock file on linux            
                  
                           printf("Locking the file %s.\n", fileName);      
                  
                          ifstream ifs( pFile );  //This doesnt work on linux why???  
                          char line[255];  
                          ifs.seekg( 0 );  
                          while( !ifs.eof() )  
                          {  
                              ifs.getline( line, sizeof( line ) );  
                              cout<<line;  
                          }  
                          return 0;  
                      }

                Comment

                • vermarajeev
                  New Member
                  • Aug 2006
                  • 180

                  #9
                  Sorry, there are #endif missing

                  Comment

                  • AdrianH
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1251

                    #10
                    It would appear that the functions I linked to you are not standard. Sorry.


                    Adrian

                    Comment

                    Working...