您的位置:首页 > 其它

文件IO操作学习中出错的地方

2012-07-25 17:12 453 查看
问题1:

file1:

1 #include<iostream>
2 #include<fstream>
3
4 using namespace std;
5 class Sprite
6 {
7 public:
8 Sprite(){};
9 Sprite(string profession,string weapon):profession(profession),weapon(weapon){};
10 void showSprite();
11 private:
12 string profession;
13 string weapon;
14 static int count;
15 };
16 int Sprite::count=0;
17 void Sprite:: showSprite()
18 {
19 ++count;
20 cout<<"jingling:"<<count<<"zhiye:"<<profession<<"wuqi:"<<weapon<<endl;
21 }
22
23
24 int main()
25 {
26 Sprite sprites[3]={Sprite("fashi","mozhang"),Sprite("zhanshi","tulongbaoda"),Sprite("daoshi","yitianjian")};
27 ofstream file("file.data",ios::ate|ios::binary);
28 if (!file)
29 {
30 cout<<"can't open the file"<<endl;
31 return 1;
32 }
33 for (int i=0;i<3;i++)
34 {
35 file.write((char*)&sprites[i],sizeof(sprites[i]));
36 }
37 file.close();
38
39 return 0;
40
41 }

File2.cpp

1 #include<iostream>
2 #include<fstream>
3 using namespace std;
4 class Sprite
5 {
6 public:
7 Sprite(){};
8 Sprite(string profession,string weapon):profession(profession),weapon(weapon){};
9 void showSprite();
10 private:
11 string profession;
12 string weapon;
13 static int count;
14
15 };
16 int Sprite::count=0;
17 void Sprite::showSprite()
18 {
19 ++count;
20 cout<<"jingling"<<count<<"zhiye"<<profession<<"wuqi"<<weapon;
21 }
22 int main()
23 {
24 Sprite spirit[3];
25 ifstream rfile("file.data",ios::binary);
26 if (!rfile)
27 {
28 cout<<"can't open the file.data"<<endl;
29 abort();
30 }
31
32 for(int i=0;i<3;i++)
33 {
34 rfile.read((char*)&spirit[i],sizeof(spirit[i]));
35 spirit[i].showSprite();
36
37 }
38 rfile.close();
39 return 0;
40 }

执行完1和2后,执行结果是:

