943,836 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2098
  • C++ RSS
Sep 4th, 2007
1

Newbie question(open a file)

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. ofstream file;
  9. file.open ("example.txt");
  10.  
  11. if(file.is_open())
  12. {
  13. file << "Write a text line";
  14. file.close();
  15. }
  16. else
  17. {
  18. file << "Unable to wirte text";
  19. }
  20. return 0;
  21. }

When I compile it gives the following error.

Quote ...
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.
Similar Threads
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Sep 4th, 2007
0

Re: Newbie question(open a file)

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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Sep 4th, 2007
0

Re: Newbie question(open a file)

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.
Last edited by Ancient Dragon; Sep 4th, 2007 at 9:26 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,951 posts
since Aug 2005
Sep 5th, 2007
0

Re: Newbie question(open a file)

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?
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Sep 5th, 2007
0

Re: Newbie question(open a file)

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.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Sep 5th, 2007
0

Re: Newbie question(open a file)

I have put it in my original post.
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Sep 5th, 2007
0

Re: Newbie question(open a file)

Ok then same messages. Try this. Goto Project->Properties->Linker->System->Subsystem. Change it to /SUBSYSTEM:CONSOLE.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Sep 5th, 2007
0

Re: Newbie question(open a file)

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

Thanks again,
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help: Win32 and standard lib functions.
Next Thread in C++ Forum Timeline: Compliler errors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC