473,729 Members | 2,086 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 8834
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
9406
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
2471
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
4117
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
14484
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
1793
by: sernamar | last post by:
Hi, I have the following base class class UnitOfMeasure { public: //... std::string& GetName() {return _uomName;}; //... protected:
7
2582
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
2046
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
10126
by: wizofaus | last post by:
Given the following code: public class Test { static unsafe void StringManip(string data) { fixed (char* ps = data) ps = '$'; }
0
8928
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9428
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
9291
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...
0
6026
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
4535
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...
0
4799
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3244
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2698
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.