i was create new project in visual studio 2010
i was write this code:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream file;
string filename = "text.text";
file.open(filename);
if(file.is_open())
{
file<<"Hello Sarah"<<endl;
}
else
{
cout<<"Could not open file :"<<filename<<endl;
}
return 0;
}
//
i did not what is the error , i could not run the coud
here the errors;
//
1 error LNK2005: _main already defined in file.obj C:\Users\s\documents\visual studio 2010\Projects\tray4\tray4\Function.obj tray4
2 error LNK1169: one or more multiply defined symbols found C:\Users\s\documents\visual studio 2010\Projects\tray4\Debug\tray4.exe 1 1 tray4
3 IntelliSense: no operator "<<" matches these operands c:\users\s\documents\visual studio 2010\projects\tray4\tray4\file.cpp 20 32 tray4
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
For number one check the "Solution Explorer", view Solution Explorer or "ctrl + alt + L", and make sure the section "Source Files" only contains one file that contains " the "main" function. The error makes me think that more than one file contains the function "main" and there can only be one "main" function in a program.
For number two it could be the same thing as one. You are trying to include more than one file with the same code. Or it may be connected to error number one.
For error three you need to include the header file "<string>". The "cout" statement the "cout" statement is having trouble with the variable "filename". Including the header file should take care of this error.
I will load up the program and see if there is anything else.
#include<iostream>
#include <string>
#include<fstream>
//using namespace std;
int main()
{
std::ofstream file;
std::string filename = "text.text";
file.open(filename);
if (file.is_open())
file << "Hello Sarah" << std::endl;
else
std::cout << "Could not open file :" << filename << std::endl;
return 0;
}
The blank lines at the top make it easier to read.
You should learn not to use the line usingnamespace std; as it WILL get you in trouble some day. Now is a good time to start learning what is in the standard namespace like "std::ofstream", "std::cout", "std::cin" and "std::endl" for a start. After a week or so typing "std::" will be done without any thought. It will become habit.
In the if/else statements the {}s are optional for a single line. It is OK if you want to use them.
Most times it is best to write the if statement this way:
1 2
if (!file.is_open())
std::cout << "Could not open file :" << filename << std::endl;
This way you are not trying to put all of your code all in the then block or have unnecessary code that you really do not need. Like telling you that the file ddid open unless you want this in the beginning.