怎样查询网站是否备案营销型网站的类型有哪些
在学习C++文件流控制时(链接)我们知道C++有一个标准库fstream
该库定义了三个数据类型 ofstream ifstream 和 fstream
在练习相应的案例时,seekg() 函数掌握的不是很好,后经过多次尝试,可以正常调用了
代码如下:
#include <fstream>
#include <iostream>
using namespace std;int main()
{char data[100]; 以写模式打开文件//ofstream outfile;//outfile.open("new.out");//cout << "Writing to the file" << endl;//cout << "Enter your name: ";//cin.getline(data, 100); 向文件写入用户输入的数据//outfile << data << endl;//cout << "Enter your name: ";//cin.getline(data, 100); 向文件写入用户输入的数据//outfile << data << endl;//cout << "Enter your name: ";//cin.getline(data, 100); 向文件写入用户输入的数据//outfile << data << endl;//cout << "Enter your name: ";//cin.getline(data, 100); 向文件写入用户输入的数据//outfile << data << endl;//cout << "Enter your name: ";//cin.getline(data, 100); 向文件写入用户输入的数据//outfile << data << endl;//cout << "Enter your name: ";//cin.getline(data, 100); 向文件写入用户输入的数据//outfile << data << endl;//cout << "Enter your age: ";//cin >> data;//cin.ignore(); 再次向文件写入用户输入的数据//outfile << data << endl; 关闭打开的文件//outfile.close();char ch;// 以读模式打开文件fstream infile;infile.open("new.out");cout << "Reading from the file" << endl;infile >> data;// 在屏幕上写入数据cout << data << endl;cout << infile.tellg() << endl;// 再次从文件读取数据,并显示它infile >> data;cout << data << endl;cout << "a line" << endl;cout << infile.tellg() << endl;infile.get(ch);cout << ch << endl;infile.seekg(0L, ios::cur);cout << infile.tellg() << endl;infile.get(ch);cout << ch << endl;6infile.seekg(-1L, ios::cur);cout << infile.tellg() << endl;infile.get(ch);cout << ch << endl;infile.seekg(-1L, ios::cur);cout << infile.tellg() << endl;infile.get(ch);cout << ch << endl;infile.seekg(-2L, ios::cur);cout << infile.tellg() << endl;infile.get(ch);cout << ch << endl;//cout << infile.rdbuf() << endl;cout << "a line" << endl;//1111infile.seekg(-3, ios::end);infile.get(ch);cout << ch << endl;//6infile.seekg(-8, ios::end);//infile.get(ch);//cout << ch << endl;cout << infile.rdbuf() ;//3infile.seekg(0, ios::beg);infile.get(ch);cout << ch << endl;cout << 'a line' << endl;// 关闭打开的文件infile.close();return 0;
}
这段代码前半段负责写入程序,后半段从文件中读取数据
需要注意以下几点:
- 在读取文件时,实例化 fstream 和 ifstream 均可
- 使用
infile.tellg()
追踪文件指针的位置 - 使用
cout << infile.rdbuf() ;
输出指针所在处的整个单词 ios::cur
在当前指针位置处跳跃ios::beg
从头开始跳跃ios::end
从后往前遍历- 使用
ios::end
时,如果想向前遍历,需要输入负的步长 infile >> data;
整行输出
关于ios::cur
指针部分还是有点迷糊,可以先通过infile.tellg()
考察指针移动情况,需要用到的时候再深入学习。