社区
C++ 语言
帖子详情
如何以二进制的形式读写文件记录
didoleo
2005-04-16 11:47:05
c++如果在没有数据库做存储数据的话,用文件来存储记录怎么做?怎么读写?
能给出代码吗?
...全文
337
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
文件
相关
这篇博客展示了如何使用C语言的fwrite和fread函数进行
二进制
文件
的
读写
操作。示例代码创建了一个包含学生信息的结构体数组,并将其以
二进制
形式
写入
文件
'score2.txt'。之后,通过fseek和fread函数读取指定位置的
记录
,演示了如何查找和打印
文件
中的特定
记录
。
Hadoop基于
文件
的数据结构及实例
本文介绍了Hadoop中的SequenceFile与MapFile两种
文件
格式。SequenceFile是一种用于存储
二进制
形式
的键值对的平面
文件
,支持
记录
级或块级压缩。MapFile则是在SequenceFile基础上增加了索引功能,支持快速查找。文章还提供了这两种
文件
的
读写
实例。
实验七实验报告
博客
记录
了
文件
操作的验证性实验,包括feof()判断
文件
结束、
文件
打开方式及字符
读写
函数使用,还对比了文本
文件
和
二进制
文件
的区别,文本
文件
每个字节存一个ASCII码,
二进制
文件
按内存存储
形式
原样输出,编程练习未得到正确结果。
实验七
该博客
记录
了
文件
操作的验证性实验和编程练习。验证性实验包括将
文件
中小写字母转大写另存、
二进制
文件
读取等。还阐述了
二进制
文件
与文本
文件
的区别,如
读写
函数、存储
形式
不同。最后作者分享了实验总结与体会。
java找出csv股价,近20年五粮液股价分析|CSV
文件
实战处理
本文介绍了Python中使用csv模块处理CSV
文件
的基本操作,包括读取和写入。读取CSV
文件
时,可以通过reader函数获取数据,可以将数据以元组或字典
形式
存储。写入CSV
文件
则使用writer函数。实战案例中,展示了如何从网上下载股票数据,读取并筛选出成交量超过2亿元的
记录
,以及如何获取最高价格和对应日期。此外,还强调了使用
二进制
模式
读写
文件
的重要性,以避免跨平台的字符转换问题。
C++ 语言
65,210
社区成员
250,514
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章