简体   繁体   中英

Try to write char[] to a text file

I trying to write a char[256] to a text file. Below is my current work:

         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 *'

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;

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:

ofs.open( "c:\\myURL.txt", ios::out | ios::text);

Anyway, it would be better to use ofstream instead of a generic fstream :

 ofstream ofs;
 ofs.open( "c:\\myURL.txt", ios::text );
 ofs.write( (char*)testDest, 256 );    
 ofs.close();

Several things wrong or "not good" in your code:

  1. You never check if the open fails.
  2. You use clunky write functions.
  3. You don't check if your write is succesful (not quite necessary if you're kind of sure it will work).

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):

// 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.

您忘记了,如果有人试图检查文件是否存在,那么如果它没有创建文件,则必须使用fstream作为对象,这样就不会有2个不同的对象用于打开和写入

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM