阿西吧 发表于 2017-6-24 21:46:42

C++上天之路3-4课笔记

第四课       
        #include<fstream> (文件读写操作)
        文件对象(ifstream文件读操作)
        文件对象(ofstream文件写操作)
        ifstream.open()
        ofstream.open()

        ifstream in;                               
        in.open("text.txt")                等同于ifstream in("text.txt")

        ofstream out;                       
        out.open("text.txt")                等同于ofstream out("text.txt")
       
        ofstream out("text.txt",ios::out)以只写打开
        ifstream in("text.txt",ios::in)以只读打开
        fstream fp("text.txt")以只

        ios::xx | ios::xx

        ios::in         打开一个可读文件               
        ios::out        打开一个可写入文件
        ios::app        写入的所有数据被加到文件的末尾
        ios::binary        以二进制的形式打开一个文件
        ios::trunk        删除文件原来已存在的内容
        ios::nocreate        如果打开的文件不存在则open函数无法进行
        ios::noreplece        打开的文件已经存在则用open打开返回一个错误
        ios::beg        文件指针指向文件头
        ios::end        文件指针指向文件尾

        seekg()                get读文件的指针
        seekp()                put写文件的指针
        seekp/seekg(Para1,Para2) Para1表示偏移地址(+-向前向后),Para2表示基地址(ios::beg,ios::end)

        cin.ignore(100,'\n')是在输入回车之后清空输入回车前的缓冲区,使下次cin没影响。


        自己写的一个文件操作
#include<iostream>
#include<fstream>
#include<string>

void Mnue()
{
        std::cout << "菜单内容:\n";
        std::cout << "1.查看文件\n"
                          << "2.添加内容\n"
                          << "3.退出程序\n"
                          << std::endl;
}

void main()
{
        std::fstream fp ("11.txt",std::ios::in | std::ios::out);
        std::ofstream fout;
        if( !fp )
        {
                std::cout << "打开文件失败\n" ;
        }
        else
        {
                std::cout << "姓名        学号        性别\n";
                const unsigned int SIZE = 100;
                char buffer;
                while( !fp.eof() )
                {
                        fp.getline( buffer, SIZE, '\n');
                        std::cout << buffer << std::endl;
                }
                fp.close();
        }
       
        Mnue();

        int input;
        while(1)
        {
                std::cin >> input;
                std::cin.ignore( 100, '\n');
                switch(input)
                {
                case 1:
                        fp.open ("11.txt",std::ios::in | std::ios::out);

                        if( !fp.is_open() )
                        {
                                std::cout << "打开文件失败\n";
                        }
                        else
                        {
                                const unsigned int SIZE = 100;
                                char buffer;

                                fp.clear ();
                                while( !fp.eof() )
                                {
                                        fp.getline( buffer, SIZE, '\n');
                                        std::cout << buffer << std::endl;
                                }
                                fp.close();
                        }
                        break;

                case 2:
                        fout.open ("11.txt",std::ios::app);
                        if( !fout.is_open() )
                        {
                                std::cout << "打开文件失败\n";
                        }
                        else
                        {
                                const unsigned int SIZE = 100;
                                char buffer;
                                std::cin >> buffer;
                                fout << '\n' << buffer;
                                fout.close();
                        }
                        break;
        }
       
}
}
页: [1]
查看完整版本: C++上天之路3-4课笔记