⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file_r02.cpp

📁 数据结构常用算法合集
💻 CPP
字号:
//file_r02.cpp
#include <fstream.h>
#include <conio.h>					//getch()
void main()
{ int data,n, filesize;
  char ch;
  ifstream infile;
  infile.open("random.dat",ios::in|ios::out|ios::binary);
  infile.seekg(0,ios::end);				//设置到文件尾
filesize = infile.tellg();				//求文件大小或目前位置
  cout << "目前有 " << filesize / sizeof(int) << " 个数据\n";
  do
  { cout << "输入要读取第几个:"; cin >> n;
    if ((n>0) && (n<= filesize / sizeof(int)))
  { int position = (n-1)*sizeof(int);
  cout << "position=" << position << endl;
  infile.seekg(position);
  infile.read((char*)&data,sizeof(int));
  cout << data << endl;
  }
  cout << "再读取吗(Y/N)?"; cin >> ch;
  }while ((ch=='y')||(ch=='Y'));
  cout << "文件数据如下:\n";
  infile.seekg(0);
  infile.read((char*)&data,sizeof(int));	//读取数据
  while (!infile.eof())				//EOF就结束
  { cout << data << ",";
  infile.read((char*)&data,sizeof(int));	//读取数据
  }
  cout << endl;
getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -