Newbie question(open a file)

Thread Solved

Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Newbie question(open a file)

 
1
  #1
Sep 4th, 2007
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.

  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.

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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,498
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Newbie question(open a file)

 
0
  #2
Sep 4th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,617
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1615
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Newbie question(open a file)

 
0
  #3
Sep 4th, 2007
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 8:26 am.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Newbie question(open a file)

 
0
  #4
Sep 4th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,337
Reputation: vishesh is on a distinguished road 
Solved Threads: 39
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Newbie question(open a file)

 
0
  #5
Sep 5th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Newbie question(open a file)

 
0
  #6
Sep 5th, 2007
I have put it in my original post.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,337
Reputation: vishesh is on a distinguished road 
Solved Threads: 39
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Newbie question(open a file)

 
0
  #7
Sep 5th, 2007
Ok then same messages. Try this. Goto Project->Properties->Linker->System->Subsystem. Change it to /SUBSYSTEM:CONSOLE.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Newbie question(open a file)

 
0
  #8
Sep 5th, 2007
Wow, I got it. Thanks a lot all of guys comments.

Thanks again,
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1740 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC