// page information $page_type = "t"; $page_title = "C++ Variables and File Handling"; $page_keywords = "C++, C, c plus plus, variables, file handling, int, char, short, long, classes, floating point, float, double, strings, math operators, input, output, files"; $page_description = "C++ variables and file handling tutorial. Find more tutorials and scripts at TheScripts.com, a programming and software development resource, directory and community."; $page_articletitle = "C++ Variables and File Handling"; $page_next_url = ""; $page_next_anchor = ""; $page_prev_url = ""; $page_prev_anchor = ""; $page_author = "John Bourbonniere"; $page_byline = "Developer, PlayZion.com"; // site header include ($_SERVER["DOCUMENT_ROOT"]."/header.php"); // begin html ?>
T', 'x', or '9'. Use single quotes)
Note: You can use the keyword 'unsigned' before any of the above to restrict the class to hold only positive integer values.
#include "cstring.h" at the beginning of your program + - * / % = Order of operations: Expressions in brackets are evaluated first. The operators *, /, and % are evaluated from left to right in the order they appear. After that, + and - are evaluated from left to right.
<class> <variable name>;
Or, to assign a value to assign a value to the variable right away, you can type
<class> <variable name> = <initial value>;
For example,
int ab = 5;
unsigned long int cd = 3948293;
float answer;
Input and Output
If you want to write a value to the screen, just type
cout << <value>;
To start a new line, type cout << endl;
You can combine these two, along with variables.
For example,
cout << "You are " << age << " years old." << endl;
To read a variable in, type cin >> <variable>;
To use input and output in your programs, you must type
#include <iostream.h>
at the beginning of your programs.
Do use input and output with files, you must type
#include <iostream.h>
at the beginning of your programs.
You must first declare a variable to open a file.
To do this, type ofstream MyFile; (or any value you want for 'MyFile'). Then, you can perform operations with it.
For example,
MyFile.Open ("c:\testfile.txt");
After that, you can output to the file just as you would output to the screen, except you type the value of the file first.
For example,
MyFile << "Line one of output" << endl;
When you're finished writing to the file, you must close it. To do this, type MyFile.close();
Reading from a file is just as easy. You must declare an input file as 'ifstream'.
For example,
ifstream MyFile ("textfile.txt");
MyFile >> a;
MyFile >> b;
cout << "the file contained the values " << a << " and " <<
b << endl;
MyFile.close()
You can read and write to the same file, but make sure you don't do it at the same time. You must close between operations.
//end html // site footer include ($_SERVER["DOCUMENT_ROOT"]."/footer.php"); ?>