I/O Stream Example :
//Reads three numbers from the file infile.txt, sums the numbers,
//and writes the sum to the file outfile.txt.
#include <fstream>
using std::ifstream;
using std::ofstream;
using std::endl;
int main( )
{
ifstream inStream;
ofstream outStream;
inStream.open("infile.txt");
outStream.open("outfile.txt");
int first, second, third;
inStream >> first >> second >> third;
outStream << "The sum of the first 3\n"
<< "numbers in infile.txt\n"
<< "is " << (first + second + third)
<< endl;
inStream.close( );
outStream.close( );
return 0;
}
Navid Plus is a Tech blog focusing on Science material that I learn day through my College and job. Most likely, blog posts contain C++ Programming, Security, Networking, and Math. Let's have some fun.
Showing posts with label I/O. Show all posts
Showing posts with label I/O. Show all posts
Wednesday, March 22, 2017
Friday, March 17, 2017
C++ Part 30
String | Getline and Cin
string s1, s2;
cin >> s1;
cin >> s2;
If the user types in
I love NavidPlus website.
s1 will receive value "I" and s2 will receive the value "love".
If you want your program to read an entire line of input into a variable of type string , you can use the function getline.
Example:
string line;
cout << "Enter a line of input:\n"; getline(cin, line); cout << line << "END OF OUTPUT\n";
I/O with string Objects
You can use the insertion operator << with cout to output string objects. You can input a string with the extraction operator >> and cin.
You can use the function getline to input an entire line of text into a string object.
EXAMPLES
string greeting("Hello"), response, nextLine;
cout << greeting; cin >> response;
getline(cin, nextLine);
Saturday, March 11, 2017
Intro to C++ Part 4
- Declaration of variables: How does the variable map onto memory? Updating variable: How do we update the variable, and what do we update it with?
- I/O: How do we input and output data into the system?
- Control: How do we control which statement gets executed next?
- Modularity and Object Orientation: How do we organize the program to enable proper software engineering practices?
- Comments: Used to describe code; ignored by compiler, but code unmaintainable without good comments! C/C++: on line beginning with // or surrounded by /*, */
Subscribe to:
Posts (Atom)
Digital Design Part 3
4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...
-
The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the...
-
4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...