Visual Studio Projects\project_name\Debug
folder. (in may be in other name, you can do a search to find out where it is). Visual Studio Projects\project_name\Debug
folder. programname.exe
to execute the program.
#include<iostream> using namespace std; int main() { cout << "Hello World.\n"; // the following few lines are useless // it mainly used to prevent the closure of DOS window char ch; cout << "Press any key to stop the program. "; cin >> ch; return 0; } |
#include<iostream> #include<fstream> // include the library using namespace std; int main() { ofstream outStream; // define an output file stream outStream.open("hello.txt"); // open a file named hello.txt outStream << "Hello World"; // write to the file outStream.close(); // close the output file stream return 0; } |
hello.txt
will appear under folder Visual Studio Projects\project_name
. hello.txt
will appear in the folder contains the executable file. i.e. under
folder Visual Studio Projects\project_name\Debug
. hello.txt
is
Hello World
#include<fstrea>
: file stream library. ofstream outStream;
declares an ofstream
(output file stream) named outStream
. outStream.open("hello.txt")
opens a file named hello.txt
for writing. outStream << "Hello World"
. Write Hello
World
to the file.
Compared to cout << "Hello World"
.outStream.close()
: close the stream.
#include<iostream> |
number.txt
1999 |
add
, then add new items
,
then choose Text file .txt
icon. Gives it a name (number.txt
).
Then type in 1999
. Compile and execute the program.
Visual Studio Projects\project_name
.
char ch;
cout << "Press any key to stop the program "
cin >> ch;
1999 |
ifstream inStream;
declare an ifstream
(input file stream) named inStream
. inStream.open("number.txt")
: open the file number.txt
.
inStream >> a
, read an integer and stored into
the variable a
. Compared to cin >> a
inStream.close()
: close the stream.
#include<iostream> |
student.txt
Betty F |
female.txt
Betty |