C++二进制文件读写
简单二进制文件读写,多文件
/*Demo9.1.cpp*/
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include "UserList.h"
using namespace std; int showMain()
{
system("cls");
cout<<"1.增加"<<endl;
cout<<"2.删除"<<endl;
cout<<"3.修改"<<endl;
cout<<"4.查询"<<endl;
cout<<"0.退出"<<endl;
cout<<"请选择:";
cin.sync();
int c;
cin>>c;
return c;
} void ADD(UserList &L)
{
cout<<"input:"<<endl;
string id,name,password;
cin>>id>>name>>password;
User u(id,name,password);
L.Add(u);
}
void DEL(UserList &L)
{cerr<<"功能未定义"<<endl;Sleep();}
void REV(UserList &L)
{cerr<<"功能未定义"<<endl;Sleep();}
void SEE(UserList &L)
{
cout<<"表:"<<endl;
for(int i=;i<L.getN();i++)
L.display(i);
cout<<endl;
system("pause");
} int main()
{
UserList L;
// User admin("0","admin","admin");
// L.Add(admin);
bool flag=true;
while(flag)
{
switch(showMain())
{
case :flag=false;break;
case :ADD(L);break;
case :DEL(L);break;
case :REV(L);break;
case :SEE(L);break;
default:break;
}
}
Sleep();
return ;
}
Demo9.1.cpp
/*UserList.h*/
#include <fstream>
#include "User.h"
using namespace std; class UserList
{
protected:
User *List;
int N,nMax;
string FileName;
public:
UserList(/*string Fn=""*/)
{
List=new User[];
N=;
nMax=;
// FileName=Fn;
FileName="D:\\#\\Users.dat";
readFile();
}
//
bool readFile()
{
ifstream fin(FileName.c_str(),ios::binary);
if(!fin)
return false;
fin.seekg(,ios::end);
int FileLen=fin.tellg(),len=sizeof(User);
fin.seekg(,ios::beg);
fin.read((char *)List,(N = FileLen<nMax*len?FileLen/len:nMax)*len);
fin.close();
/*
printf("*%d*",N);
for(int i=0;i<N;i++)
display(i);
cout<<endl;
*/
return true;
}
int Add(User u)
{
if(N<nMax)
{
List[N]=u;
ofstream fout(FileName.c_str(),ios::app|ios::binary);
int len=sizeof(User);
fout.seekp((N)*len);
fout.write((char *)&List[N],len);
fout.close(); N++;
return N-;
}//'else-重分配内存'或'使用vector容器'解决数据溢出
return -;
}
void idFind(string id)
{
}
void nameFind(string name)
{
}
void display(int i)
{cout<<"ID:"<<List[i].id<<endl<<"Name:"<<List[i].name<<endl<<"Password:"<<List[i].password<<endl;}
int getN()
{return N;}
};
UserList.h
/*User.h*/
#include <iostream>
#include <string>
using namespace std; class User
{
friend class UserList;
protected:
string id;
string name;
string password;
public:
User(string id="",string name="",string password=""):id(id),name(name),password(password)
{}
};
User.h
后序慢慢补充
C++二进制文件读写的更多相关文章
- [转载:]Fortran 二进制文件读写
一些朋友总是咨询关于二进制文件的读写和转化.这里就我自己的理解说一说. 一).一般问题 二进制文件与我们通常使用的文本文件储存方式有根本的不同.这样的不同很难用言语表达,自己亲自看一看,理解起来会容易 ...
- JAVA核心技术I---JAVA基础知识(二进制文件读写和zip文件读写)
一:二进制文件读写 (一)写文件 –先创建文件,写入数据,关闭文件 –FileOutputStream, BufferedOutputStream,DataOutputStream –DataOutp ...
- Qt 学习之路 2(36):二进制文件读写
Qt 学习之路 2(36):二进制文件读写 豆子 2013年1月6日 Qt 学习之路 2 20条评论 在上一章中,我们介绍了有关QFile和QFileInfo两个类的使用.我们提到,QIODevice ...
- Qt 二进制文件读写(使用“魔术数字”)
今天开始进入 Qt 的另一个部分:文件读写,也就是 IO.文件读写在很多应用程序中都是需要的.Qt 通过 QIODevice 提供了IO的抽象,这种设备(device)具有读写字节块的能力.常用的IO ...
- Qt 学习之路:二进制文件读写
在上一章中,我们介绍了有关QFile和QFileInfo两个类的使用.我们提到,QIODevice提供了read().readLine()等基本的操作.同时,Qt 还提供了更高一级的操作:用于二进制的 ...
- Qt 学习 之 二进制文件读写
在上一章中,我们介绍了有关QFile和QFileInfo两个类的使用.我们提到,QIODevice提供了read().readLine()等基本的操作.同时,Qt 还提供了更高一级的操作:用于二进制的 ...
- 8.7 C++二进制文件读写操作
参考:http://www.weixueyuan.net/view/6413.html 总结: 二进制文件的读写稍微麻烦一些,对二进制文件的读写同样需要打开文件和关闭文件,打开和关闭方式与文本文件相同 ...
- VB二进制文件读写
数组存取 存数组 Private Sub Command2_Click() Dim fileNumber As Integer Dim S9 As String Dim k As Integer Di ...
- C++ 输入输出流 文本文件 二进制文件读写
文本文件/ASCII文件(能直接显示内容,费存储空间):文件中每一个字节中均以ASCII代码形式存放数据,即一个字节存放一个字符,这个文件就是ASCII文件或称字符文件. 二进制文件(不能显示内容,节 ...
随机推荐
- PKUSC2018 Slay The Spire
有攻击牌和强化牌各 $n$ 张,强化牌可以让之后所有攻击牌攻击力乘一个大于 $1$ 的系数,攻击牌可以造成伤害 求所有“抽出 $m$ 张然后打 $k$ 张”能造成的伤害之和 $k,m,2n \leq ...
- Shiro-从数据表中初始化资源和权限
我们在 applicationContext中配置受保护的资源和权限的关系 <property name="filterChainDefinitions"> <v ...
- Yii 查询 搜索
一.视图 <div class="form-horizontal"> <?php $form = $this->beginWidget('CActiveFo ...
- C#关闭一个窗口的同时打开另一个窗口
在.net的WinForm程序中,如果是直接起动的Form作为主窗口,那么这个主窗口是不能关闭的,因为它维护了一个Windows消息循环,它一旦关闭了就等于声明整个应用程序结束,所以新打开的窗口也就被 ...
- SQL Server 学习系列之六
SQL Server 学习系列之六 SQL Server 学习系列之一(薪酬方案+基础) SQL Server 学习系列之二(日期格式问题) SQL Server 学习系列之三(SQL 关键字) SQ ...
- unix下网络编程之I/O复用(四)
首先需要了解的是select函数: select函数 #include<sys/select.h> #include<sys/time.h> int select (int m ...
- UVA548(二叉树遍历)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- [cinder] volume type 使用简记
cinder type-create sharecinder type-key share set volume_backend_name=GLUSTERFScinder type-create lo ...
- Java多线程中的常用方法
本文将带你讲诉Java多线程中的常用方法 Java多线程中的常用方法有如下几个 start,run,sleep,wait,notify,notifyAll,join,isAlive,current ...
- 第八课 go的条件语句
1 if ... else package main import "fmt" func main() { flag:= { fmt.Println("flag > ...