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? 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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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;
|
by: tggfang |
last post by:
Please, anybody can tell me what is the real meaning of this
const String& str
thanks
|
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";
|
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.
| |
by: sernamar |
last post by:
Hi,
I have the following base class
class UnitOfMeasure
{
public:
//...
std::string& GetName() {return _uomName;};
//...
protected:
|
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?
|
by: Ook |
last post by:
I have a function getStuff, and two choices of implementation:
const string *getStuff()
{
return &_stuff;
}
or
const string getStuff()
|
by: wizofaus |
last post by:
Given the following code:
public class Test
{
static unsafe void StringManip(string data)
{
fixed (char* ps = data)
ps = '$';
}
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |