GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — learn/doc/cpp_input_output at main · gyuho/learn

Expand file tree

/

cpp_input_output

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

cpp_input_output

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
code
code
 
 
README.md
README.md
 
 

README.md

Outline

back to contents

C++: input, output

  • input, output
  • exist
  • write, read

↑ top




input, output

#include <iostream>
using namespace std;

int main ()
{
	int i;
	cout << "Please enter an integer value: ";
	cin >> i;
	cout << "The value you entered is " << i;
	cout << " and its double is " << i*2 << ".\n";
}

/*
Please enter an integer value: 111
The value you entered is 111 and its double is 222.
*/

↑ top




exist

#include <iostream>
using namespace std;
#include <sys/stat.h>

inline bool isExist (const string& name) {
	struct stat buffer;
	return (stat (name.c_str(), &buffer) == 0); 
}

int main()
{
	cout << isExist("./testdata/sample.txt") << endl; // 1
}

↑ top




write, read

#include <iostream>
#include <fstream>
// #include <stdio.h>
#include <cstdio>
using namespace std;

int main () {

	string fpath = "example.txt";

	ofstream fw;
	fw.open (fpath);
	fw << "Hello World!\n";
	fw << "C++\n";
	fw.close();

	string line;
	ifstream fi (fpath);
	if (fi.is_open())
	{
		while ( getline (fi, line) )
		{
			cout << line << endl;
		}
		fi.close();
	}
	else cout << "Unable to open file" << endl;

	if( remove( fpath.c_str() ) != 0 )
		perror( "Error deleting file" );
	else
		puts( "File successfully deleted" );
}

/*
Hello World!
C++
File successfully deleted
*/

↑ top




You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.