473,756 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const string & to fopen..

Jae
Real(const string &fileName)
{

FILE * myInputFile = fopen(fileName, "rt");

.....

fclose(myInputF ile);
}
I'd like to read a file names from fileName. However, I always got an error

error: cannot convert `const std::basic_stri ng<char, std::char_trait s<char>,
std::allocator< char> >' to `const char*' for argument `1' to `FILE*
fopen(const char*, const char*)'

How can I fix it ?

I tried

fopen((const char) fileName, "rt");

but it shows

error: `const struct std::basic_stri ng<char, std::char_trait s<char>,
std::allocator< char> >' used where a `const char' was expected
Can anyone help me?
May 2 '06 #1
5 8836
Jae wrote:
Real(const string &fileName)
{
FILE * myInputFile = fopen(fileName, "rt");
fclose(myInputF ile);
}
I'd like to read a file names from fileName. However, I always got an error

error: cannot convert `const std::basic_stri ng<char, std::char_trait s<char>,
std::allocator< char> >' to `const char*' for argument `1' to `FILE*
fopen(const char*, const char*)'

How can I fix it ?
fopen() expects a const char*, not a std::string (if that's what
"string" is). Just do

fopen(fileName. c_str(), "rt");
I tried

fopen((const char) fileName, "rt");


No. First, fopen() expects a const char* (a string), not a const char
(a character). Second, you cannot convert a std::string to a const
char.
Jonathan

May 2 '06 #2
Jae
Thank you and I can get as a line.
Well, but in this case, it reads only one line of the file. How can I read
whole file?
I'd like to read it as a string..
FILE* ifs = fopen(fileName. c_str(), "rt" );
if(!ifs)
{
cout<<"Cannot open a file"<<endl;
return;
}

fgets( totalString, 1000, ifs );

string myString;
for(int i = 0; i < 1000; i++){
if(!totalString[i]) break;
myString += totalString[i];
cout<<totalStri ng[i];
}

It prints only one line, even though the file input has more than 10 lines.
Is my code wrong?


"Jonathan Mcdougall" <jo************ ***@gmail.com> wrote in message
news:11******** *************@v 46g2000cwv.goog legroups.com...
Jae wrote:
Real(const string &fileName)
{
FILE * myInputFile = fopen(fileName, "rt");
fclose(myInputF ile);
}
I'd like to read a file names from fileName. However, I always got an
error

error: cannot convert `const std::basic_stri ng<char,
std::char_trait s<char>,
std::allocator< char> >' to `const char*' for argument `1' to `FILE*
fopen(const char*, const char*)'

How can I fix it ?


fopen() expects a const char*, not a std::string (if that's what
"string" is). Just do

fopen(fileName. c_str(), "rt");
I tried

fopen((const char) fileName, "rt");


No. First, fopen() expects a const char* (a string), not a const char
(a character). Second, you cannot convert a std::string to a const
char.
Jonathan

May 2 '06 #3
Jae wrote:
Thank you and I can get as a line.
Well, but in this case, it reads only one line of the file. How can I read
whole file?
I'd like to read it as a string..
FILE* ifs = fopen(fileName. c_str(), "rt" );
if(!ifs)
{
cout<<"Cannot open a file"<<endl;
return;
}

fgets( totalString, 1000, ifs );

string myString;
for(int i = 0; i < 1000; i++){
if(!totalString[i]) break;
myString += totalString[i];
cout<<totalStri ng[i];
}

It prints only one line, even though the file input has more than 10 lines.
Is my code wrong?


Yes, fgets() stops reading after a newline. This may be of help:

# include <iostream>
# include <sstream>
# include <string>
# include <fstream>

int main()
{
std::ifstream ifs("file");

std::ostringstr eam oss;
oss << ifs.rdbuf();
std::string s(oss.str());

std::cout << s; // outputs whole file
}
Jonathan

May 2 '06 #4
Jae
I got it. Thanks a lot !!
"Jonathan Mcdougall" <jo************ ***@gmail.com> wrote in message
news:11******** *************@v 46g2000cwv.goog legroups.com...
Jae wrote:
Real(const string &fileName)
{
FILE * myInputFile = fopen(fileName, "rt");
fclose(myInputF ile);
}
I'd like to read a file names from fileName. However, I always got an
error

error: cannot convert `const std::basic_stri ng<char,
std::char_trait s<char>,
std::allocator< char> >' to `const char*' for argument `1' to `FILE*
fopen(const char*, const char*)'

How can I fix it ?


fopen() expects a const char*, not a std::string (if that's what
"string" is). Just do

fopen(fileName. c_str(), "rt");
I tried

fopen((const char) fileName, "rt");


No. First, fopen() expects a const char* (a string), not a const char
(a character). Second, you cannot convert a std::string to a const
char.
Jonathan

May 2 '06 #5
Jae wrote:
"Jonathan Mcdougall" <jo************ ***@gmail.com> wrote in message
Jae wrote:
Real(const string &fileName)
{
FILE * myInputFile = fopen(fileName, "rt");
fclose(myInputF ile);
}
I'd like to read a file names from fileName. However, I always got an
error

error: cannot convert `const std::basic_stri ng<char,
std::char_trait s<char>,
std::allocator< char> >' to `const char*' for argument `1' to `FILE*
fopen(const char*, const char*)'

How can I fix it ?
fopen() expects a const char*, not a std::string (if that's what
"string" is). Just do

fopen(fileName. c_str(), "rt");

I got it. Thanks a lot !!


Glad you did, but please do not top-post next time
(http://en.wikipedia.org/wiki/Top-post).
Jonathan

May 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
9407
by: selder21 | last post by:
Hello, I have a class with constructor taking a const string&. Now i want to call this constructor with a string literal. Because this is of type char* there are overload resolution conflicts. If i make another constructor with parameter const char*, how can i call the constructor with the const string& ? I tried
16
2478
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I could set that qualifier in the function call. Can that be done? #include <iostream> using std::ostream; using std::cout;
1
3052
by: tggfang | last post by:
Please, anybody can tell me what is the real meaning of this const String& str thanks
1
4118
by: Erik Tamminga | last post by:
Hi, I'm totally bluffed: how can a 'public const string name = "myname";' ever evaluate to null? I have the following class: public class MIB2 { public const string org = "1.3";
10
14486
by: lchian | last post by:
Hi, For two stl strings s1 and s2, I got different results from strcmp(s1.c_str(), s2.c_str()) and s1.compare(s2) can someone explain what these functions do? It seems that strcmp gives the "right" (i.e.wysiwyg) answer while compare() does not.
3
1795
by: sernamar | last post by:
Hi, I have the following base class class UnitOfMeasure { public: //... std::string& GetName() {return _uomName;}; //... protected:
7
2585
by: Zytan | last post by:
I am trying to set a const string made up of Chr(), but if the value passed into Chr() is too large, the compiler complains that it is not constant!! Example: Public Const m_Data As String = Chr(&HD) & Chr(&HE) 'ok Public Const m_Data As String = Chr(&HD0) & Chr(&HE0) 'not ok But, Char stores 0..255, so it should be ok in both cases. What's going on?
8
2048
by: Ook | last post by:
I have a function getStuff, and two choices of implementation: const string *getStuff() { return &_stuff; } or const string getStuff()
2
10135
by: wizofaus | last post by:
Given the following code: public class Test { static unsafe void StringManip(string data) { fixed (char* ps = data) ps = '$'; }
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.