为什么不能将C ++字符串用作打开文件但C字符串可以的路径名? [英] Why can't a C++ string be used as the path name to open a file but a C-string can?

查看:111
本文介绍了为什么不能将C ++字符串用作打开文件但C字符串可以的路径名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我可以使用C字符串而不是C ++字符串打开文件?


// C字符串工作

字符路径[15 ];

strcpy(路径," c:\ test.txt");

ifstream infile(路径);


// C ++字符串类没有

字符串路径(" c:\ test.txt");

ifstream infile(路径);


为什么?有什么我可以做的事情来使用字符串(也许是重铸??)?

Why can I open a file using a C-string but not a C++ string?

//C-strings works
char path[15];
strcpy(path,"c:\test.txt");
ifstream infile(path);

//C++ string class does not
string path("c:\test.txt");
ifstream infile(path);

Why? Is there something I can do to use a string (maybe a recast??)?

推荐答案



" solartimba" ; < KA ***** @ hotmail.com>在消息中写道

新闻:1a ************************** @ posting.google.c om ...

"solartimba" <ka*****@hotmail.com> wrote in message
news:1a**************************@posting.google.c om...
为什么我可以使用C字符串而不是C ++字符串打开文件?

// C字符串工作
字符路径[15];
strcpy(path," c:\test.txt");
ifstream infile(路径);

// C ++字符串类没有
字符串路径(" c:\test.txt");
ifstream infile(路径);

为什么?有什么我可以做的事情来使用字符串(也许是重铸??)?
Why can I open a file using a C-string but not a C++ string?

//C-strings works
char path[15];
strcpy(path,"c:\test.txt");
ifstream infile(path);

//C++ string class does not
string path("c:\test.txt");
ifstream infile(path);

Why? Is there something I can do to use a string (maybe a recast??)?




这样做:


ifstream infile(path.c_str());



Do this:

ifstream infile(path.c_str());




" solartimba" < KA ***** @ hotmail.com>在消息中写道

新闻:1a ************************** @ posting.google.c om ...

"solartimba" <ka*****@hotmail.com> wrote in message
news:1a**************************@posting.google.c om...
为什么我可以使用C字符串而不是C ++字符串打开文件?

// C字符串工作
字符路径[15];
strcpy(path," c:\test.txt");


strcpy(路径," c:\\test.txt");

ifstream infile(路径);

// C ++字符串类没有
字符串路径(" c:\test.txt");


字符串路径(" c:\\test.txt");


(查找''转义字符'' )。

ifstream infile(路径);

为什么?有什么我可以做的事情来使用字符串(也许是重铸??)?
Why can I open a file using a C-string but not a C++ string?

//C-strings works
char path[15];
strcpy(path,"c:\test.txt");
strcpy(path,"c:\\test.txt");
ifstream infile(path);

//C++ string class does not
string path("c:\test.txt");
string path("c:\\test.txt");

(look up ''escape character'').
ifstream infile(path);

Why? Is there something I can do to use a string (maybe a recast??)?




ifstream infile(path.c_str());


注意''c_str()''返回一个指向* const * chars的指针,

所以不要试图改变这些字符。

-Mike



ifstream infile(path.c_str());

Note that ''c_str()'' returns a pointer to *const* chars,
so don''t try to change those characters.

-Mike


" solartimba" < KA ***** @ hotmail.com>在消息中写道

新闻:1a ************************** @ posting.google.c om ...
"solartimba" <ka*****@hotmail.com> wrote in message
news:1a**************************@posting.google.c om...
为什么我可以使用C字符串而不是C ++字符串打开文件?

// C字符串工作
字符路径[15];
strcpy(path," c:\test.txt");
ifstream infile(路径);

// C ++字符串类没有
字符串路径(" c:\test.txt");
ifstream infile(路径);

为什么?有什么我可以做的事情来使用一个字符串(也许是重铸?)?
Why can I open a file using a C-string but not a C++ string?

//C-strings works
char path[15];
strcpy(path,"c:\test.txt");
ifstream infile(path);

//C++ string class does not
string path("c:\test.txt");
ifstream infile(path);

Why? Is there something I can do to use a string (maybe a recast??)?




当然你可以使用c_str(),因为其他人说,但它似乎我你

应该能够使用std :: string来获取

标准库中的任何字符串参数。让我们希望标准的下一个版本修复此问题。


-

Cy
http://home.rochester.rr.com/cyhome/


这篇关于为什么不能将C ++字符串用作打开文件但C字符串可以的路径名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