如何以二进制的形式读写文件记录

didoleo 2005-04-16 11:47:05
c++如果在没有数据库做存储数据的话,用文件来存储记录怎么做?怎么读写?
能给出代码吗?
...全文
300 11 打赏 收藏 转发到动态 举报
写回复
用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;
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