Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1115435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:09:17+00:00 2026-05-17T03:09:17+00:00

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

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T03:09:18+00:00Added an answer on May 17, 2026 at 3:09 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.