1.ofstream中write函数怎么用的
你是用二进制写的,怎么读的,用编辑器看的? 那乱码就对了
要用代码读才不乱
void main(void)
{
ofstream f1("f:\\shif1.dat",ios::out|ios::binary);
if(!f1){
cerr<<"f:\\shif1.dat file not open!"<<endl;
exit(1);
}
int a[10]={48,62,25,73,66,80,78,54,36,47};
for(int i=0;i<10;i++)
f1.write((char*) &a[i],sizeof(int));
f1.close();
ifstream f2("f:\\shif1.dat",ios::in|ios::binary);
int b[10];
if(!f2){
cerr<<"f:\\shif1.dat file not open!"<<endl;
exit(1);
}
for( i=0;i<10;i++)
{
f2.read((char*) &b[i],sizeof(int));
cout << b[i] <<endl;
}
f2.close();
}
2.C语言中的Write函数
哇!两百分,好慷慨!
在这个情况中,你可以把handle看作一个结构体的指针,而这种struct不会在头文件中定义,我写段简单的例子:
头文件:
int write(int handle);
int open(char* filename);
实现文件:
struct file{
int size;
void* data;
};
int open(char* filename){
file* fp = 0;
/*
打开文件后,
把文件内容读取到fp->data,
*/
…………
return (int)fp;/*返回时候转换为int类型*/
}
int write(int handle){
file* fp = (file*)handle;
/*然后对fp进行相关操作*/
..
}
这样就把file类型的内部细节给隐藏以来了,
你在使用的时候只看头文件,
那些write,和open之内的函数,
仅仅需要知道handle代表什么,
而不需要知道handle所指向的结构的内部细节!
当然handle不光止可以代表结构体指针,一个抽象的概念而已。
3.MFC中的Write函数的参数及用法
不知道说的是哪个类的Write用法 我想应该是 CFILE类的Write的用法
virtual void Write(
const void* lpBuf,
UINT nCount );
这个MSDN中的Write的形式。
第一个参数表示存储要写的东西的字符串或者字符数组。
第二个参数表示你要从这个字符串或者字符数组中写多少个字符到文件中。
举个小例子
CFile file(1.TXT, CFile::modeWrite );
CString str="abcdefg";
file.Write(str, 5);
第一行为定义一个CFile类的对象 其中第一个参数是我们要打开一个1.TXT文件
第二个参数是我们要用写文件的方式使 用这个文件
第二行为定义一个字符串,它的内容为abcdefg.
第三行就是使用这个函数了,讲这个字符串的内容写到文件中,然后写5个字符
我们打开1.TXT文件就可以看到5个字符。
他们是abcde
祝你好运 如果有什么不明白 还可以问我
我的百度号为pengxuewanp
4.如何用c++里的read和write函数向一个txt文件中写数据和读数据
#include<iostream>
#include<fstream>
using namespace std;
struct student
{
char name;
int num;
int age;
char sex;
};
int main()
{student stud[3]={"li",1001,14,'f',"Fun",1002,19,'m',"wang",1003,21,'f'};
ofstream outfile("d:\\stud.txt",ios::binary);
if(!outfile)
{
ceer<<"open error!"<<endl;
abort();
}
for(int i=0;i<3;i++)
outfile.write((char *)&stud[i],sizeof(stud[i]));
outfile.close();
return 0;
}
5.C++中ofstream的write函数的用法
write()函数 读写二进制数据块,使用成员函数read()和write()成员函数,它们原型如下:read(unsigned char *buf,int num); write(const unsigned char *buf,int num); read()从文件中读取 num 个字符到 buf 指向的缓存中,如果在还未读入 num 个字符时就到了文件尾,可以用成员函数 int gcount();来取得实际读取的字符数;而 write() 从buf 指向的缓存写 num 个字符到文件中,值得注意的是缓存的类型是 unsigned char *,有时可能需要类型转换。
例: unsigned char str1[]="I Love You"; int n[5]; ifstream in("xxx.xxx"); ofstream out("yyy.yyy"); out.write(str1,strlen(str1));//把字符串str1全部写到yyy.yyy中 in.read((unsigned char*)n,sizeof(n));//从xxx.xxx中读取指定个整数,注意类型转换 in.close();out.close();。
6.C语言中的Write函数
write()写文件函数原形:int write(int handle,char *buf,unsigned len)功能:将缓冲区的数据写入与handle相联的文件或设备中,handle是从creat、open、dup或dup2调用中得到的文件句柄。
对于磁盘或磁盘文件,写操作从当前文件指针处开始,对于用O_APPEND选项打开的文件,写数据之前,文件指针指向EOF;对于设备,字节被直接传送到设备中。扩展资料:用法头文件:
一种是:ssize_t write(int fd, const void *buf, size_t nbyte);fd:文件描述符;buf:指定的缓冲区,即指针,指向一段内存单元;nbyte:要写入文件指定的字节数;返回值:写入文档的字节数(成功);-1(出错)write函数把buf中nbyte写入文件描述符handle所指的文档,成功时返回写的字节数,错误时返回-1.另一种是: write(const char* str,int n)str是字符指针或字符数组,用来存放一个字符串。n是int型数,它用来表示输出显示字符串中字符的个数。
write("string",strlen("string");表示输出字符串常量参考资料来源:百度百科-write函数。
7.如何用c++里的read和write函数向一个txt文件中写数据和读数据
#include<iostream>
#include<fstream>
using namespace std;
struct student
{
char name;
int num;
int age;
char sex;
};
int main()
{student stud[3]={"li",1001,14,'f',"Fun",1002,19,'m',"wang",1003,21,'f'};
ofstream outfile("d:\\stud.txt",ios::binary);
if(!outfile)
{
ceer<<"open error!"<<endl;
abort();
}
for(int i=0;i<3;i++)
outfile.write((char *)&stud[i],sizeof(stud[i]));
outfile.close();
return 0;
}
转载请注明出处育才学习网 » write函数怎么写数字