关于文件读写的问题

rabbit729 2007-12-30 09:34:39
我想把一个含有char*指针的结构体写入文件代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct Data
{
int a;
char * p;
}DATA;

int main()
{
DATA d_W;
char file[128];
FILE *fp;

d_W.a = 123;
d_W.p = "this is a test!";

strcpy(file,"c:\\test.txt");
fp=fopen(file,"w");
if (fp!=NULL)
{
fwrite(&d_W, sizeof(DATA), 1, fp);
fclose(fp);
}
else
printf("open faild!");

system("pause");
return 0;
}
结果文件"test.txt"的内容为:
7B 00 00 00 CC 51 42 00
其中7B 00 00 00为123没错,CC 51 42 00为结构体中char*的指针地址,我是想把"this is a test!"写进去,请问改如何实现,我试了用char p[20]数组可以实现,但是如果这个字符串是变长的话这样就不好办,请问char*的该如何实现?
上面的test1.txt文件我用下面一段代码读
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct Data
{
int a;
char * p;
}DATA;

int main()
{

DATA d_W, *d_R;
char file[128];
FILE *fp;

d_R = (DATA*)malloc(sizeof(DATA));

d_W.a = 123;
d_W.p = "this is a test!";

strcpy(file,"c:\\test.txt");
fp=fopen(file,"w");
if (fp!=NULL)
{
fwrite(/*(const void*)*/&d_W, sizeof(DATA), 1, fp);
fclose(fp);
}
else
printf("open faild!");

fp=fopen(file,"r");
if (fp!=NULL)
{
fread(/*(const void*)*/d_R, sizeof(DATA), 1, fp);
fclose(fp);

printf("%d\n",d_R->a);
printf("%s\n",d_R->p);
}
else
printf("open faild!");
system("pause");
return 0;
}
结果显示:
123
后面的字符串由于读的是指针,并且指针为空所以显示空字符,请问这种Char*的该如何读写?
谢谢各位!!!祝大家新年快乐!
...全文
114 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ryfdizuo 2007-12-30
  • 打赏
  • 举报
回复

还是这个问题啊,
//用C++写了一下, 效率比较低的,C语言不怎么熟悉了, 不用忘了,

//写文件,每个结构体记录占一行;
#include <string>
#include <cassert>
#include <fstream>
#include <iostream>
using namespace std;

typedef struct Data
{
int a;
//char *p;
string str;
}DATA;

int main()
{
DATA d_W;

d_W.a = 123;
d_W.str = "this is a test!";

ofstream outFile("c:\\test.txt");
assert(outFile!=NULL);
outFile<<d_W.a<<" ";
for(int i=0; i<strlen(d_W.str.c_str()); i++) outFile<<d_W.str[i];
outFile<<endl;
outFile.close();

cout<<d_W.a<<d_W.str<<endl;
system("pause");
return 0;
}

//读取记录;先将内容读取到string的line中,在取分割;取出内容;
#include <string>
#include <cassert>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;

//字符串分割函数;
vector< string> split(string s, string delim)
{
int last = 0;
vector<string> ret;
for(int i = 0; i + delim.size() <= s.size(); i++)
{
bool ok = true;
for(int j = 0; j < delim.size() && ok; j++)
ok = s[i + j] == delim[j];
if(ok)
{
if(i - last) ret.push_back(s.substr(last, i - last));
last = i + delim.size();
}
}
if(last < s.size()) ret.push_back(s.substr(last));
return ret;
}

typedef struct Data
{
int a;
string str;
}DATA;

int main()
{
DATA data;
ifstream inFile("c:\\test.txt");
assert(inFile!=NULL);

string line;
getline(inFile, line, '\n');
vector<string>& dest=split(line, string(" ")); //空格分割;
data.a=atoi(dest[0].c_str()); //得到数据;
for(int i=1;i<dest.size(); i++) dest[i].append(" ");//追加空格;
for(int i=1;i<dest.size(); i++) data.str+=dest[i]; //得到DATA::str;

inFile.close();

cout<<data.a<<endl;
cout<<data.str<<endl;
system("pause");
return 0;
}

rabbit729 2007-12-30
  • 打赏
  • 举报
回复
怎么没人回答?期待高手!!!!!
rabbit729 2007-12-30
  • 打赏
  • 举报
回复
我想知道的是如何读取结构体,也就是如何读取数据块并且数据块中有些项还是变长的
ryfdizuo 2007-12-30
  • 打赏
  • 举报
回复
[code=C/C++]
ofstream outFile("c:\\test.txt");
assert(outFile!=NULL);
outFile<<... //此时outFile就相当于cout,使用

ifstream inFIle("c:\\test.txt");
assert(inFile!=NULL);
inFile>>... //与cin用法一样,
/code]
rabbit729 2007-12-30
  • 打赏
  • 举报
回复
C++怎么实现呀?请指点?谢谢!
ryfdizuo 2007-12-30
  • 打赏
  • 举报
回复
用C语言的写法啊,
用C++实现简单,

64,636

社区成员

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

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