Hi all,

I have just start to work on C++.

I want to open a text file, then write few text there. This is the code I wrote for that.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	ofstream file;
	file.open ("example.txt");

	if(file.is_open())
		{
			file << "Write a text line";
            		file.close();
		}
	else
		{
			file << "Unable to wirte text";
		}
	return 0;
}

When I compile it gives the following error.

Compiling...
rbf.cpp
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function _WinMainCRTStartup
Debug/ReadFile.exe : fatal error LNK1120: 1 unresolved externals

What's wrong there. Can you guys help me to solve it.

Comments
Thanks for using code tags

You are using the Visual C++ IDE.
You have created a "Win32 Project".
You should be creating a "Win32 Console Application" project.
To solve this problem, create a new "Win32 Console Application" and add the rbf.cpp source file to the new project and compile. Should work.

you created a windows program not a console program. The simplest fix to that problem is to start all over again, but this time when you get to the Application Settings window choose Console Application

[edit] what WolfPack wrote -- I didn't see it when I wrote this [/edit]


About the code you posted: If the file failed to open you can't write an error message to it. You probably want to use cout instead of the file handle that failed to open.

Ok, Here what I have done.

I used Microsoft Visual Studio .Net 2003.

First click File -> New -> Project

From New Project window, select Visual C++ Project as project type and Win32 Console Project as template.

On the Application Settings, select Console Application and Empty Project.

But I'm fail. What should I do?

What error messages are you are getting? The project as console application must compiler your program well.

Also try to deselect(if selected) the Precompiled Header option. Sometimes it happens due to this because of some silly mistakes.

Ok then same messages. Try this. Goto Project->Properties->Linker->System->Subsystem. Change it to /SUBSYSTEM:CONSOLE.

This question has already been answered. Start a new discussion instead.