Hi,
I'm trying a very simple code as below.

#include <fstream>
#include <iostream>

int main()
{
       ofstream myfile;
    myfile.open ("example.txt");
    myfile << "Writing this to a file.n";
    myfile.close();
    return 0;
}

But getting following error on myfile.open statement...

Debug Assertion Failed!

Program: ...
File f:spvctoolsicrt_bldself_x86crtsrcopen.c
Line 308

Expression: ("Invalid sharing flag", 0)

Any idea?

Member Avatar for jencas

Is it a Unicode project?

You have to prefix std:: before ofstream, as in

std::ofstream myfile;

>>f:\sp\vctools\icrt_bld\self_x86\crt\src\open.c
Is this your source file?
This is a .C file. Make it .CPP This may fix your problem.
Some implementation (particularly on windows) wants your source file of C++ to end with .cpp

Are you absolutely sure "example.txt" isn't already open in another application?

Its a multi byte project.

Surprisingly its worked fine when I executed the code in release mode. Earlier I will doing in debug mode. Any idea?

you have forgot :

using namespace std;

right after the

#include <...>

try this, for me its working fine,
for VS and for dev .

ok...
first, thanks about that link, i'm yet a beginner.
second, if you wouldn't like to use "using namespace",
you might only use " std::std::ofstream myfile; ",
like siddhant3s said up there before.

any problems with that?

only use " std::std::ofstream myfile; ",

any problems with that?

Yup. There's one std:: too many.

Adding Namespace does not help. As I said, same code works in release mode but not in debug mode.

Just for fun, could you add this to your code and tell me what the program outputs:

if (!myfile.is_open() || !myfile.good()){
    std::cout << "Failed to open file!\n";
    return 0;
}

Put this directly after : myfile.open ("example.txt");

Adding Namespace does not help. As I said, same code works in release mode but not in debug mode.

Ok, this is a long shot but I'll suggest it anyway. Are you specifying a filepath or just "example.txt". If it's the latter you could be looking for the file in your debug directory, when it only exists in your release directory.

Program simply refuses to proceed after myfile.open. So it never reaches your piece of code.


Just for fun, could you add this to your code and tell me what the program outputs:

if (!myfile.is_open() || !myfile.good()){
    std::cout << "Failed to open file!\n";
    return 0;
}

Put this directly after : myfile.open ("example.txt");

Ok, this is a long shot but I'll suggest it anyway. Are you specifying a filepath or just "example.txt". If it's the latter you could be looking for the file in your debug directory, when it only exists in your release directory.

MrSpigot, I also tried specifying the complete file path. So this option is ruled out.

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.