i read c++ ebook, then i make a code to open a file using fstream and use ios::in flag
here's the code :

#include <iostream>
#include <fstream>

using namespace std;

bool openFileIn(fstream&, string);
int main()
{
    fstream dataFile;
        if(openFileIn(dataFile, "sendy.txt"))
        {
            cout<<"succes";
        }
        else
        cout<<"fail";

        return 0;
}

bool openFileIn(fstream &file, string name)
{
    bool status;
    file.open(name, ios::in);

    if (file.fail())
    status = false;

    else
    status = true;

    return status;
}

the code has problem with

file.open(name, ios::in);

then i read again, the problem can be solved by converting name(string object) to cstring :

file.open(name.c_str(), ios::in);

but there's no further explanation why i must convert the it to cstring, anyone knows?
and 1 more question, why do i should use reference variable to pass a fstream object?

edited :

i just tried the code using visual c++, and there's no problem with the string "name", is the problem because of the compiler?

Recommended Answers

All 5 Replies

but there's no further explanation why i must convert the it to cstring, anyone knows?

Yeah, I know. The reason is because of the original design of the libraries, in particular the iostream and the string library. Basically, they were developed more or less in isolation, and iostream came first. You will notice that the iostream library does not make use of C++ strings anywhere. The operations that you can do between iostreams and C++ strings are limited to functions that are defined by the string library (like std::getline() or the operator << and >> for strings). In other words, none of the iostream libraries include the string header. It was simply designed like that. And, obviously, if that is the case, the file-streams cannot take a C++ string as the file-name, and thus the conversion to a C-string.

why do i should use reference variable to pass a fstream object?

Such that the function parameter becomes an alias for the fstream object that was passed to the function. In other words, if you want the changes made to the fstream object within the function to affect the dataFile object in the main() function, then it must be passed by reference. Otherwise, if you don't pass-by-reference, the fstream object will be copied into the function, and when the function returns, the file will be closed as that local variable gets destroyed, and as for the dataFile object, it will remain completely unaffected (still empty).

i just tried the code using visual c++, and there's no problem with the string "name", is the problem because of the compiler?

Again, string literals like "name" are C-strings in C++. So, it should work as a file-name for a file-stream object.

errr sry i have bad english and still newbie in programming, so the conclusion is because the fstream and string is in a different libraries, but cstring can be used by all libraries?
erm i meant i tried the code using visual c++, and there's no problem with the string object named name, is the problem because of the compiler?

 file.open(name, ios::in);

in code blocks, it has problem, but in visual c++ it works.

is the problem because of the compiler?

No. You have C++ Strings and you have C-strings. Both are different and not interchangable.

Some functions have been designed to use C++ strings; some use C-strings. The documentation of each function tells you which one you need to use.

Some compilers will make extensions (added abilities) that the standard C++ definition does not describe. If both C-strings and C++Strings work in VC++ they made an extension so it will work for their compiler but not others.

but cstring can be used by all libraries?

C-strings are a built-in type. In other words, they are part of the programming language. C++ string is a library feature, defined in the header <string>. Built-in types can be used everywhere, without the need to include a library for them.

oh okay thx i finally got it! :D

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.