将文件转换为char *版本 [英] Converting file to char* Issue
问题描述
我是C ++编程的初学者,我遇到了一个问题。
我想能够将文件的内容转换为char *,我使用文件和字符串流。
这是我执行工作的函数:
<$> c $ c> char * fileToChar(std :: string const& file){
std :: ifstream in(file);
if(!in){
std :: cout<< 错误:文件不存在\\\
;
exit(EXIT_FAILURE);
}
std :: stringstream buffer;
buffer<< in.rdbuf()<< std :: flush;
in.close();
return const_cast< char *>(buffer.str()。c_str());但是,当我通过将方法的内容输出到另一个文件中来测试该方法时,可以使用下面的方法来检查该方法:
}
$ b <这:
std :: ofstream file(test.txt);
file<< fileToChar(fileTest.txt);
我只是得到了很多奇怪的字符:
îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþ[... etc]
究竟是在这里?有什么我错过了吗?
如果有更好的方法,我很高兴知道!
char * fileToChar(std :: string const& file){
这一行已经显示出一些错误的方向。你返回一个指向某个字符串的指针,如果 nullptr
可以返回,等等。
如果你想要一个字符串,那么一定使用 std :: string
!
std :: string fileToChar(std :: string const& file){
return const_cast< char *>(buffer.str .c_str());
另一个应该使所有闹钟关闭的行。 const_cast
总是解决一些潜在的问题(或外部代码的一些问题)。
好的原因为什么东西是 const
。通过强制编译器关闭安全检查并允许其尝试修改不可修改的数据,您通常会将编译错误转换为难以诊断的运行时错误。
如果此功能正常工作,任何修改结果的尝试都将是未定义的行为:
char * file_contents = fileToChar(foo.txt);
file_contents [0] ='x'; // undefined behavior
但它不能正常工作。 buffer.str()
返回临时 std :: string
对象。 c_str()
返回指向该临时对象的内部管理内存的指针。当已经评估完整表达式 return const_cast< char *>(buffer.str()。c_str())
时,对象的生命周期结束。因此,使用生成的指针也是未定义的行为。
问题听起来很复杂,但修复很容易。使函数返回 std :: string
,并将最后一个语句转换为 return buffer.str();
p>
I am a beginner at C++ programming, and I encountered an issue. I want to be able to convert the contents of a file to a char*, and I used file and string streams. However, it's not working.
This is my function that does the work:
char* fileToChar(std::string const& file){
std::ifstream in(file);
if (!in){
std::cout << "Error: file does not exist\n";
exit(EXIT_FAILURE);
}
std::stringstream buffer;
buffer << in.rdbuf() << std::flush;
in.close();
return const_cast<char *>(buffer.str().c_str());
}
However, when I test the method out by outputting its contents into another file like this:
std::ofstream file("test.txt");
file << fileToChar("fileTest.txt");
I just get tons of strange characters like this:
îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþ[...etc]
What exactly is going on here? Is there anything I missed? And if there's a better way to do this, I would be glad to know!
char* fileToChar(std::string const& file){
This line already shows that something is going into the wrong direction. You return a pointer to some string, and it's completely unclear to the user of the function who is responsible for releasing the allocated memory, if it has to be released at all, if nullptr
can be returned, and so on.
If you want a string, then by all means use std::string
!
std::string fileToChar(std::string const& file){
return const_cast<char *>(buffer.str().c_str());
Another line that should make all alarms go off. const_cast
is always a workaround to some underlying problem (or some problem with external code).
There is usually a good reason why something is const
. By forcing the compiler to turn off the security check and allowing it to attempt modifications of unmodifiable data, you typically turn compilation errors into hard-to-diagnose run-time errors.
Even if this function worked correctly, any attempt to modify the result would be undefined behaviour:
char* file_contents = fileToChar("foo.txt");
file_contents[0] = 'x'; // undefined behaviour
But it does not work correctly anyway. buffer.str()
returns a temporary std::string
object. c_str()
returns a pointer to that temporary object's internally managed memory. The object's lifetime ends when the full expression return const_cast<char *>(buffer.str().c_str())
has been evaluated. Using the resulting pointer is therefore undefined behaviour, too.
The problems sound complicated, but the fix is easy. Make the function return std::string
and turn the last statement into return buffer.str();
.
这篇关于将文件转换为char *版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!