C++ 尝试将 char[] 写入文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3811328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Try to write char[] to a text file
提问by karikari
I trying to write a char[256] to a text file. Below is my current work:
我试图将字符 [256] 写入文本文件。以下是我目前的工作:
fstream ofs;
ofs.open("c:\myURL.txt");
ofs.write((char*)testDest,256);
ofs.close();
It still does not work.
它仍然不起作用。
here is the error:
这是错误:
error C2440: 'type cast' : cannot convert from '' to 'char *'
错误 C2440:“类型转换”:无法从“”转换为“字符 *”
update:
更新:
so far, here is my progress attempt, the code can compile, but when running, my program suddenly terminated.
到目前为止,这是我的进度尝试,代码可以编译,但是在运行时,我的程序突然终止了。
ofstream stream;
CBar *a;
switch(uMessage) {
case WM_PAINT:
return bar->OnPaint();
case WM_ERASEBKGND:
return 1;
case WM_LBUTTONDOWN: //wira
if (!bar->OnClick(wParam, lParam)) {
stream.open("C:\myURL.txt");
stream << a->testDest << endl; // if I replace `a->testDest` with "testword" string, my prgrom does not terminated. Why?
return 0;
}
break;
回答by rubenvb
Several things wrong or "not good" in your code:
您的代码中有几处错误或“不好”:
- You never check if the
open
fails. - You use clunky
write
functions. - You don't check if your write is succesful (not quite necessary if you're kind of sure it will work).
- 你永远不会检查是否
open
失败。 - 您使用笨拙的
write
功能。 - 您不会检查您的写入是否成功(如果您确定它会起作用,则不是很有必要)。
This will give you more info if something fails:
如果出现故障,这将为您提供更多信息:
#include <fstream>
using std::ofstream;
#include <iostream>
using std::cout;
using std::endl;
int main()
{
ofstream stream;
char charArray[] = "Some stuff in a char array.";
stream.open("C:\myurl.txt");
if( !stream )
cout << "Opening file failed" << endl;
// use operator<< for clarity
stream << testDest << endl;
// test if write was succesful - not *really* necessary
if( !stream )
cout << "Write failed" << endl;
return 0;
}
My guess is that opening the file failed because you lack proper permissions. The above program will tell you where what fails.
我的猜测是打开文件失败,因为您缺乏适当的权限。上面的程序会告诉你哪里失败了。
UPDATE: To answer your second question: you do this:
更新:要回答你的第二个问题:你这样做:
CBar* a;
Which creates a pointer but leaves it unitiallized. You then want to dereference it to access its testDest
data member, which obviously leads to a crash. You need to initialize your pointer (or don't use a pointer here, I see no reason to):
它创建了一个指针,但使其单元化。然后您想取消引用它以访问其testDest
数据成员,这显然会导致崩溃。你需要初始化你的指针(或者不要在这里使用指针,我认为没有理由):
// Either this
CBar* a = new CBar(/*some arguments, or none, depending on CBar definition*/);
//...
cout << a->testDest << endl;
// Or this (better here in my opinion)
CBar a; // OK if there is a default constructor (one with no arguments);
//...
cout << a.testDest << endl;
Please read any good tutorial on c++. These are mistakes you make either when you've not slept for three days or if you don't understand the basic concepts of the language.
请阅读有关 C++ 的任何好的教程。如果您三天没睡觉,或者您不理解语言的基本概念,您就会犯这些错误。
回答by Baltasarq
You need to pass to fstream, in open(), the kind of operation you're expecting to do: input, output, or even both. You should try with:
您需要在open()中将您期望执行的操作类型传递给fstream:输入、输出,甚至两者。你应该尝试:
ofs.open( "c:\myURL.txt", ios::out | ios::text);
Anyway, it would be better to use ofstreaminstead of a generic fstream:
无论如何,最好使用ofstream而不是通用的fstream:
ofstream ofs;
ofs.open( "c:\myURL.txt", ios::text );
ofs.write( (char*)testDest, 256 );
ofs.close();
回答by N0k7urn4l
You forget that if someone is trying to check if file exists, then if it doesn't create the file you have to use fstream for your object so is to not have 2 different objects for open and write
你忘记了,如果有人试图检查文件是否存在,那么如果它没有创建文件,你必须对你的对象使用 fstream,所以不要有 2 个不同的对象来打开和写入