宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

ASCII和二进制文件的输入输出

First包含头文件#include <fstream> 

ASCII输入:

  首先要创建一个in-stream对象:ifstream fin(“input.txt”);

  逐词读取:fin>>num>>s;

       读取过程中遇到空白符,>>操作符就会停止读取内容,知道遇到另一个>>操作符。

  逐行读取:fin.getline(sentence, num);

       第一个参数用来接受char数组;第二个参数是在遇到换行符之前,数组允许接受的最大元素数量。

 ASCII输出:

  首先声明一个ofstream-fout类对象(打开文件):ofstream fout(“output.txt”);

  操作举例:

    int num = 150;
    char name[] = “John Doe”;
    fout << “Here is a number: ” << num << “/n”;
    fout << “Now here is a string: ” << name << “/n”;

  关闭文件(自动保存文件),或者回写文件缓冲(保持文件打开的情况下保存文件):

    fout << flush; fout.close();

二进制输入输出:

  声明:不再使用插入(<<)和提取(>>)操作符,必须使用read()和方法write()读取和写入二进制文件。

  First:以二进制方式打开文件——ofstream fout(“file.dat”, ios::binary); 

  写入:write()——共两个参数,第一个是指向对象的char类型的指针, 第二个是对象的大小(译者注:字节数)。

    操作举例:

      int number = 30; fout.write((char *)(&number), sizeof(number)); 

      第一个参数写作”(char *)(&number)”,这是把一个整型变量转为char *指针。第二个参数写作”sizeof(number)”,sizeof() 返回对象大小的字节数。

    优势:复杂结构体的写入——如果使用ASCII方式写入,只能一条一条的写入所有的成员变量。

    操作举例:

      struct OBJECT { int number; char letter; } obj;
      obj.number = 15;
      obj.letter = ‘M’;
      fout.write((char *)(&obj), sizeof(obj));

  读取:read()——同write()

    ifstream fin(“file.dat”, ios::binary);

    fin.read((char *)(&obj), sizeof(obj));

算法描述:

 1 #include <iostream>
 2 #include <fstream>
 3 #include <string>
 4 
 5 using namespace std;
 6 
 7 //输出空行
 8 void OutPutAnEmptyLine()
 9 {
10     cout<<"
";
11  }
12 
13 //读取方式: 逐词读取, 词之间用空格区分
14 //read data from the file, Word By Word
15 //when used in this manner, we'll get space-delimited bits of text from the file
16 //but all of the whitespace that separated words (including newlines) was lost. 
17 void ReadDataFromFileWBW()
18 {
19     ifstream fin("data.txt");  
20     string s;  
21     while( fin >> s ) 
22     {    
23         cout << "Read from file: " << s << endl;  
24      }
25  }
26 
27 //读取方式: 逐行读取, 将行读入字符数组, 行之间用回车换行区分
28 //If we were interested in preserving whitespace, 
29 //we could read the file in Line-By-Line using the I/O  getline_r() function.
30 void ReadDataFromFileLBLIntoCharArray()
31 {
32     ifstream fin("data.txt"); 
33     const int LINE_LENGTH = 100; 
34     char str[LINE_LENGTH];  
35     while( fin. getline_r(str,LINE_LENGTH) )
36     {    
37         cout << "Read from file: " << str << endl;
38      }
39  }
40 
41 //读取方式: 逐行读取, 将行读入字符串, 行之间用回车换行区分
42 //If you want to avoid reading into character arrays, 
43 //you can use the C++ string  getline_r() function to read lines into strings
44 void ReadDataFromFileLBLIntoString()
45 {
46     ifstream fin("data.txt");  
47     string s;  
48     while(  getline_r(fin,s) )
49     {    
50         cout << "Read from file: " << s << endl; 
51      }
52  }
53 
54 //带错误检测的读取方式
55 //Simply evaluating an I/O object in a boolean context will return false 
56 //if any errors have occurred
57 void ReadDataWithErrChecking()
58 {
59     string filename = "dataFUNNY.txt";  
60     ifstream fin( filename.c_str());  
61     if( !fin ) 
62     {   
63         cout << "Error opening " << filename << " for input" << endl;   
64         exit(-1);  
65      }
66  }
67 
68 int main()
69 {
70     ReadDataFromFileWBW(); //逐词读入字符串 
71     OutPutAnEmptyLine(); //输出空行
72 
73     ReadDataFromFileLBLIntoCharArray(); //逐词读入字符数组
74     OutPutAnEmptyLine(); //输出空行
75 
76     ReadDataFromFileLBLIntoString(); //逐词读入字符串
77     OutPutAnEmptyLine(); //输出空行
78 
79     ReadDataWithErrChecking(); //带检测的读取
80     return 0;
81  }

ASCII读取方式

 ifstream 和ofstream对象

打开文件:

  ifstream fin(“input.txt”);          ofstream fout(“output.txt”);

  ifstream fin(“file.dat”, ios::binary);     ofstream fout(“file.dat”, ios::binary);

  默认打开为ASCII方式,不存在则创建,存在则覆盖。二进制方式打开文件选项ios::binary为额外标志。

  其他额外选项: 

        ios::app 添加到文件尾
        ios::ate 把文件标志放在末尾而非起始。
        ios::trunc 默认. 截断并覆写文件。
        ios::nocreate 文件不存在也不创建。
        ios::noreplace 文件存在则失败。

