Basic i/o operations
====================




//
// copy standard input to output
// not to copy whitespaces
//


#include <iostream>

int main()
{
  char ch;
  std::cin >> ch;

  while ( ! std::cin.eof() )   // good(), bad(), fail(), 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.





//
// copy standard input to output
//


#include <iostream>

int main()
{
  char ch;
  std::cin >> std::noskipws;   // io manipulator
  std::cin >> ch;

  while ( ! std::cin.eof() )   // good(), bad(), fail(), eof()
  {
    std::cout << ch;
    std::cin  >> ch;
  }
  return 0;
}



$ ./a.out
jhaG AUIdh uidh uh duhiadoh o
jhaG AUIdh uidh uh duhiadoh o
$





//
// copy standard input to output
// C++ style
//


#include <iostream>

int main()
{
  char ch;
  std::cin >> std::noskipws;

  while ( std::cin >> ch )   // automatic conversion to void*
  {
    std::cout << ch;
  }
  return 0;
}






//
// copy standard input to output
// io methods
//


#include <iostream>

int main()
{
  char ch;

  while ( std::cin.get(ch) )   // automatic conversion to void*
  {
    std::cout.put(ch);
  }
  return 0;
}






//
// copy standard input to output
// with string
//


#include <iostream>
#include <string>

int main()
{
  std::string line;
  while ( std::getline(std::cin, line) )   // automatic conversion to void*
  {
    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
$





//
// copying cin to cout 
// converting lowercase to uppercase
// 
// resulting not really what we want
//

#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
$





//
// copying cin to cout 
// converting lowercase to uppercase
// 
// narrowing conversion to char
//

#include <iostream>

int main()
{
  char ch;
  std::cin >> std::noskipws;

  while ( std::cin >> ch )
  {
    // converting int to char
    ch = ('a' <= ch && ch <= 'z' ?  ch - 'a'+'A' : ch);
    std::cout << ch;
  }
  return 0;
}




$ ./a.out
gdfgd IUOZGFTFTF ŐÚéá
GDFGD IUOZGFTFTF ŐÚéá
$






//
// copying cin to cout 
// converting lowercase to uppercase
// 
// using std::toupper()
//

#include <iostream>
#include <locale>

int main()
{
  char ch;
  std::cin >> std::noskipws;

  while ( std::cin >> ch )
  {
    ch = std::toupper(ch);    // std::toupper() returns int
    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.



//
// copying cin to cout 
// converting lowercase to uppercase
// 
// using toupper() and static_cast<>()
//

#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;
}




//
// reading integers, ignore non-digits using ignore()
//
#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


//
// typical settings for locale
//
#include <iostream>
#include <locale>

int main()
{
  std::wcout << "User-preferred locale setting is "
             << std::locale("").name().c_str() << '\n';

  // on startup, the global locale is the "C" locale
  std::wcout << 1000.01 << '\n';

  // replace the C++ global locale as well as the C locale 
  // with the user-preferred locale
  std::locale::global(std::locale(""));

  // use the new global locale for future wide character output
  std::wcout.imbue(std::locale());

  // output the same number again
  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







//
// newline count
//


#include <iostream>

int main()
{
  int  cnt = 0;   // don't forget initialization
  char ch;

  std::cin >> std::noskipws;

  while ( std::cin >> ch )
  {
    if ( '\n' == ch )    // literal or const on the left side!
    {
      ++cnt;
    }
  }
  std::cout << "#newlines = " << cnt << std::endl;
  return 0;
}



$ ./a.out
jkdh
ajd ői
éasdjú őpo
<ctrl-d>
#newlines = 3
$




//
//  wordcount
//


#include <iostream>
#include <cctype>      // for std::isspace


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
=================





//
// list all argv arguments
// including the programname
//

#include <iostream>

int main(int argc, char *argv[])
{
  for ( int i = 0; i < argc; ++i )  // argc >= 1
  {
    std::cout << "arg" << i << " = " << argv[i] << std::endl;
  }
  return 0;
}



$ ./a.out first second third
arg0 = ./a.out
arg1 = first
arg2 = second
arg3 = third
$








//
// iterate over commanline parameters
// try to open files for read
// print error message if failes
//

#include <iostream>
#include <cctype>       // for isspace
#include <fstream>      // for ifstream
#include <cerrno>       // for errno
#include <cstring>      // for strerror


int wcount( std::istream &inp)    // returns #words
{
  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 )  // start from 1 for real arguments
  {
    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;
    }
  }  // inpfile closes automatically
  return 0;
}




$ ./a.out mainargs2.cpp bla
mainargs2.cpp: 148
No such file or directory
$



$ ./a.out
$







//
// iterate over commanline parameters
// try to open files for read
// print error message if failes
//
// another C++ style
//

#include <iostream>
#include <cctype>       // for isspace
#include <fstream>      // for ifstream
#include <cerrno>       // for errno
#include <cstring>      // for strerror


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 )    // no real arguments
  {
    std::cout << "stdin: " << wcount(std::cin) << std::endl;
  }
  else
  {
    for ( int i = 1; i < argc; ++i )  // start from 1 for real arguments
    {
      std::ifstream inpfile( argv[i], std::ios_base::in);   // constructor

      if ( ! inpfile )  // converted to logical value
      {
        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;
      }
    }  // inpfile closes automatically -  inpfile destructor called  
  }
  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
$