1.
#include <iostream>
int main()
{
char input[255];
std::cin.get(input, 255, '$');
std::cout << "字符数: " << strlen(input) << std::endl;
std::cout << "下一个字符: "<< (char)std::cin.get() << "\n";
return 0;
}
2.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main(int argc, char * argv[])
{
using namespace std;
ofstream fout;
for (int file = 1; file < argc; file++)
{
fout.open(argv[file]);
if (!fout.is_open())
{
cerr << "Can't open " << argv[file] << endl;
system("pause");
exit(EXIT_FAILURE);
}
cout << "Please enter your input (enter a blank line to quit): \n";
string input;
while (getline(cin, input) && input.size() > 0)
fout << input << endl;
cout << "Input over.\nBye.\n";
fout.close();
}
return 0;
}
3.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
char ch;
ifstream fin;
ofstream fout;
if (argc < 3)
{
cerr << "参数不够" << endl;
exit(EXIT_FAILURE);
}
fin.open(argv[1]);
fout.open(argv[2]);
if (!fin.is_open())
{
cerr << "Could not open " << argv[1] << endl;
exit(EXIT_FAILURE);
}
if (!fout.is_open())
{
cerr << "Could not open " << argv[2] << endl;
exit(EXIT_FAILURE);
}
cout << "open successfully.\n";
while (fin.get(ch))
fout << ch;
cout << "Copy and paste successfully.\nBye.\n";
fin.close();
fout.close();
return 0;
}
4.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
char ch;
ifstream fin, fin1;
ofstream fout;
char file1[] = "helloworld.txt";
char file2[] = "areyouok.txt";
char file3[] = "hello.txt";
fin.open(file1);
fin1.open(file2);
fout.open(file3);
if (!fin.is_open())
{
cerr << "Could not open :" << file1 << endl;
exit(EXIT_FAILURE);
}
if (!fin1.is_open())
{
cerr << "Could not open :" << file2 << endl;
exit(EXIT_FAILURE);
}
if (!fout.is_open())
{
cerr << "Could not open " << file3 << endl;
exit(EXIT_FAILURE);
}
cout << "open successfully.\n";
string temp;
string temp1;
int a = 0;
int b = 0;
while (getline(fin, temp))
++a;
while (getline(fin1, temp1))
++b;
fin.clear();
fin1.clear();
fin.close();
fin1.close();
fin.open(file1);
fin1.open(file2);
if (a >= b)
{
for (int i = 0; i < a; ++i)
{
getline(fin, temp);
fout << temp << " ";
if (i < b)
{
getline(fin1, temp1);
fout << temp1 << endl;
}
}
}
else
{
for (int i = 0; i < b; ++i)
{
getline(fin1, temp1);
fout << temp1 << " ";
if (i < a)
{
getline(fin, temp);
fout << temp << endl;
}
}
}
cout << "Copy and paste successfully.\nBye.\n";
fin.close();
fout.close();
return 0;
}
5.
#include <iostream>
#include <fstream>
#include <cstdlib>