Hey,
I know how to read from file.. I would to save it and send it to another function that can print it out that is in a different c file .. how can i do that?

Recommended Answers

All 5 Replies

I tried but it dosn't work ...
The brain.cpp file that is from where I want to send the string

Brain::Brain()
{
    char* s="Ctor of class Brain Called";
    Taxi x;
    x.PrintTaxi(s);
}

and the Taxi.cpp is where I want to print the string to the file

void Taxi::PrintTaxi(char *a)
{
    ofstream myfile("output.txt");
    if (myfile.is_open())
    {
        myfile << *a;
        myfile.close();
    }
    else cout << "Unable to open file";

}

I tried but it dosn't work ...

"Doesn't work" is not a helpful problem report. Please be specific about how it doesn't work.

However, at a glance there are two immediate problems. One is subtle and the other is glaring:

char* s="Ctor of class Brain Called";

While compilers typically allow this, you should use a pointer to const char for string literals:

const char *s = "Ctor of class Brain Called";

myfile << *a;

This is fine if you only want the first character of the string to be written to the file. Otherwise, don't dereference the pointer.

I don't really know how to explain how it doesn't work because I don't have any compiler mistakes..
It doesn't write to the "output" file.. when I used the function without getting the argument char *a it was writing to the output file properly...

I recomend you to use strings, neither chars nor pointers.
#include <string>
and then
string s = "Ctor of class Brain Called";
finally
void Taxi::PrintTaxi(const string& a)
Remember, you are using c++ not c.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.