社区
C++ 语言
帖子详情
如何以二进制的形式读写文件记录
didoleo
2005-04-16 11:47:05
c++如果在没有数据库做存储数据的话,用文件来存储记录怎么做?怎么读写?
能给出代码吗?
...全文
342
11
打赏
收藏
如何以二进制的形式读写文件记录
c++如果在没有数据库做存储数据的话,用文件来存储记录怎么做?怎么读写? 能给出代码吗?
复制链接
扫一扫
分享
举报
写回复
配置赞助广告
用AI写文章
11 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
arrowcy
2005-04-17
打赏
举报
回复
如果用C++的输入输出流的话
就是在打开的时候加上一个ios::binary参数就行了
然后用read来读,write来写
arrowcy
2005-04-17
打赏
举报
回复
pcboyxhy(-273.15℃) 具的两个例子把两种情况都说了
如果使用C标准库的话,用fopen打开文件,wb表示写二进制文件,rb表示读二进制文件
例如pcboyxhy(-273.15℃) 的那段程序中的
outfile=fopen("log.txt", "wb");
infile=fopen("log.txt", "rb");
打开以后,用fwrite写文件,fread读文件
fwrite(地址,大小, 个数, 文件指针);
fread(地址, 大小, 个数, 文件指针);
完成之后
fclose(文件指针);就可以了
llmsn
2005-04-17
打赏
举报
回复
// EXER1_7.cpp : Defines the entry point for the console application.
//
//从文件(data.txt)中读入每个字到一个vector<string>对象中,遍历该对象
//将内容显示到cout,然后用泛型算法sort()对所有的文字排序
//然后再输出到文件(odata.txt)中
//#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
ifstream infile("data.txt");
ofstream outfile("odata.txt");
vector<string> s;
string str;
while(infile>>str)
{
s.push_back(str);
}
sort(s.begin(),s.end());
for(int i=0;i<s.size();i++)
{
str=s[i];
cout<<s[i]<<endl;
outfile<<str<<endl;
}
system("Pause");
//printf("Hello World!\n");
return 0;
}
llmsn
2005-04-17
打赏
举报
回复
这些一般的C++书上都写得很清楚的了,看看书吧.
mengxiangfengwz
2005-04-17
打赏
举报
回复
我也需要呢
domestic007
2005-04-17
打赏
举报
回复
查书
oyljerry
2005-04-17
打赏
举报
回复
#include <vector>
didoleo
2005-04-17
打赏
举报
回复
to: llmsn(若虚)
vector<string> s
如果我要在vector里放自己定义的类可以吗?
比如我有个Hotel类,
可以vectro<Hotel> h吗?
我编译时出错
error C2065: 'vector' : undeclared identifier
error C2275: 'Hotel' : illegal use of this type as an expression
error C2065: 'H' : undeclared identifier
zhangfjj
2005-04-16
打赏
举报
回复
看看书是最好的,书上说得很明白了!
llf_hust
2005-04-16
打赏
举报
回复
typedef struct
{
char name[20];
int age;
char number[20];
}student;
student stu[10];
fwrite((char*)&stu[i], sizeof(stu[i]), 1, outfile); 写1条大小为stu[i] 的记录到文件outfile
fread((char*)&stu, sizeof(stu), 1, infile);从文件infile读条大小为stu的记录到stu
pcboyxhy
2005-04-16
打赏
举报
回复
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char name[20];
int age;
char number[20];
}student;
void save( );
void show( );
void error( );
int main(int argc, char *argv[])
{
puts("输入 1 输入学生数据并存盘");
puts("输入 2 读入学生数据并显示");
puts("输入 3 退出");
int cho;
scanf("%d", &cho);
if(cho==1)
save( );
if(cho==2)
show( );
system("PAUSE");
return 0;
}
void save( )
{
FILE *outfile;
student stu[10];
int i, k;
outfile=fopen("log.txt", "wb");
if(!outfile)
error();
for(i=0; i<10; i++)
{
printf("\n\n输入第 %d 个学生的信息\n", i+1);
puts("依次是 姓名 年龄 学号");
k=scanf("%s%d%s", &stu[i].name, &stu[i].age, &stu[i].number);
if(k!=3)
{
puts("输入错误");
i--;
continue;
}
fwrite((char*)&stu[i], sizeof(stu[i]), 1, outfile);
}
fclose(outfile);
}
void show( )
{
FILE *infile;
student stu;
int i;
infile=fopen("log.txt", "rb");
if(!infile)
error();
for(i=1; i<=10; i+=2)
{
printf("\n读出第 %d 个学生的信息\n", i);
puts("依次是 姓名 年龄 学号");
fread((char*)&stu, sizeof(stu), 1, infile);
printf("%s\t%d\t%s\n", stu.name, stu.age, stu.number);
fseek(infile, sizeof(stu), SEEK_CUR);
}
fclose(infile);
}
void error( )
{
puts("Error");
system("Pause");
exit(0);
}
-------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
struct student
{
char Serial[9]; //学号
char Name[40]; //姓名
char enableUse; //是否到校(用1表示到,用0表示没有到校!)
};
int main(int argc, char *argv[])
{
fstream test("student.txt", ios::binary|ios::out);
student f;
strcpy(f.Serial, "03055061");
strcpy(f.Name, "杨惠杰");
f.enableUse = '0';
test.write((char*)&f, sizeof(f));
strcpy(f.Serial, "03055062");
strcpy(f.Name, "杨元伟");
f.enableUse = '0';
test.write((char*)&f, sizeof(f));
test.close( );
test.clear();
test.open("student.txt", ios::binary|ios::in); //show file
while(1)
{
test.read((char*)&f, sizeof(f));
if(test.eof( )) break;
cout<<f.Serial<<endl<<f.Name<<endl<<f.enableUse<<endl<<endl;
}
test.close();
test.clear();
test.open("student.txt", ios::binary|ios::in|ios::out); //modify
while(1)
{
test.read((char*)&f, sizeof(f));
if(!strcmp(f.Serial, "03055061"))
{
test.seekp(-sizeof(f), ios_base::cur);
f.enableUse = '1';
test.write((char*)&f, sizeof(f));
break;
}
}
test.close();
test.clear();
test.open("student.txt", ios::binary|ios::in); //show file
while(1)
{
test.read((char*)&f, sizeof(f));
if(test.eof( )) break;
cout<<f.Serial<<endl<<f.Name<<endl<<f.enableUse<<endl<<endl;
}
test.close();
system("PAUSE");
return 0;
}
C fwrite和fread
文件
相关
fwrite和fread函数用于
二进制
文本
读写
,按数据块读
文件
而fprintf和fscanf函数用于文本
文件
,以ASCII码存储的
文件
读写
fseek函数好像也主要用于
二进制
文件
(第二段代码如果我是文本
文件
,不改变fseek函数语句,会出现错误) //将内容先以
二进制
形式
存到
文件
中 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct student { long id
Hadoop基于
文件
的数据结构及实例
基于
文件
的数据结构 两种
文件
格式: 1、SequenceFile 2、MapFile SequenceFile 1、SequenceFile
文件
是Hadoop用来存储
二进制
形式
的<key,value>对而设计的一种平面
文件
(Flat File)。 2、能够把SequenceFile当做一个容器,把全部
文件
打包到SequenceFile类中能够高效的对...
python学习_四
2020.04.30 今天是四月最后一天。学校前两天已经通知不用返校,唉,20届毕业生太难了。。。。 ---------------------------------正文分割线 python中的异常捕获 和java等高级语言一样,都采用: try except finally 的
形式
,但是在python中还可以在except后面加上else语句,当没有异常发生的时候执行else语句。 Pyth...
c语言
文件
文件
:存储在外部介质上数据的集合,是操作系统数据管理的单位。 一、按
文件
的分类 按
文件
的逻辑结构:
记录
文件
:由具有一定结构的
记录
组成(定长和不定长) 流式
文件
:由一个个字符(字节)数据顺序组成 按存储介质: 普通
文件
:存储介质
文件
(磁盘、磁带等) 设备
文件
:非存储介质(键盘、显示器、打印机等) 按
文件
的内容 程序
文件
。程序
文件
又可分为源
文件
、目标
文件
和可执行
文件
。 数据
文件
。如各种图像
文件
、声音...
C/C++
文件
读写
操作(
二进制
文件
&文本
文件
)
C/C++读取
文件
操作包括
二进制
和文本
文件
。
C++ 语言
65,214
社区成员
250,514
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章