C++

Technical Notes

These notes are my personal online notebook of useful commands and "how-to's". You are welcome to make use of them if you find them helpful. They obviously don't come with any warranty! Click on one of the category tags above for the notes in any category.

C++ string handling

Read a line from a file: ( http://www.cplusplus.com/forum/beginner/8388/ )

#include < iostream>
#include < fstream>
#include < string>
using namespace std;

void main ()
{
        string STRING;
	ifstream infile;
	infile.open ("names.txt");
        while(!infile.eof) // To get you all the lines.
        {
	        getline(infile,STRING); // Saves the line in STRING.
	        cout << STRING; // Prints our STRING.
        }
	infile.close();
	system ("pause");
}

Alternately we can open the file with: ( http://www.cplusplus.com/doc/tutorial/files/ )

C++ Sample Code

#include <iostream>
using namespace std;

void f(const char *g);
void f(void);

void f(const char *g) { cout << "Hello, " << g << "!" << endl; }
void f(void) { cout << "Welcome to Paradise!" << endl; }

int main(int argc, char *argv[])
{
	f("Student");
	f();
	f(argv[1]);
	return 0;
}

C++ Notes

Syndicate content