954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Newbie question(open a file)

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.

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

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.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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?

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

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.

vishesh
Nearly a Posting Virtuoso
1,381 posts since Oct 2006
Reputation Points: 85
Solved Threads: 42
 

I have put it in my original post.

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

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

vishesh
Nearly a Posting Virtuoso
1,381 posts since Oct 2006
Reputation Points: 85
Solved Threads: 42
 

Wow, I got it. Thanks a lot all of guys comments.

Thanks again,

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You