jingling1zhiyewuqi`˜Ý‚e+ð Pÿÿÿÿÿÿÿÿ!P „Ý‚e+àÝ‚e+ X P( P¸ Pˆ P!Pè P±í*** glibc detected *** m: free(): invalid pointer: 0x00000000005020d0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x2b6582c0a4fe]
/lib64/libc.so.6(__libc_free+0x6c)[0x2b6582c0bb7c]
/usr/lib64/libstdc++.so.6(_ZNSsD1Ev+0x3a)[0x2b65827d9e5a]
m(__gxx_personality_v0+0x494)[0x401094]
m(__gxx_personality_v0+0x41d)[0x40101d]
/lib64/libc.so.6(__libc_start_main+0xf4)[0x2b6582bbc164]
m(__gxx_personality_v0+0x49)[0x400c49]
======= Memory map: ========
00400000-00402000 r-xp 00000000 fd:01 197048 /home2/billing/wangpanpan/C++/w/m
00501000-00502000 rw-p 00001000 fd:01 197048 /home2/billing/wangpanpan/C++/w/m
00502000-00523000 rw-p 00502000 00:00 0 [heap]
2b6582622000-2b658263d000 r-xp 00000000 08:02 12961 /lib64/ld-2.4.so
2b658263d000-2b658263f000 rw-p 2b658263d000 00:00 0
2b6582658000-2b6582659000 rw-p 2b6582658000 00:00 0
2b658273c000-2b658273e000 rw-p 0001a000 08:02 12961 /lib64/ld-2.4.so
2b658273e000-2b6582821000 r-xp 00000000 08:02 17856 /usr/lib64/libstdc++.so.6.0.8
2b6582821000-2b6582921000 ---p 000e3000 08:02 17856 /usr/lib64/libstdc++.so.6.0.8
2b6582921000-2b6582927000 r--p 000e3000 08:02 17856 /usr/lib64/libstdc++.so.6.0.8
2b6582927000-2b658292a000 rw-p 000e9000 08:02 17856 /usr/lib64/libstdc++.so.6.0.8
2b658292a000-2b658293c000 rw-p 2b658292a000 00:00 0
2b658293c000-2b6582990000 r-xp 00000000 08:02 12976 /lib64/libm-2.4.so
2b6582990000-2b6582a8f000 ---p 00054000 08:02 12976 /lib64/libm-2.4.so
2b6582a8f000-2b6582a91000 rw-p 00053000 08:02 12976 /lib64/libm-2.4.so
2b6582a91000-2b6582a9e000 r-xp 00000000 08:02 17620 /lib64/libgcc_s.so.1
2b6582a9e000-2b6582b9d000 ---p 0000d000 08:02 17620 /lib64/libgcc_s.so.1
2b6582b9d000-2b6582b9e000 rw-p 0000c000 08:02 17620 /lib64/libgcc_s.so.1
2b6582b9e000-2b6582b9f000 rw-p 2b6582b9e000 00:00 0
2b6582b9f000-2b6582cd5000 r-xp 00000000 08:02 12968 /lib64/libc-2.4.so
2b6582cd5000-2b6582dd5000 ---p 00136000 08:02 12968 /lib64/libc-2.4.so
2b6582dd5000-2b6582dd8000 r--p 00136000 08:02 12968 /lib64/libc-2.4.so
2b6582dd8000-2b6582dda000 rw-p 00139000 08:02 12968 /lib64/libc-2.4.so
2b6582dda000-2b6582de0000 rw-p 2b6582dda000 00:00 0
2b6582e00000-2b6582e21000 rw-p 2b6582e00000 00:00 0
2b6582e21000-2b6582f00000 ---p 2b6582e21000 00:00 0
7fff28471000-7fff28488000 rw-p 7fff28471000 00:00 0 [stack]
ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0 [vdso]
Abort

而且File.data文件的内容是:

X P^@^@^@^@^@( P^@^@^@^@^@¸ P^@^@^@^@^@<88> P^@^@^@^@^@^X!P^@^@^@^@^@è P^@^@^@^@^@

问题2:

我把class类放到了Sprite.h中,然后file1变成了:

#include<iostream>

#include<fstream>

#include "Sprite.h"

using namespace std;

int Sprite::count=0;
void Sprite:: showSprite()
{
++count;
cout<<"jingling:"<<count<<"zhiye:"<<profession<<"wuqi:"<<weapon<<endl;
}

int main()

{

Sprite sprites[3]={Sprite("fashi","mozhang"),Sprite("zhanshi","tulongbaoda"),Sprite("daoshi","yitianjian")};

ofstream file("file.data",ios::ate|ios::binary);

if (!file)

{

cout<<"can't open the file"<<endl;

return 1;

}

for (int i=0;i<3;i++)

{

file.write((char*)&sprites[i],sizeof(sprites[i]));

}

file.close();

return 0;

}

Spirit.h文件的内容是:

class Sprite

{

public:

Sprite(){};

Sprite(string profession,string weapon):profession(profession),weapon(weapon){};

void showSprite();

private:

string profession;

string weapon;

static int count;

};

但是编译的结果怎么报这样的错误呢?

Sprite.h:6: error: expected `)' before ‘profession’
Sprite.h:9: error: ‘string’ does not name a type
Sprite.h:10: error: ‘string’ does not name a type
file4.cpp: In member function ‘void Sprite::showSprite()’:
file4.cpp:9: error: ‘profession’ was not declared in this scope
file4.cpp:9: error: ‘weapon’ was not declared in this scope
file4.cpp: In function ‘int main()’:
file4.cpp:13: error: no matching function for call to ‘Sprite::Sprite(const char [6], const char [8])’
Sprite.h:5: note: candidates are: Sprite::Sprite()
Sprite.h:3: note: Sprite::Sprite(const Sprite&)
file4.cpp:13: error: no matching function for call to ‘Sprite::Sprite(const char [8], const char [12])’
Sprite.h:5: note: candidates are: Sprite::Sprite()
Sprite.h:3: note: Sprite::Sprite(const Sprite&)
file4.cpp:13: error: no matching function for call to ‘Sprite::Sprite(const char [7], const char [11])’
Sprite.h:5: note: candidates are: Sprite::Sprite()
Sprite.h:3: note: Sprite::Sprite(const Sprite&)

回答问题1:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Sprite
{
public:
Sprite(){};
Sprite(char *p, char *w){
strcpy(profession, p);
strcpy(weapon, w);
};

void showSprite();

char profession[10];
char weapon[15];
static int count;
};
int Sprite::count=0;
void Sprite:: showSprite()
{
++count;
cout<<"jingling:"<<count<<"zhiye:"<<profession<<"wuqi:"<<weapon<<endl;
}

int main()
{
Sprite sprites[3]={Sprite("fashi","mozhang"),Sprite("zhanshi","tulongbaoda"),Sprite("daoshi","yitianjian")};
ofstream file("file.data",ios::ate|ios::binary);
if (!file)
{
cout<<"can't open the file"<<endl;
return 1;
}
for (int i=0;i<3;i++)
{
file.write(sprites[i].profession, strlen(sprites[i].profession));
file.write("", 1);
file.write(sprites[i].weapon, strlen(sprites[i].weapon));
file.write("", 1);
}
file.close();
string a="saasdaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadsadasdasdada";
cout<<sizeof(a)<<endl;
return 0;

}

原因是:string类型不是基本类型,定义为string型的,它的字符串存在堆中,而变量只是存储了指向堆得指针和长度,以及两个指针(读取得地方),所以问题1中是乱码,是读不到指定的内容的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: