473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ifstream to FILE*

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

Something like this

Expand|Select|Wrap|Line Numbers
  1. ifstream ifs( " test.txt ");
  2. if( !ifs.is_opsn() )
  3.    return 0;
  4.  
  5. FILE* f = ifs; //how can I convert ifs to FILE*
  6.  
  7.  
Thanks
May 21 '07 #1
9 15001
AdrianH
1,251 Expert 1GB
Hi,
Is there a way to convert ifstream to FILE*;

Something like this

Expand|Select|Wrap|Line Numbers
  1. ifstream ifs( " test.txt ");
  2. if( !ifs.is_opsn() )
  3.    return 0;
  4.  
  5. FILE* f = ifs; //how can I convert ifs to FILE*
  6.  
  7.  
Thanks
Convert it to a file descriptor using .fd() and then to a FILE stream using fdopen().

See here for more detailed information.


Adrian
May 21 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
May 21 '07 #3
AdrianH
1,251 Expert 1GB
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
May 21 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Good point. I forgot about the legacy code. Time for more coffee.
May 21 '07 #5
Savage
1,764 Expert 1GB
Good point. I forgot about the legacy code. Time for more coffee.
Ha,ha.memory leak I would say.

PS:Subscribing(Intresting thread)

Savage
May 21 '07 #6
vermarajeev
180 100+
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

Expand|Select|Wrap|Line Numbers
  1. int main ( )
  2. {
  3.     fstream fs( "test.txt", ios::in );
  4.     if( fs.is_open() )
  5.       return 0;        
  6.     FILE* f = fs.fd(); 
  7.     return 0;
  8. }
I get this error
error C2039: 'fd' : is not a member of 'std::basic_fstream<_Elem,_Traits>'
I need FILE pointer from a C++ stream because, I need to lock the file and I came across this function
Expand|Select|Wrap|Line Numbers
  1. void _lock_file(
  2.    FILE* file
  3. );
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
May 22 '07 #7
vermarajeev
180 100+
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.

Expand|Select|Wrap|Line Numbers
  1.       int main ( )   
  2.       {   
  3.           FILE    *pFile = NULL;   
  4.           char    *fileName = "Logfile.log";   
  5.  
  6.           // Open the file in write mode   
  7.          #ifdef win32   
  8.           fopen_s(&pFile, fileName ,"r");   
  9.          #else  
  10.           pFile = fopen( fileName ,"r");     
  11.  
  12.           if (!pFile)  
  13.           {  
  14.               printf("Error opening file %s!\n", fileName);  
  15.               exit(1);  
  16.           }    
  17.  
  18.           // Lock the file.  
  19.          #ifdef win32  
  20.              _lock_file(pFile);  
  21.          #else  
  22.              //lock file on linux            
  23.  
  24.            printf("Locking the file %s.\n", fileName);      
  25.  
  26.           ifstream ifs( pFile );  //This doesnt work on linux why???  
  27.           char line[255];  
  28.           ifs.seekg( 0 );  
  29.           while( !ifs.eof() )  
  30.           {  
  31.               ifs.getline( line, sizeof( line ) );  
  32.               cout<<line;  
  33.           }  
  34.           return 0;  
  35.       } 
May 22 '07 #8
vermarajeev
180 100+
Sorry, there are #endif missing
May 22 '07 #9
AdrianH
1,251 Expert 1GB
It would appear that the functions I linked to you are not standard. Sorry.


Adrian
May 22 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is...
6
by: Herv? LEBAIL | last post by:
Hi everybody, I'm writing a program which use the <string>, <vector> and <ifstream> classes. Given an array of string, i.e vector<string> file_names, example : file_names = "file1.txt"...
6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
4
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream...
10
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
1
by: Xiaozhou.Yin | last post by:
Hi~ In the program,I first used the ifstream variable fin open the file and,open it again after called the fin.close().But the fin is fieled to open the file in the second time.The book I'm...
7
by: Boltar | last post by:
Hi I'm using ifstream (which I hardly ever use) to read an ascii text file (containing numbers delimited by newlines) in a loop using something like: ifstream infile("filename") int value; ...
2
by: mpalomas | last post by:
Hi C++ folks, I have trouble to open files whose path contains non-ascii characters with std::ifstream. For instance let's say i just have a file which has Japanese characters either in the...
11
by: adramolek | last post by:
So... I'm trying to get used to using C++ ifstream (or ofstream) instead of stdio (although I'm having second thoughts). Anyways, I want to be able to display a meaningful error message if ifstream...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.