Basic i/o operations
====================
#include <iostream>
int main()
{
char ch;
std::cin >> ch;
while ( ! std::cin.eof() )
{
std::cout << ch;
std::cin >> ch;
}
return 0;
}
$ ./a.out
gaVKDHLASHDLADHASLDKÉJ
gaVKDHLASHDLADHASLDKÉJ
jhasdjh iausdpasuid pauisd pőasuido aősdúőasd9
jhasdjhiausdpasuidpauisdpőasuidoaősdúőasd9
$
std::operator>> skips whitespaces by default.
#include <iostream>
int main()
{
char ch;
std::cin >> std::noskipws;
std::cin >> ch;
while ( ! std::cin.eof() )
{
std::cout << ch;
std::cin >> ch;
}
return 0;
}
$ ./a.out
jhaG AUIdh uidh uh duhiadoh o
jhaG AUIdh uidh uh duhiadoh o
$
#include <iostream>
int main()
{
char ch;
std::cin >> std::noskipws;
while ( std::cin >> ch )
{
std::cout << ch;
}
return 0;
}
#include <iostream>
int main()
{
char ch;
while ( std::cin.get(ch) )
{
std::cout.put(ch);
}
return 0;
}
#include <iostream>
#include <string>
int main()
{
std::string line;
while ( std::getline(std::cin, line) )
{
std::cout << line.size() << ": " << line << std::endl;
}
return 0;
}
$ ./a.out
zxg asuz asz zgzfg as zagpigpif psaiu pfpz f
44: zxg asuz asz zgzfg as zagpigpif psaiu pfpz f
0:
usdzfosdufzofuz
16: usdzfosdufzofuz
$
#include <iostream>
int main()
{
char ch;
std::cin >> std::noskipws;
while ( std::cin >> ch )
{
std::cout << ('a' <= ch && ch <= 'z' ? ch - 'a'+'A' : ch);
}
return 0;
}
$ ./a.out
shgoaszgo zgzifgifuz i127836ö2746
83727179658390717932907190737071737085903273495055565154-61-745055525410
$
#include <iostream>
int main()
{
char ch;
std::cin >> std::noskipws;
while ( std::cin >> ch )
{
ch = ('a' <= ch && ch <= 'z' ? ch - 'a'+'A' : ch);
std::cout << ch;
}
return 0;
}
$ ./a.out
gdfgd IUOZGFTFTF ŐÚéá
GDFGD IUOZGFTFTF ŐÚéá
$
#include <iostream>
#include <locale>
int main()
{
char ch;
std::cin >> std::noskipws;
while ( std::cin >> ch )
{
ch = std::toupper(ch);
std::cout << ch;
}
return 0;
}
- C++ allows narrowing conversions on assignment.
- Not working for all characters, using <locale> solve the issue.
- static_cast allows conversions within related types.
#include <iostream>
#include <locale>
int main()
{
char ch;
std::cin >> std::noskipws;
while ( std::cin >> ch )
{
std::cout << static_cast<char>(std::toupper(ch));
}
return 0;
}
#include <iostream>
int main()
{
int i;
std::cin >> i;
while ( ! std::cin.eof() )
{
if ( ! std::cin.good() )
{
std::cin.clear();
std::cin.ignore();
}
else
{
std::cout << i << std::endl;
}
std::cin >> i;
}
return 0;
}
For local-sensitive programs <locale> should be used
#include <iostream>
#include <locale>
int main()
{
std::wcout << "User-preferred locale setting is "
<< std::locale("").name().c_str() << '\n';
std::wcout << 1000.01 << '\n';
std::locale::global(std::locale(""));
std::wcout.imbue(std::locale());
std::wcout << 1000.01 << '\n';
return 0;
}
$ a.out
C_CTYPE=hu_HU.UTF-8;LC_NUMERIC=hu_HU.utf8;LC_TIME=hu_HU.utf8;LC_COLLATE=hu_HU.UTF-8;LC_MONETARY=hu_HU.utf8;LC_MESSAGES=hu_HU.UTF-8;LC_PAPER=hu_HU.UTF-8;LC_NAME=hu_HU.UTF-8;LC_ADDRESS=hu_HU.UTF-8;LC_TELEPHONE=hu_HU.UTF-8;LC_MEASUREMENT=hu_HU.utf8;LC_IDENTIFICATION=hu_HU.UTF-8
1000.01
1.000,01
#include <iostream>
int main()
{
int cnt = 0;
char ch;
std::cin >> std::noskipws;
while ( std::cin >> ch )
{
if ( '\n' == ch )
{
++cnt;
}
}
std::cout << "#newlines = " << cnt << std::endl;
return 0;
}
$ ./a.out
jkdh
ajd ői
éasdjú őpo
<ctrl-d>
#newlines = 3
$
#include <iostream>
#include <cctype>
int main()
{
int cnt = 0;
char prev = '\n';
char curr;
std::cin >> std::noskipws;
while ( std::cin >> curr )
{
if ( std::isspace(prev) && ! std::isspace(curr) )
{
++cnt;
}
prev = curr;
}
std::cout << "#words = " << cnt << std::endl;
return 0;
}
$ ./a.out
ozidg pduihp ikshd
sskl suh
ashdgzg s őof
<ctrl-d>
#words = 8
$
Program arguments
=================
#include <iostream>
int main(int argc, char *argv[])
{
for ( int i = 0; i < argc; ++i )
{
std::cout << "arg" << i << " = " << argv[i] << std::endl;
}
return 0;
}
$ ./a.out first second third
arg0 = ./a.out
arg1 = first
arg2 = second
arg3 = third
$
#include <iostream>
#include <cctype>
#include <fstream>
#include <cerrno>
#include <cstring>
int wcount( std::istream &inp)
{
int cnt = 0;
char prev = '\n';
char curr;
inp >> std::noskipws;
while ( inp >> curr )
{
if ( std::isspace(prev) && ! std::isspace(curr) )
{
++cnt;
}
prev = curr;
}
return cnt;
}
int main(int argc, char *argv[])
{
for ( int i = 1; i < argc; ++i )
{
std::ifstream inpfile;
inpfile.open( argv[i], std::ios_base::in);
if ( inpfile.good() )
{
std::cout << argv[i] << ": " << wcount(inpfile) << std::endl;
}
else
{
std::setlocale(LC_MESSAGES, "en_EN.utf8");
std::cerr << std::strerror(errno) << std::endl;
}
}
return 0;
}
$ ./a.out mainargs2.cpp bla
mainargs2.cpp: 148
No such file or directory
$
$ ./a.out
$
#include <iostream>
#include <cctype>
#include <fstream>
#include <cerrno>
#include <cstring>
int wcount( std::istream &inp)
{
int cnt = 0;
char prev = '\n';
char curr;
inp >> std::noskipws;
while ( inp >> curr )
{
if ( std::isspace(prev) && ! std::isspace(curr) )
{
++cnt;
}
prev = curr;
}
return cnt;
}
int main(int argc, char *argv[])
{
if ( argc < 2 )
{
std::cout << "stdin: " << wcount(std::cin) << std::endl;
}
else
{
for ( int i = 1; i < argc; ++i )
{
std::ifstream inpfile( argv[i], std::ios_base::in);
if ( ! inpfile )
{
std::setlocale(LC_MESSAGES, "en_EN.utf8");
std::cerr << argv[i] << ": " << std::strerror(errno) << std::endl;
}
else
{
std::cout << argv[i] << ": " << wcount(inpfile) << std::endl;
}
}
}
return 0;
}
$ ./a.out mainargs2.cpp bla
mainargs2.cpp: 148
bla: No such file or directory
$
$ ./a.out
hello
this is the stdin
<ctrl-d>
stdin: 5
$