C++IO流

C++IO流

C++流是指信息从外部输入设备(键盘)向计算机内部输入和从内存向外设(显示器)输出的过程,这样的输入输出称作“流”。
这里写图片描述
流分为两种,一种是及时输出的,一种带缓冲区的
C++中定义了标准流类库,来完成各种功能
其中,定义了四个全局流对象:cin’标准输入流对象,标准设备为键盘,cout标准输出流对象,标准设备为显示器,cerr和clog为标准错误输出流,输出设备为显示器。
其中:cin为缓冲流,输入的数据保存在缓冲区中,需要时从缓冲区提取,输入时一旦按下回车键就无法进行修改;输入数据的类型与提取的数据类型要保持一致

菱形继承
这里写图片描述

#include <iostream>
#include <string>
using namespace std;

class Data
{
private:
    int _year;
    int _month;
    int _day;
};
int main()
{
    //支持内置类型的输入输出
    int i;
    cin >> i;
    cout << i << endl;

    //不支持,未在Data类中对操作符"<<"进行重载
    Data d;
    cout << d;

    //支持,string类中已重载">>"
    string s;
    cin >> s;
}

//流结束是检测到流结束的标志时才会结束
//即通过键盘输入给它发信号

    //会将空格替换成换行
    while (cin >> s)
    {
        cout << s << endl;
    }

getline:按行输入
istream& getline (char* s, streamsize n );

// istream::getline example
#include <iostream>     // std::cin, std::cout

int main () {
  char name[256], title[256];

  std::cout << "Please, enter your name: ";
  std::cin.getline (name,256);

  std::cout << "Please, enter your favourite movie: ";
  std::cin.getline (title,256);

  std::cout << name << "'s favourite movie is " << title;

  return 0;
}

使用时需要指定字符数组的大小,过大过小都不太好
我们可以使用string类中的重载的非成员函数getline
istream& getline (istream& is, string& str);
//Get line from stream into string
通常建议使用这种

// extract to string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string name;

  cout << "Please, enter your full name: ";
  getline (cin,name);
  cout << "Hello, " << name << "!\n";

  return 0;
}

C中文件操作

fwrite:二进制写
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

fread:二进制读
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
二进制读写是直来直去的过程,在内存中是什么形式,存在磁盘中就是什么形式,没有任何中间处理过程,优点是快,缺点不能直观的看到文件中的内容,因为都是乱码
以下实现将一个学生信息写入文件:

//C++文件操作
struct stuinfo
{
    char name[10];
    int age;
    int tel;
};
void WriteFile()
{
    stuinfo stu1;
    strcpy(stu1.name, "sdt");
    stu1.age = 18;
    stu1.tel = 110;

    FILE* fout = fopen("通讯录", "w");
    assert(fout);
    fwrite(&stu1, sizeof(stuinfo), 1, fout);
    fclose(fout);
}
void ReadFile()
{
    stuinfo stu1;
    FILE* fin = fopen("通讯录", "r");
    fread(&stu1, sizeof(stuinfo), 1, fin);
    fclose(fin);
    cout << stu1.name << endl;
    cout << stu1.age << endl;
    cout << stu1.tel << endl;
}
int main()
{
    WriteFile();
    ReadFile();
    return 0;
}

向文件中写内容后查看文件是这样的:
这里写图片描述
读取文件并输出:
这里写图片描述


fputs:文本写
int fputs ( const char * str, FILE * stream );
fgets:文本读
char * fgets ( char * str, int num, FILE * stream );
若要用这种文本的方式处理上述情况比较麻烦,需要将整形转成字符串存储在一个缓冲区中,然后进行向文件中读写。
Write string to stream

/* fputs example */
#include <stdio.h>

int main ()
{
   FILE * pFile;
   char sentence [256];

   printf ("Enter sentence to append: ");
   fgets (sentence,256,stdin);
   pFile = fopen ("mylog.txt","a");
   fputs (sentence,pFile);
   fclose (pFile);
   return 0;
}

C++文件操作

C++与C不同的是C++都是通过对象进行操作,fstream文件流的对象可以调用其中的write,read,get,put等方法来对文件进行操作,还有对 << 和 >> 也进行了重载,可直接将内容输出到文件中或从文件中读取到某个缓冲区内。

文件操作的步骤:
1.定义一个文件流对象
ifstream ifs (用于输入)
ofstream ofs(用于输出)
iofstream iofs(可输入输出)
2.使用文件流对象的成员函数打开一个磁盘文件,使文件流对象和磁盘文件之间建立联系
3.使用成员函数或提取插入运算符对文件进行读写操作
4.关闭文件

read:Read block of data
istream& read (char* s, streamsize n);
write:Write block of data
ostream& write (const char* s, streamsize n);

#include <fstream>
struct stuinfo
{
    char name[10];
    int age;
    int tel;
};
void WriteFile()
{
    stuinfo stu1;
    strcpy(stu1.name, "sdt");
    stu1.age = 18;
    stu1.tel = 110;

    //创建一个输出文件流对象
    ofstream ofs("通讯录 ");
    //ofs.write((const char*)&stu1, sizeof(stuinfo));

    ofs << stu1.name << endl;
    ofs << stu1.age << endl;
    ofs << stu1.tel << endl;
    //此时不用调用fclose关闭文件,因为函数结束,
    //对象销毁会自动调用析构函数来关闭这个输出流
}
void ReadFile()
{
    stuinfo stu1;
    ifstream ifs;//创建一个输入文件流对象

    ifs >> stu1.name;
    ifs >> stu1.age;
    ifs >> stu1.tel;

    cout << stu1.name << endl;
    cout << stu1.age << endl;
    cout << stu1.tel << endl;
}
int main()
{
    WriteFile();
    ReadFile();
    return 0;
}

相比于C,C++中的实现方式显得更为简单直观

总结:
C语言读写文件均通过FILE指针执行操作,其中文本文件的读写用fprintf,fscanf,二进制文件的读写用fread,fwrite
C++读写文件通过fstream,ifstream,ofstream进行操作,文本文件用<< 和>> 进行读写,二进制文件用read和write进行读写

猜你喜欢

转载自blog.csdn.net/shidantong/article/details/80864106