C++ 教程 在线

1944C++ 高级教程 C++ 文件和流

简单文件输入输出

#include <fstream> 
#include <string>
#include <iostream>
#include <streambuf> 

using namespace std; 

int WriteFile(string filepath,const string& Init)
{
    //定义文件输出流 
    ofstream outfile; 

    //根据参数路径创建要输出的文件 
    outfile.open(filepath, ios::out | ios::trunc);    
    if(!outfile)
        return 1;
    outfile << Init << endl;

    outfile.close(); 
    return 0;
}

string Read_Str(string filepath)
{
    ifstream infile;
    infile.open(filepath);
    //打开失败,路径不正确
    if(!infile)
        cout << "Open File Fail!" << endl;
    //读取文本内容到字符串
    string readStr((std::istreambuf_iterator<char>(infile)),  std::istreambuf_iterator<char>()); 
    
    return readStr;
}

int main()
{
    //执行完成在mian.cpp同一目录下生产C.txt文件
    WriteFile("./C.txt","Hello , world!");
    cout << Read_Str("./C.txt") << endl;

    getchar();
    return 0;
}

在main.cpp同一目录下生成一个C.txt文件。屏幕显示输出结果

Hello , world!

1943C++ 高级教程 C++ 文件和流

对于 cin 的操作 使用 getline(cin,str)往往可以实现更加简单以及安全的字符串操作,不同于 cin.getline(char*, int a),前者可以直接对字符串进行操作。

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
    fstream iofile;
    iofile.open("D:\\t.txt", ios::out | ios::in | ios::trunc);
    string bookname;
    string bookwriter;
    cout << "input the bookname:" << endl;
    getline(cin, bookname);
    iofile << bookname << endl;
    cout << "input the bookwriter:" << endl;
    getline(cin, bookwriter);
    iofile << bookwriter << endl;
    iofile.close();
    cout << "read the input file:" << endl;
    iofile.open("D:\\t.txt");
    while (getline(iofile, bookname))
    {
        cout << bookname << endl;

    }
 
    system("pause");
    return 0;

}

程序输入输出结果:

input the bookname:
we are the world
input the bookwriter:
Tanks
read the input file:
we are the world
Tanks
请按任意键继续. . .

1942C++ 高级教程 C++ 文件和流

关于 cin.ignore() ,完整版本是 cin.ignore(int n, char a), 从输入流 (cin) 中提取字符,提取的字符被忽略 (ignore),不被使用。每抛弃一个字符,它都要计数和比较字符:如果计数值达到 n 或者被抛弃的字符是 a,则 cin.ignore()函数执行终止;否则,它继续等待。它的一个常用功能就是用来清除以回车结束的输入缓冲区的内容,消除上一次输入对下一次输入的影响。比如可以这么用:cin.ignore(1024,'\n'),通常把第一个参数设置得足够大,这样实际上总是只有第二个参数 \n 起作用,所以这一句就是把回车(包括回车)之前的所以字符从输入缓冲(流)中清除出去。

#include <iostream>
using namespace std;
void main()
{
    int a,b,c;
    cout<<"input a:";
    cin>>a;
    cin.ignore(1024, '\n');
    cout<<"input b:";
    cin>>b;
    cin.ignore(1024, '\n');
    cout<< "input c:";
    cin>> c;
    cout<< a << "\t" << b << "\t" << c << endl;
}

如果cin.ignore()不给参数,则默认参数为cin.ignore(1,EOF) 其中 EOF是end of file的缩写,表示"文字流"(stream)的结尾。

#include<iostream>  
using   namespace   std;  
int main()  
{  
    char   str1[30],str2[30],str3[30];  
    cout   <<   "请输入你的姓名:";  
    cin>>str1;  
    cout<<"请输入你的住址:";  
    cin.ignore();  
    cin.getline(str2,30,'a');  
    cout   <<   "请输入你的籍贯:";  
    cin.ignore();  
    cin.getline(str3,30);  
    cout<<str3;  
}

如果在地址那里输入 bcdabcd 那么此时流里面剩的是 bcd\n,此时 cin.ignore(); 吃掉的就是b了,这是流里还剩下 cd\n 直接交给 cin.getline(str3,30); 应为有个 \n 所以这里 getline 就直接返回。

1941C++ 高级教程 C++ 文件和流

读写&复制实例

下面的 C++ 程序以读写模式打开一个文件。

file_wr() 在向文件 test.txt 写入用户输入的信息之后,程序从文件读取信息,并将其输出到屏幕上;

file_copy()将文件test.txt里的数据读取出来后,再写入test_1.txt中。

#include "iostream"
#include "fstream"
using namespace std;

//向文件内部写入数据,并将数据读出
void file_wr(void)
{
    char data[100];
    //向文件写入数据
    ofstream outfile;
    outfile.open("test.txt");
    cout << "Write to the file" << endl;
    cout << "Enter your name:" << endl;
    cin.getline(data, 100);
    outfile << data << endl;
    cout << "Enter your age:" << endl;
    cin >> data;
    cin.ignore();
    outfile << data << endl;
    outfile.close();
    //从文件读取数据
    ifstream infile;
    infile.open("test.txt");
    cout << "Read from the file" << endl;
    infile >> data;
    cout << data << endl;
    infile >> data;
    cout << data << endl;
    infile.close();
}


//将数据从一文件复制到另一文件中
void file_copy(void)
{
    char data[100];
    ifstream infile;
    ofstream outfile;
    infile.open("test.txt");
    outfile.open("test_1.txt");
    cout << "copy from test.txt to test_1.txt" << endl;
    while (!infile.eof())
    {
        infile >> data;
        cout << data << endl;
        outfile << data << endl;
    }
    infile.close();
    outfile.close();
}

//测试上述读写文件,与文件数据复制
int _tmain(int argc, _TCHAR* argv[])
{
    file_wr();
    file_copy();
    return 0;
}

当上面的代码被编译和执行时,它会产生下列输入和输出:

$./a.out
Writing to the file
Enter your name: 
John
Enter your age:
20
Reading from the file
John
20
copy from test.txt to test_1.txt
John
20

1940C++ 数据封装

C++中, 虚函数可以为private, 并且可以被子类覆盖(因为虚函数表的传递),但子类不能调用父类的private虚函数。虚函数的重载性和它声明的权限无关。

一个成员函数被定义为private属性,标志着其只能被当前类的其他成员函数(或友元函数)所访问。而virtual修饰符则强调父类的成员函数可以在子类中被重写,因为重写之时并没有与父类发生任何的调用关系,故而重写是被允许的。

编译器不检查虚函数的各类属性。被virtual修饰的成员函数,不论他们是private、protect或是public的,都会被统一的放置到虚函数表中。对父类进行派生时,子类会继承到拥有相同偏移地址的虚函数表(相同偏移地址指,各虚函数相对于VPTR指针的偏移),则子类就会被允许对这些虚函数进行重载。且重载时可以给重载函数定义新的属性,例如public,其只标志着该重载函数在该子类中的访问属性为public,和父类的private属性没有任何关系!

纯虚函数可以设计成私有的,不过这样不允许在本类之外的非友元函数中直接调用它,子类中只有覆盖这种纯虚函数的义务,却没有调用它的权利。