检查文件:

  good():返回一个布尔值,表示文件打开是否正确。

  bad():返回一个布尔值,表示文件打开是否错误。

  fail():类似于bad()。

文件状态:

  eof() :返回是否标志已经到了文件末尾。

 1 #include <stdio.h>
 2 int main(void)
 3 {
 4 int n;
 5 int array[25];
 6 while (scanf("%d",&n),n!=EOF) //while(scanf("%d",&n)!=EOF)也行
 7 {
 8 for (int i=0; i<n; i++)
 9 scanf("%d",array+i);
10 for (i=0; i<n; i++)
11 printf ("%d ",array[i]);
12 }
13 return 0;
14 }

eof()举例

 读文件:

  get():每次返回一个字符。

  ignore(int,char):跳过输入流中n个字符,或在遇到指定的终止字符时提前结束(此时跳过包括终止字符在内的若干字符)。

    原型:istream &ignore( streamsize num=1, int delim=EOF )

    功能:用于输入流。它读入字符,直到已经读了num 个字符(默认为1)或是直到字符delim 被读入(默认为EOF)

    调用方式:cin.ignore(n,终止字符)

 1 #include<iostream>
 2 usingnamespacestd;
 3 intmain()
 4 {
 5 chararray[8];
 6 cin.ignore(6,'a');
 7 cin.getline(array,8);
 8 cout<<array<<endl;
 9 return0;
10 }

ignore()举例

  peek():

    调用方式:cin.peek()

    说明:其返回值是一个char型的字符,其返回值是指针指向的当前字符,但它只是观测,指针仍停留在当前位置,并不后移。如果要访问的字符是文件结束符,则函数值是EOF(-1)。

    功能:从输入流中读取一个字符 但该字符并未从输入流中删除。即若把输入流比作一个 栈类,那么这里的peek函数就相当于栈的成员函数front  ,而cin.get()则相当于栈的成员函数pop。

 1 / istream peek
 2 #include <iostream>
 3 using namespace std;
 4 int main () {
 5 char c;
 6 int n;
 7 char str[256];
 8 cout << "Enter a number or a word: ";
 9 c=cin.peek();
10 if ( (c >= '0') && (c <= '9') )
11 {
12 cin >> n;
13 cout << "You have entered number " << n << endl;
14 }
15 else
16 {
17 cin >> str;
18 cout << " You have entered word " << str << endl;
19 }
20 return 0;
21 }

peek()举例

  putback(char):

    调用方式:cin.putback(ch)

    功能:将前面用get或者getline函数从输入流中读取的字符ch返回到输入流,插入到当前指针的位置,供后面读取。

 1 using namespace std;
 2 int main () {
 3 char c;
 4 int n;
 5 char str[256];
 6 cout << "Enter a number or a word: ";
 7 c = cin.get();
 8 if ( (c >= '0') && (c <= '9') )
 9 {
10 cin.putback (c);
11 cin >> n;
12 cout << "You have entered number " << n << endl;
13 }
14 else
15 {
16 cin.putback (c);
17 cin >> str;
18 cout << " You have entered word " << str << endl;
19 }
20 return 0;
21 }

putback()举例

写文件:

  put():向流写入一个字符。

    其原型:ofstream &put(char ch)

    操作举例:file1.put(‘c’);就是向流写一个字符’c’。

  putchar():

    调用方式:putchar(c)——加上头文件#include <stdio.h>或#include “stdio.h”。

    说明:当c为一个被单引号(英文状态下)引起来的字符时,输出该字符(注:该字符也可为转义字符);

       当c为一个介于0~127(包括0及127)之间的十进制整型数时,它会被视为对应字符的ASCII代码,输出该ASCII代码对应的字符;

       当c为一个事先用char定义好的字符型变量时,输出该变量所指向的字符。

       返回值:正确即为字符;错误即EOF文件结束符。

    功能:向终端输出一个字符。

文件定位:

  seekg():对读定位,设置输入文件流的指针位置。有两个参数,第一个是偏移量(正表示向后偏移,负表示向前偏移),第二个是基地址。

  seekp():对写定位,设置输出文件流的指针位置。

  函数原型:

      ostream& seekp( streampos pos );
      ostream& seekp( streamoff off, ios::seek_dir dir );
      istream& seekg( streampos pos );
      istream& seekg( streamoff off, ios::seek_dir dir );

  分析:

    函数参数
      pos:新的文件流指针位置值
      off:需要偏移的值
      dir:搜索的起始位置
      dir参数用于对文件流指针的定位操作上,代表搜索的起始位置
   在ios中定义的枚举类型:
      enum seek_dir {beg, cur, end};
      每个枚举常量的含义:
        ios::beg:文件流的起始位置
        ios::cur:文件流的当前位置
        ios::end:文件流的结束位置

注:这两个函数一般用于二进制文件,因为文本文件会因为系统对字符的解释而可能与预想的值不同。

  tellg()函数不需要带参数,它返回当前定位指针的位置,也代表着输入流的大小。

  而tellp()函数用于写文件时。