C++文件操作Word文档格式.docx
- 文档编号:17649918
- 上传时间:2022-12-07
- 格式:DOCX
- 页数:8
- 大小:16.29KB
C++文件操作Word文档格式.docx
《C++文件操作Word文档格式.docx》由会员分享,可在线阅读,更多相关《C++文件操作Word文档格式.docx(8页珍藏版)》请在冰豆网上搜索。
;
abort();
}
//下面请注意往文本文件写字符流的语句!
outfile<
thisisaprogram.\n"
thisisastring."
\nok!
outfile.close();
}
【例2】编程序将【例1】中被写入文本文件中的字符信息读出并显示。
fstreaminfile;
//或ifstreamoutfile;
infile.open("
in);
infile)
chars[80];
while(!
infile.eof())
{
/*用成员函数getline(两个形参)从文本文件读取!
*/
infile.getline(s,sizeof(s));
cout<
s<
endl;
infile.close();
【例3】编程使用get()函数和put()函数读、写文本文件。
特点是一次处理一个字符!
iostream.h>
fstream.h>
string.h>
fstreamoutfile,infile;
//或ofstreamoutfile;
ofstreaminfile;
file2.dat"
file2.datcan'
chars[]="
IloveC++programing."
for(inti=0;
i<
=(int)strlen(s);
i++)
outfile.put(s[i]);
//往文本文件中写一个字符
charch;
/*下面从文本文件中读取一个字符到字符变量ch中。
while(infile.get(ch))
cout.put(ch);
infile.close();
【例4】编程对一个二进制文件进行读/写操作。
structperson
charname[80];
//姓名
doubleheight;
//身高
unsignedshortage;
//年龄
}people[5]={"
Ma"
1.78,35,
Zhang"
1.68,24,
Hu"
1.90,40,
Lu"
1.89,50,
Lang"
2.00,18};
fstreamfile;
file.open("
file4.dat"
in|ios:
out|ios:
binary);
file)
file4.datcan'
5;
file.write((char*)&
people[i],sizeof(people[i]));
file.seekp(0,ios:
beg);
for(i=0;
file.read((char*)&
people[i].name<
'
\t'
people[i].height<
people[i].age<
file.close();
【例5】按下列要求编写一个程序。
①该程序用来将一些大学生和硕士生的有关信息输入后存放在一个文件中。
②关于大学生的信息有:
姓名,学号,平均成绩。
③关于硕士生的信息有:
除了大学生应有信息外,再增加一个导师姓名。
④将一个大学生和两个硕士生的信息写入程序中,也可从键盘输入。
iomanip.h>
classStudent
{private:
charName[80];
unsigneduID;
//学号
doublegrade;
//平均成绩
public:
Student(char*pN,unsignednum,doubleg)
strcpy(Name,pN);
uID=num;
grade=g;
virtualvoidPrint(ostream&
friendostream&
operator<
(ostream&
out,Student&
st);
};
voidStudent:
Print(ostream&
out)
out<
Name<
“”<
uID<
“”<
grade<
ostream&
operator<
st)
st.Print(out);
out<
returnout;
classMaster:
publicStudent
chardName[80];
//导师姓名
Master(char*pN,unsignednum,doubleg,char*pdN):
Student(pN,num,g)
strcpy(dName,pdN);
voidPrint(ostream&
voidMaster:
{
Student:
Print(out);
"
dName;
ofstreamout("
abc.txt"
);
Students1("
Wangping"
99001,96.5);
Masters2("
Maguang"
99056,84.8,"
Masters3("
Jiangfang"
99078,90.5,"
Huang"
s1;
s2;
s3;
【例6】分析下列程序的输出结果,学会随机读/写数据文件的方法。
fstreamfile("
file5.dat"
binary);
file5.datcan'
for(inti=1;
=20;
file.write((char*)&
i,sizeof(int));
longpos=file.tellp();
Currentbytenumber:
pos<
for(i=20;
=50;
i,sizeof(int));
file.seekp(pos);
file.read((char*)&
cout<
Thedatastoredis"
file.seekg(0,ios:
for(i=50;
=100;
file.seekg(pos);
file.seekp(116,ios:
cur);
file.tellp()<
【例7】分析下列程序的输出结果,熟悉成员函数seekp()的用法。
structstudent
charname[50];
longnumber;
doubletotalscord;
stu[5]={"
98001,89.5,"
Li"
98023,82.9,"
Gao"
98045,90.2,
98066,92.1,"
Yan"
98067,79.5};
students1;
fstreamfile1;
file1.open("
file6.dat"
file1)
for(inti=0;
file1.write((char*)&
stu[i],sizeof(student));
file1.seekp(sizeof(student)*4);
file1.read((char*)&
s1,sizeof(stu[i]));
s1.name<
s1.number<
s1.totalscord<
file1.seekp(sizeof(student)*1);
file1.close();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 文件 操作