社区
C++ 语言
帖子详情
如何以二进制的形式读写文件记录
didoleo
2005-04-16 11:47:05
c++如果在没有数据库做存储数据的话,用文件来存储记录怎么做?怎么读写?
能给出代码吗?
...全文
306
11
打赏
收藏
如何以二进制的形式读写文件记录
c++如果在没有数据库做存储数据的话,用文件来存储记录怎么做?怎么读写? 能给出代码吗?
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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++中
文件
以
二进制
形式
和以文本
形式
打开的区别
若要以
二进制
形式
读写
特定数据类型,应使用`fread`、`fwrite`、`ifstream::read`或`ofstream::write`等函数。 ### 结论 理解C++中
文件
以
二进制
形式
和以文本
形式
打开及操作的区别,是编写高效、可靠的
文件
处理程序...
采用read()和write()
读写
二进制
文件
以文本
形式
读写
文件
和以
二进制
形式
读写
文件
的区别,并掌握了用重载的 >> 和 << 运算符实现以文本
形式
读写
文件
。在此基础上,本节继续讲解如何以
二进制
形式
读写
文件
。举个例子,现在要做一个学籍管理程序...
C++ read()和write()
读写
二进制
文件
(超级详细)
通过前一节的学习,读者了解了以文本
形式
读写
文件
和以
二进制
形式
读写
文件
的区别,并掌握了用重载的 >> 和 << 运算符实现以文本
形式
读写
文件
。在此基础上,本节继续讲解如何以
二进制
形式
读写
文件
。 不过...
C#
二进制
文件
读写
操作全解
二进制
文件
读写
是指直接对
文件
系统中的数据进行字节级的读取和写入操作。与文本
文件
的字符
读写
不同,
二进制
读写
操作直接处理
文件
中的原始字节数据,这使得它能够处理任何类型的数据
文件
。FileStream是.NET Framework...
C++ (
二进制
)
读写
文件
偷偷拿来
记录
一下萌新的cs路——day 45 C++
读写
文件
C++ 语言
65,189
社区成员
250,526
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章