阅读背景:

txt c++写入_CSDN131137的博客

来源:互联网 

 

 


#include<stdio.h>
#include<string>
#include<afxwin.h>
#include<fstream>
#include<iostream>
using namespace std;
 
int main()
{
	int k = 12;
	float l[3] = { 1.23, 3.28, 2.6 };
	char *ch = "kjgd好几个宽度激光kgaskga";
	string ss("gk好几个j");
	string s = "gg航空国际化kljh";//strlen无法使用
	CString cs = "kg汉口开个房kk";
	CString css("hjf好几个科技hj");
	CString kcs;
	kcs.Format("%d", k);

	//FILE *pf = fopen("1.txt", "wb");
	FILE *pf;
	fopen_s(&pf, "1.txt", "w");
	fwrite(ch, 1, strlen(ch), pf);
	//fwrite(ch, 1, sizeof(ch), pf);// sizeof(ch)=4,只能写入kjgd4个字母
	fwrite(&k, 1, sizeof(k), pf);
	//fwrite(&l, 1, sizeof(l), pf);//很有问题
	fwrite(&l[0], sizeof(float), 1,pf);
	fwrite(l, sizeof(float),3, pf);
	fwrite(cs, 1, strlen(cs), pf);
	fwrite(css, 1, strlen(css), pf);
	fwrite(kcs, 1, strlen(kcs), pf);
	fprintf_s(pf, "\n%d", k);//int类型写入文件正确方法
	fprintf_s(pf, "%f %f %f\n", l[0], l[1], l[2]);
	//fprintf_s(pf, "%s %s %s\n", l[0], l[1], l[2]);//错误的写入,文件未写入任何数据
	//fprintf_s(pf, "%s", &l[0]);
	fwrite(&k, sizeof(k),1, pf);

	/*CString is;
	is = _itoa_s(k, is, 10);
	fwrite(is, 1, strlen(is), pf);*/

	//fwrite(s, 1, s.length(), pf);
	fwrite(s.c_str(), 1, s.length(), pf);//string类型写入文件正确方法
	/*const char *c_str();
	c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同.
		这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
		注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
	比如:最好不要这样 :
	char* c;
	string s = "1234";
	c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将一个const char *赋与一个char *。

	应该这样用:
		char c[20];
	string s = "1234";
	strcpy(c, s.c_str());
	这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作*/
	//string fs;
	//sprintf(fs, "%f", l[0]);//字符串在内存中是一个常量字符串数组
	fclose(pf);

	HANDLE hFile;
	hFile = CreateFile("2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);//创建文件
	DWORD dwWrites;//接收实际写入的字节数
	WriteFile(hFile, ch, strlen(ch), &dwWrites, NULL);
	WriteFile(hFile, &k, sizeof(k), &dwWrites, NULL);
	WriteFile(hFile, &l, sizeof(l), &dwWrites, NULL);
	CloseHandle(hFile);

	CFile file;
	file.Open("3.txt", CFile::modeCreate | CFile::modeWrite);
	file.Write(ch, strlen(ch));
	file.Write(&k, sizeof(k));
	file.Write(&l, sizeof(l));
	file.Close();

	//写入txt未乱码,可读
	ofstream m_outC;
	m_outC.open("4.txt", std::ios::out);
	m_outC << ch << endl;
	m_outC << k << endl;
	//m_outC << l << endl;//写入l数组地址
	m_outC << l[0] << endl;
	m_outC << l[1] << endl;
	m_outC << l[2] << endl;
	m_outC << ch << k << l[0] << l;
	m_outC.close();

	//序列化
	CFile fileA;
	fileA.Open("5.txt", CFile::modeCreate | CFile::modeWrite);
	CArchive ar(&fileA, CArchive::store);
	ar << *ch;//需要一个一个的系列化
	ar << *ch + 1;//写入l字母,相当于将k的ASCII码+1
	ar << *(ch + 1);//写入的j字母
	ar << k;
	ar << '\n' << l[0] << '\n' << l[1] << '\n' << l[2];//写入乱码
	ar << cs;
}

/*sizeof操作符的结果类型为size_t(The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types).
This keyword returns a value of type size_t.)(它在头文件用typedfe定义为unsigned int类型),计算的是分配空间的实际字节数。
strlen结果类型也为size_t(size_t strlen(const char *string)),但strlen是计算的空间中字符的个数(不包括‘

 

 


#include<stdio.h>
#include<string>
#include<afxwin.h>
#include<fstream>
#include<iostream>
using namespace std;
 
int main()
{
	int k = 12;
	float l[3] = { 1.23, 3.28, 2.6 };
	char *ch = "kjgd好几个宽度激光kgaskga";
	string ss("gk好几个j");
	string s = "gg航空国际化kljh";//strlen无法使用
	CString cs = "kg汉口开个房kk";
	CString css("hjf好几个科技hj");
	CString kcs;
	kcs.Format("%d", k);

	//FILE *pf = fopen("1.txt", "wb");
	FILE *pf;
	fopen_s(&pf, "1.txt", "w");
	fwrite(ch, 1, strlen(ch), pf);
	//fwrite(ch, 1, sizeof(ch), pf);// sizeof(ch)=4,只能写入kjgd4个字母
	fwrite(&k, 1, sizeof(k), pf);
	//fwrite(&l, 1, sizeof(l), pf);//很有问题
	fwrite(&l[0], sizeof(float), 1,pf);
	fwrite(l, sizeof(float),3, pf);
	fwrite(cs, 1, strlen(cs), pf);
	fwrite(css, 1, strlen(css), pf);
	fwrite(kcs, 1, strlen(kcs), pf);
	fprintf_s(pf, "\n%d", k);//int类型写入文件正确方法
	fprintf_s(pf, "%f %f %f\n", l[0], l[1], l[2]);
	//fprintf_s(pf, "%s %s %s\n", l[0], l[1], l[2]);//错误的写入,文件未写入任何数据
	//fprintf_s(pf, "%s", &l[0]);
	fwrite(&k, sizeof(k),1, pf);

	/*CString is;
	is = _itoa_s(k, is, 10);
	fwrite(is, 1, strlen(is), pf);*/

	//fwrite(s, 1, s.length(), pf);
	fwrite(s.c_str(), 1, s.length(), pf);//string类型写入文件正确方法
	/*const char *c_str();
	c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同.
		这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
		注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
	比如:最好不要这样 :
	char* c;
	string s = "1234";
	c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将一个const char *赋与一个char *。

	应该这样用:
		char c[20];
	string s = "1234";
	strcpy(c, s.c_str());
	这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作*/
	//string fs;
	//sprintf(fs, "%f", l[0]);//字符串在内存中是一个常量字符串数组
	fclose(pf);

	HANDLE hFile;
	hFile = CreateFile("2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);//创建文件
	DWORD dwWrites;//接收实际写入的字节数
	WriteFile(hFile, ch, strlen(ch), &dwWrites, NULL);
	WriteFile(hFile, &k, sizeof(k), &dwWrites, NULL);
	WriteFile(hFile, &l, sizeof(l), &dwWrites, NULL);
	CloseHandle(hFile);

	CFile file;
	file.Open("3.txt", CFile::modeCreate | CFile::modeWrite);
	file.Write(ch, strlen(ch));
	file.Write(&k, sizeof(k));
	file.Write(&l, sizeof(l));
	file.Close();

	//写入txt未乱码,可读
	ofstream m_outC;
	m_outC.open("4.txt", std::ios::out);
	m_outC << ch << endl;
	m_outC << k << endl;
	//m_outC << l << endl;//写入l数组地址
	m_outC << l[0] << endl;
	m_outC << l[1] << endl;
	m_outC << l[2] << endl;
	m_outC << ch << k << l[0] << l;
	m_outC.close();

	//序列化
	CFile fileA;
	fileA.Open("5.txt", CFile::modeCreate | CFile::modeWrite);
	CArchive ar(&fileA, CArchive::store);
	ar << *ch;//需要一个一个的系列化
	ar << *ch + 1;//写入l字母,相当于将k的ASCII码+1
	ar << *(ch + 1);//写入的j字母
	ar << k;
	ar << '\n' << l[0] << '\n' << l[1] << '\n' << l[2];//写入乱码
	ar << cs;
}

/*sizeof操作符的结果类型为size_t(The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types).
This keyword returns a value of type size_t.)(它在头文件用typedfe定义为unsigned int类型),计算的是分配空间的实际字节数。
strlen结果类型也为size_t(size_t strlen(const char *string)),但strlen是计算的空间中字符的个数(不包括‘\0’)。

sizeof是运算符,可以以类型、函数、做参数 。strlen是函数,只能以char*(字符串)做参数。而且,要想得到的结果正确必须包含 ‘\0’(通过strlen的实现得知)。

sizeof是在编译的时候就将结果计算出来了是类型所占空间的字节数,所以以数组名做参数时计算的是整个数组的大小。
而strlen是在运行的时候才开始计算结果,这是计算的结果不再是类型所占内存的大小,数组名就退化为指针了。

另外,sizeof不能计算动态分配空间的大小如:
char *s = (char *)malloc(20);
strcpy(s, str);
结果sizeof(s)=4
最后的sizeof计算的是指针(sizeof(char *)) 的大小,为4。当适用了于一个结构类型时或变量,
sizeof 返回实际的大小, 当适用一静态地空间数组, sizeof 归还全部数组的尺寸。 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸

char* s = "0123456789";
sizeof(s);     //结果 4    ===》s是指向字符串常量的字符指针
sizeof(*s);    //结果 1    ===》*s是第一个字符
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
strlen(*s);     //结果 10   ===》错误


char s[] = "0123456789";
sizeof(s);     //结果 11   ===》s是数组,计算到\0位置,因此是10+1
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
sizeof(*s);    //结果 1    ===》*s是第一个字符

char s[100] = "0123456789";
sizeof(s);     //结果是100 ===》s表示在内存中的大小 100×1
strlen(s);     //结果是10  ===》strlen是个函数内部实现是用一个循环计算到\0为止之前

int s[100] = "0123456789";
sizeof(s);     //结果 400  ===》s表示再内存中的大小 100×4
strlen(s);     //错误      ===》strlen的参数只能是char* 且必须是以‘\0‘结尾的

char q[]="abc";
char p[]="a\n";
sizeof(q),sizeof(p),strlen(q),strlen(p);\\结果是 4 3 3 2

char p[] = {'a','b','c','d','e','f','g','h'};
char q[] = {'a','b','c','d,'\0','e','f','g'};
sizeof(p);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(p);     //为一个随机值,结果与编译器有关,不同编译器结果一般不同
sizeof(q);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(q);     //结果为4 ===》存在'\0',遇到'\0'计算停止。
*/

 


’)。 sizeof是运算符,可以以类型、函数、做参数 。strlen是函数,只能以char*(字符串)做参数。而且,要想得到的结果正确必须包含 ‘

 

 


#include<stdio.h>
#include<string>
#include<afxwin.h>
#include<fstream>
#include<iostream>
using namespace std;
 
int main()
{
	int k = 12;
	float l[3] = { 1.23, 3.28, 2.6 };
	char *ch = "kjgd好几个宽度激光kgaskga";
	string ss("gk好几个j");
	string s = "gg航空国际化kljh";//strlen无法使用
	CString cs = "kg汉口开个房kk";
	CString css("hjf好几个科技hj");
	CString kcs;
	kcs.Format("%d", k);

	//FILE *pf = fopen("1.txt", "wb");
	FILE *pf;
	fopen_s(&pf, "1.txt", "w");
	fwrite(ch, 1, strlen(ch), pf);
	//fwrite(ch, 1, sizeof(ch), pf);// sizeof(ch)=4,只能写入kjgd4个字母
	fwrite(&k, 1, sizeof(k), pf);
	//fwrite(&l, 1, sizeof(l), pf);//很有问题
	fwrite(&l[0], sizeof(float), 1,pf);
	fwrite(l, sizeof(float),3, pf);
	fwrite(cs, 1, strlen(cs), pf);
	fwrite(css, 1, strlen(css), pf);
	fwrite(kcs, 1, strlen(kcs), pf);
	fprintf_s(pf, "\n%d", k);//int类型写入文件正确方法
	fprintf_s(pf, "%f %f %f\n", l[0], l[1], l[2]);
	//fprintf_s(pf, "%s %s %s\n", l[0], l[1], l[2]);//错误的写入,文件未写入任何数据
	//fprintf_s(pf, "%s", &l[0]);
	fwrite(&k, sizeof(k),1, pf);

	/*CString is;
	is = _itoa_s(k, is, 10);
	fwrite(is, 1, strlen(is), pf);*/

	//fwrite(s, 1, s.length(), pf);
	fwrite(s.c_str(), 1, s.length(), pf);//string类型写入文件正确方法
	/*const char *c_str();
	c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同.
		这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
		注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
	比如:最好不要这样 :
	char* c;
	string s = "1234";
	c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将一个const char *赋与一个char *。

	应该这样用:
		char c[20];
	string s = "1234";
	strcpy(c, s.c_str());
	这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作*/
	//string fs;
	//sprintf(fs, "%f", l[0]);//字符串在内存中是一个常量字符串数组
	fclose(pf);

	HANDLE hFile;
	hFile = CreateFile("2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);//创建文件
	DWORD dwWrites;//接收实际写入的字节数
	WriteFile(hFile, ch, strlen(ch), &dwWrites, NULL);
	WriteFile(hFile, &k, sizeof(k), &dwWrites, NULL);
	WriteFile(hFile, &l, sizeof(l), &dwWrites, NULL);
	CloseHandle(hFile);

	CFile file;
	file.Open("3.txt", CFile::modeCreate | CFile::modeWrite);
	file.Write(ch, strlen(ch));
	file.Write(&k, sizeof(k));
	file.Write(&l, sizeof(l));
	file.Close();

	//写入txt未乱码,可读
	ofstream m_outC;
	m_outC.open("4.txt", std::ios::out);
	m_outC << ch << endl;
	m_outC << k << endl;
	//m_outC << l << endl;//写入l数组地址
	m_outC << l[0] << endl;
	m_outC << l[1] << endl;
	m_outC << l[2] << endl;
	m_outC << ch << k << l[0] << l;
	m_outC.close();

	//序列化
	CFile fileA;
	fileA.Open("5.txt", CFile::modeCreate | CFile::modeWrite);
	CArchive ar(&fileA, CArchive::store);
	ar << *ch;//需要一个一个的系列化
	ar << *ch + 1;//写入l字母,相当于将k的ASCII码+1
	ar << *(ch + 1);//写入的j字母
	ar << k;
	ar << '\n' << l[0] << '\n' << l[1] << '\n' << l[2];//写入乱码
	ar << cs;
}

/*sizeof操作符的结果类型为size_t(The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types).
This keyword returns a value of type size_t.)(它在头文件用typedfe定义为unsigned int类型),计算的是分配空间的实际字节数。
strlen结果类型也为size_t(size_t strlen(const char *string)),但strlen是计算的空间中字符的个数(不包括‘\0’)。

sizeof是运算符,可以以类型、函数、做参数 。strlen是函数,只能以char*(字符串)做参数。而且,要想得到的结果正确必须包含 ‘\0’(通过strlen的实现得知)。

sizeof是在编译的时候就将结果计算出来了是类型所占空间的字节数,所以以数组名做参数时计算的是整个数组的大小。
而strlen是在运行的时候才开始计算结果,这是计算的结果不再是类型所占内存的大小,数组名就退化为指针了。

另外,sizeof不能计算动态分配空间的大小如:
char *s = (char *)malloc(20);
strcpy(s, str);
结果sizeof(s)=4
最后的sizeof计算的是指针(sizeof(char *)) 的大小,为4。当适用了于一个结构类型时或变量,
sizeof 返回实际的大小, 当适用一静态地空间数组, sizeof 归还全部数组的尺寸。 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸

char* s = "0123456789";
sizeof(s);     //结果 4    ===》s是指向字符串常量的字符指针
sizeof(*s);    //结果 1    ===》*s是第一个字符
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
strlen(*s);     //结果 10   ===》错误


char s[] = "0123456789";
sizeof(s);     //结果 11   ===》s是数组,计算到\0位置,因此是10+1
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
sizeof(*s);    //结果 1    ===》*s是第一个字符

char s[100] = "0123456789";
sizeof(s);     //结果是100 ===》s表示在内存中的大小 100×1
strlen(s);     //结果是10  ===》strlen是个函数内部实现是用一个循环计算到\0为止之前

int s[100] = "0123456789";
sizeof(s);     //结果 400  ===》s表示再内存中的大小 100×4
strlen(s);     //错误      ===》strlen的参数只能是char* 且必须是以‘\0‘结尾的

char q[]="abc";
char p[]="a\n";
sizeof(q),sizeof(p),strlen(q),strlen(p);\\结果是 4 3 3 2

char p[] = {'a','b','c','d','e','f','g','h'};
char q[] = {'a','b','c','d,'\0','e','f','g'};
sizeof(p);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(p);     //为一个随机值,结果与编译器有关,不同编译器结果一般不同
sizeof(q);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(q);     //结果为4 ===》存在'\0',遇到'\0'计算停止。
*/

 


’(通过strlen的实现得知)。 sizeof是在编译的时候就将结果计算出来了是类型所占空间的字节数,所以以数组名做参数时计算的是整个数组的大小。 而strlen是在运行的时候才开始计算结果,这是计算的结果不再是类型所占内存的大小,数组名就退化为指针了。 另外,sizeof不能计算动态分配空间的大小如: char *s = (char *)malloc(20); strcpy(s, str); 结果sizeof(s)=4 最后的sizeof计算的是指针(sizeof(char *)) 的大小,为4。当适用了于一个结构类型时或变量, sizeof 返回实际的大小, 当适用一静态地空间数组, sizeof 归还全部数组的尺寸。 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸 char* s = "0123456789"; sizeof(s); //结果 4 ===》s是指向字符串常量的字符指针 sizeof(*s); //结果 1 ===》*s是第一个字符 strlen(s); //结果 10 ===》有10个字符,strlen是个函数内部实现是用一个循环计算到

 

 


#include<stdio.h>
#include<string>
#include<afxwin.h>
#include<fstream>
#include<iostream>
using namespace std;
 
int main()
{
	int k = 12;
	float l[3] = { 1.23, 3.28, 2.6 };
	char *ch = "kjgd好几个宽度激光kgaskga";
	string ss("gk好几个j");
	string s = "gg航空国际化kljh";//strlen无法使用
	CString cs = "kg汉口开个房kk";
	CString css("hjf好几个科技hj");
	CString kcs;
	kcs.Format("%d", k);

	//FILE *pf = fopen("1.txt", "wb");
	FILE *pf;
	fopen_s(&pf, "1.txt", "w");
	fwrite(ch, 1, strlen(ch), pf);
	//fwrite(ch, 1, sizeof(ch), pf);// sizeof(ch)=4,只能写入kjgd4个字母
	fwrite(&k, 1, sizeof(k), pf);
	//fwrite(&l, 1, sizeof(l), pf);//很有问题
	fwrite(&l[0], sizeof(float), 1,pf);
	fwrite(l, sizeof(float),3, pf);
	fwrite(cs, 1, strlen(cs), pf);
	fwrite(css, 1, strlen(css), pf);
	fwrite(kcs, 1, strlen(kcs), pf);
	fprintf_s(pf, "\n%d", k);//int类型写入文件正确方法
	fprintf_s(pf, "%f %f %f\n", l[0], l[1], l[2]);
	//fprintf_s(pf, "%s %s %s\n", l[0], l[1], l[2]);//错误的写入,文件未写入任何数据
	//fprintf_s(pf, "%s", &l[0]);
	fwrite(&k, sizeof(k),1, pf);

	/*CString is;
	is = _itoa_s(k, is, 10);
	fwrite(is, 1, strlen(is), pf);*/

	//fwrite(s, 1, s.length(), pf);
	fwrite(s.c_str(), 1, s.length(), pf);//string类型写入文件正确方法
	/*const char *c_str();
	c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同.
		这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
		注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
	比如:最好不要这样 :
	char* c;
	string s = "1234";
	c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将一个const char *赋与一个char *。

	应该这样用:
		char c[20];
	string s = "1234";
	strcpy(c, s.c_str());
	这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作*/
	//string fs;
	//sprintf(fs, "%f", l[0]);//字符串在内存中是一个常量字符串数组
	fclose(pf);

	HANDLE hFile;
	hFile = CreateFile("2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);//创建文件
	DWORD dwWrites;//接收实际写入的字节数
	WriteFile(hFile, ch, strlen(ch), &dwWrites, NULL);
	WriteFile(hFile, &k, sizeof(k), &dwWrites, NULL);
	WriteFile(hFile, &l, sizeof(l), &dwWrites, NULL);
	CloseHandle(hFile);

	CFile file;
	file.Open("3.txt", CFile::modeCreate | CFile::modeWrite);
	file.Write(ch, strlen(ch));
	file.Write(&k, sizeof(k));
	file.Write(&l, sizeof(l));
	file.Close();

	//写入txt未乱码,可读
	ofstream m_outC;
	m_outC.open("4.txt", std::ios::out);
	m_outC << ch << endl;
	m_outC << k << endl;
	//m_outC << l << endl;//写入l数组地址
	m_outC << l[0] << endl;
	m_outC << l[1] << endl;
	m_outC << l[2] << endl;
	m_outC << ch << k << l[0] << l;
	m_outC.close();

	//序列化
	CFile fileA;
	fileA.Open("5.txt", CFile::modeCreate | CFile::modeWrite);
	CArchive ar(&fileA, CArchive::store);
	ar << *ch;//需要一个一个的系列化
	ar << *ch + 1;//写入l字母,相当于将k的ASCII码+1
	ar << *(ch + 1);//写入的j字母
	ar << k;
	ar << '\n' << l[0] << '\n' << l[1] << '\n' << l[2];//写入乱码
	ar << cs;
}

/*sizeof操作符的结果类型为size_t(The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types).
This keyword returns a value of type size_t.)(它在头文件用typedfe定义为unsigned int类型),计算的是分配空间的实际字节数。
strlen结果类型也为size_t(size_t strlen(const char *string)),但strlen是计算的空间中字符的个数(不包括‘\0’)。

sizeof是运算符,可以以类型、函数、做参数 。strlen是函数,只能以char*(字符串)做参数。而且,要想得到的结果正确必须包含 ‘\0’(通过strlen的实现得知)。

sizeof是在编译的时候就将结果计算出来了是类型所占空间的字节数,所以以数组名做参数时计算的是整个数组的大小。
而strlen是在运行的时候才开始计算结果,这是计算的结果不再是类型所占内存的大小,数组名就退化为指针了。

另外,sizeof不能计算动态分配空间的大小如:
char *s = (char *)malloc(20);
strcpy(s, str);
结果sizeof(s)=4
最后的sizeof计算的是指针(sizeof(char *)) 的大小,为4。当适用了于一个结构类型时或变量,
sizeof 返回实际的大小, 当适用一静态地空间数组, sizeof 归还全部数组的尺寸。 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸

char* s = "0123456789";
sizeof(s);     //结果 4    ===》s是指向字符串常量的字符指针
sizeof(*s);    //结果 1    ===》*s是第一个字符
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
strlen(*s);     //结果 10   ===》错误


char s[] = "0123456789";
sizeof(s);     //结果 11   ===》s是数组,计算到\0位置,因此是10+1
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
sizeof(*s);    //结果 1    ===》*s是第一个字符

char s[100] = "0123456789";
sizeof(s);     //结果是100 ===》s表示在内存中的大小 100×1
strlen(s);     //结果是10  ===》strlen是个函数内部实现是用一个循环计算到\0为止之前

int s[100] = "0123456789";
sizeof(s);     //结果 400  ===》s表示再内存中的大小 100×4
strlen(s);     //错误      ===》strlen的参数只能是char* 且必须是以‘\0‘结尾的

char q[]="abc";
char p[]="a\n";
sizeof(q),sizeof(p),strlen(q),strlen(p);\\结果是 4 3 3 2

char p[] = {'a','b','c','d','e','f','g','h'};
char q[] = {'a','b','c','d,'\0','e','f','g'};
sizeof(p);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(p);     //为一个随机值,结果与编译器有关,不同编译器结果一般不同
sizeof(q);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(q);     //结果为4 ===》存在'\0',遇到'\0'计算停止。
*/

 


为止之前 strlen(*s); //结果 10 ===》错误 char s[] = "0123456789"; sizeof(s); //结果 11 ===》s是数组,计算到

 

 


#include<stdio.h>
#include<string>
#include<afxwin.h>
#include<fstream>
#include<iostream>
using namespace std;
 
int main()
{
	int k = 12;
	float l[3] = { 1.23, 3.28, 2.6 };
	char *ch = "kjgd好几个宽度激光kgaskga";
	string ss("gk好几个j");
	string s = "gg航空国际化kljh";//strlen无法使用
	CString cs = "kg汉口开个房kk";
	CString css("hjf好几个科技hj");
	CString kcs;
	kcs.Format("%d", k);

	//FILE *pf = fopen("1.txt", "wb");
	FILE *pf;
	fopen_s(&pf, "1.txt", "w");
	fwrite(ch, 1, strlen(ch), pf);
	//fwrite(ch, 1, sizeof(ch), pf);// sizeof(ch)=4,只能写入kjgd4个字母
	fwrite(&k, 1, sizeof(k), pf);
	//fwrite(&l, 1, sizeof(l), pf);//很有问题
	fwrite(&l[0], sizeof(float), 1,pf);
	fwrite(l, sizeof(float),3, pf);
	fwrite(cs, 1, strlen(cs), pf);
	fwrite(css, 1, strlen(css), pf);
	fwrite(kcs, 1, strlen(kcs), pf);
	fprintf_s(pf, "\n%d", k);//int类型写入文件正确方法
	fprintf_s(pf, "%f %f %f\n", l[0], l[1], l[2]);
	//fprintf_s(pf, "%s %s %s\n", l[0], l[1], l[2]);//错误的写入,文件未写入任何数据
	//fprintf_s(pf, "%s", &l[0]);
	fwrite(&k, sizeof(k),1, pf);

	/*CString is;
	is = _itoa_s(k, is, 10);
	fwrite(is, 1, strlen(is), pf);*/

	//fwrite(s, 1, s.length(), pf);
	fwrite(s.c_str(), 1, s.length(), pf);//string类型写入文件正确方法
	/*const char *c_str();
	c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同.
		这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
		注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
	比如:最好不要这样 :
	char* c;
	string s = "1234";
	c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将一个const char *赋与一个char *。

	应该这样用:
		char c[20];
	string s = "1234";
	strcpy(c, s.c_str());
	这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作*/
	//string fs;
	//sprintf(fs, "%f", l[0]);//字符串在内存中是一个常量字符串数组
	fclose(pf);

	HANDLE hFile;
	hFile = CreateFile("2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);//创建文件
	DWORD dwWrites;//接收实际写入的字节数
	WriteFile(hFile, ch, strlen(ch), &dwWrites, NULL);
	WriteFile(hFile, &k, sizeof(k), &dwWrites, NULL);
	WriteFile(hFile, &l, sizeof(l), &dwWrites, NULL);
	CloseHandle(hFile);

	CFile file;
	file.Open("3.txt", CFile::modeCreate | CFile::modeWrite);
	file.Write(ch, strlen(ch));
	file.Write(&k, sizeof(k));
	file.Write(&l, sizeof(l));
	file.Close();

	//写入txt未乱码,可读
	ofstream m_outC;
	m_outC.open("4.txt", std::ios::out);
	m_outC << ch << endl;
	m_outC << k << endl;
	//m_outC << l << endl;//写入l数组地址
	m_outC << l[0] << endl;
	m_outC << l[1] << endl;
	m_outC << l[2] << endl;
	m_outC << ch << k << l[0] << l;
	m_outC.close();

	//序列化
	CFile fileA;
	fileA.Open("5.txt", CFile::modeCreate | CFile::modeWrite);
	CArchive ar(&fileA, CArchive::store);
	ar << *ch;//需要一个一个的系列化
	ar << *ch + 1;//写入l字母,相当于将k的ASCII码+1
	ar << *(ch + 1);//写入的j字母
	ar << k;
	ar << '\n' << l[0] << '\n' << l[1] << '\n' << l[2];//写入乱码
	ar << cs;
}

/*sizeof操作符的结果类型为size_t(The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types).
This keyword returns a value of type size_t.)(它在头文件用typedfe定义为unsigned int类型),计算的是分配空间的实际字节数。
strlen结果类型也为size_t(size_t strlen(const char *string)),但strlen是计算的空间中字符的个数(不包括‘\0’)。

sizeof是运算符,可以以类型、函数、做参数 。strlen是函数,只能以char*(字符串)做参数。而且,要想得到的结果正确必须包含 ‘\0’(通过strlen的实现得知)。

sizeof是在编译的时候就将结果计算出来了是类型所占空间的字节数,所以以数组名做参数时计算的是整个数组的大小。
而strlen是在运行的时候才开始计算结果,这是计算的结果不再是类型所占内存的大小,数组名就退化为指针了。

另外,sizeof不能计算动态分配空间的大小如:
char *s = (char *)malloc(20);
strcpy(s, str);
结果sizeof(s)=4
最后的sizeof计算的是指针(sizeof(char *)) 的大小,为4。当适用了于一个结构类型时或变量,
sizeof 返回实际的大小, 当适用一静态地空间数组, sizeof 归还全部数组的尺寸。 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸

char* s = "0123456789";
sizeof(s);     //结果 4    ===》s是指向字符串常量的字符指针
sizeof(*s);    //结果 1    ===》*s是第一个字符
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
strlen(*s);     //结果 10   ===》错误


char s[] = "0123456789";
sizeof(s);     //结果 11   ===》s是数组,计算到\0位置,因此是10+1
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
sizeof(*s);    //结果 1    ===》*s是第一个字符

char s[100] = "0123456789";
sizeof(s);     //结果是100 ===》s表示在内存中的大小 100×1
strlen(s);     //结果是10  ===》strlen是个函数内部实现是用一个循环计算到\0为止之前

int s[100] = "0123456789";
sizeof(s);     //结果 400  ===》s表示再内存中的大小 100×4
strlen(s);     //错误      ===》strlen的参数只能是char* 且必须是以‘\0‘结尾的

char q[]="abc";
char p[]="a\n";
sizeof(q),sizeof(p),strlen(q),strlen(p);\\结果是 4 3 3 2

char p[] = {'a','b','c','d','e','f','g','h'};
char q[] = {'a','b','c','d,'\0','e','f','g'};
sizeof(p);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(p);     //为一个随机值,结果与编译器有关,不同编译器结果一般不同
sizeof(q);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(q);     //结果为4 ===》存在'\0',遇到'\0'计算停止。
*/

 


位置,因此是10+1 strlen(s); //结果 10 ===》有10个字符,strlen是个函数内部实现是用一个循环计算到

 

 


#include<stdio.h>
#include<string>
#include<afxwin.h>
#include<fstream>
#include<iostream>
using namespace std;
 
int main()
{
	int k = 12;
	float l[3] = { 1.23, 3.28, 2.6 };
	char *ch = "kjgd好几个宽度激光kgaskga";
	string ss("gk好几个j");
	string s = "gg航空国际化kljh";//strlen无法使用
	CString cs = "kg汉口开个房kk";
	CString css("hjf好几个科技hj");
	CString kcs;
	kcs.Format("%d", k);

	//FILE *pf = fopen("1.txt", "wb");
	FILE *pf;
	fopen_s(&pf, "1.txt", "w");
	fwrite(ch, 1, strlen(ch), pf);
	//fwrite(ch, 1, sizeof(ch), pf);// sizeof(ch)=4,只能写入kjgd4个字母
	fwrite(&k, 1, sizeof(k), pf);
	//fwrite(&l, 1, sizeof(l), pf);//很有问题
	fwrite(&l[0], sizeof(float), 1,pf);
	fwrite(l, sizeof(float),3, pf);
	fwrite(cs, 1, strlen(cs), pf);
	fwrite(css, 1, strlen(css), pf);
	fwrite(kcs, 1, strlen(kcs), pf);
	fprintf_s(pf, "\n%d", k);//int类型写入文件正确方法
	fprintf_s(pf, "%f %f %f\n", l[0], l[1], l[2]);
	//fprintf_s(pf, "%s %s %s\n", l[0], l[1], l[2]);//错误的写入,文件未写入任何数据
	//fprintf_s(pf, "%s", &l[0]);
	fwrite(&k, sizeof(k),1, pf);

	/*CString is;
	is = _itoa_s(k, is, 10);
	fwrite(is, 1, strlen(is), pf);*/

	//fwrite(s, 1, s.length(), pf);
	fwrite(s.c_str(), 1, s.length(), pf);//string类型写入文件正确方法
	/*const char *c_str();
	c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同.
		这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
		注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
	比如:最好不要这样 :
	char* c;
	string s = "1234";
	c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将一个const char *赋与一个char *。

	应该这样用:
		char c[20];
	string s = "1234";
	strcpy(c, s.c_str());
	这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作*/
	//string fs;
	//sprintf(fs, "%f", l[0]);//字符串在内存中是一个常量字符串数组
	fclose(pf);

	HANDLE hFile;
	hFile = CreateFile("2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);//创建文件
	DWORD dwWrites;//接收实际写入的字节数
	WriteFile(hFile, ch, strlen(ch), &dwWrites, NULL);
	WriteFile(hFile, &k, sizeof(k), &dwWrites, NULL);
	WriteFile(hFile, &l, sizeof(l), &dwWrites, NULL);
	CloseHandle(hFile);

	CFile file;
	file.Open("3.txt", CFile::modeCreate | CFile::modeWrite);
	file.Write(ch, strlen(ch));
	file.Write(&k, sizeof(k));
	file.Write(&l, sizeof(l));
	file.Close();

	//写入txt未乱码,可读
	ofstream m_outC;
	m_outC.open("4.txt", std::ios::out);
	m_outC << ch << endl;
	m_outC << k << endl;
	//m_outC << l << endl;//写入l数组地址
	m_outC << l[0] << endl;
	m_outC << l[1] << endl;
	m_outC << l[2] << endl;
	m_outC << ch << k << l[0] << l;
	m_outC.close();

	//序列化
	CFile fileA;
	fileA.Open("5.txt", CFile::modeCreate | CFile::modeWrite);
	CArchive ar(&fileA, CArchive::store);
	ar << *ch;//需要一个一个的系列化
	ar << *ch + 1;//写入l字母,相当于将k的ASCII码+1
	ar << *(ch + 1);//写入的j字母
	ar << k;
	ar << '\n' << l[0] << '\n' << l[1] << '\n' << l[2];//写入乱码
	ar << cs;
}

/*sizeof操作符的结果类型为size_t(The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types).
This keyword returns a value of type size_t.)(它在头文件用typedfe定义为unsigned int类型),计算的是分配空间的实际字节数。
strlen结果类型也为size_t(size_t strlen(const char *string)),但strlen是计算的空间中字符的个数(不包括‘\0’)。

sizeof是运算符,可以以类型、函数、做参数 。strlen是函数,只能以char*(字符串)做参数。而且,要想得到的结果正确必须包含 ‘\0’(通过strlen的实现得知)。

sizeof是在编译的时候就将结果计算出来了是类型所占空间的字节数,所以以数组名做参数时计算的是整个数组的大小。
而strlen是在运行的时候才开始计算结果,这是计算的结果不再是类型所占内存的大小,数组名就退化为指针了。

另外,sizeof不能计算动态分配空间的大小如:
char *s = (char *)malloc(20);
strcpy(s, str);
结果sizeof(s)=4
最后的sizeof计算的是指针(sizeof(char *)) 的大小,为4。当适用了于一个结构类型时或变量,
sizeof 返回实际的大小, 当适用一静态地空间数组, sizeof 归还全部数组的尺寸。 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸

char* s = "0123456789";
sizeof(s);     //结果 4    ===》s是指向字符串常量的字符指针
sizeof(*s);    //结果 1    ===》*s是第一个字符
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
strlen(*s);     //结果 10   ===》错误


char s[] = "0123456789";
sizeof(s);     //结果 11   ===》s是数组,计算到\0位置,因此是10+1
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
sizeof(*s);    //结果 1    ===》*s是第一个字符

char s[100] = "0123456789";
sizeof(s);     //结果是100 ===》s表示在内存中的大小 100×1
strlen(s);     //结果是10  ===》strlen是个函数内部实现是用一个循环计算到\0为止之前

int s[100] = "0123456789";
sizeof(s);     //结果 400  ===》s表示再内存中的大小 100×4
strlen(s);     //错误      ===》strlen的参数只能是char* 且必须是以‘\0‘结尾的

char q[]="abc";
char p[]="a\n";
sizeof(q),sizeof(p),strlen(q),strlen(p);\\结果是 4 3 3 2

char p[] = {'a','b','c','d','e','f','g','h'};
char q[] = {'a','b','c','d,'\0','e','f','g'};
sizeof(p);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(p);     //为一个随机值,结果与编译器有关,不同编译器结果一般不同
sizeof(q);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(q);     //结果为4 ===》存在'\0',遇到'\0'计算停止。
*/

 


为止之前 sizeof(*s); //结果 1 ===》*s是第一个字符 char s[100] = "0123456789"; sizeof(s); //结果是100 ===》s表示在内存中的大小 100×1 strlen(s); //结果是10 ===》strlen是个函数内部实现是用一个循环计算到

 

 


#include<stdio.h>
#include<string>
#include<afxwin.h>
#include<fstream>
#include<iostream>
using namespace std;
 
int main()
{
	int k = 12;
	float l[3] = { 1.23, 3.28, 2.6 };
	char *ch = "kjgd好几个宽度激光kgaskga";
	string ss("gk好几个j");
	string s = "gg航空国际化kljh";//strlen无法使用
	CString cs = "kg汉口开个房kk";
	CString css("hjf好几个科技hj");
	CString kcs;
	kcs.Format("%d", k);

	//FILE *pf = fopen("1.txt", "wb");
	FILE *pf;
	fopen_s(&pf, "1.txt", "w");
	fwrite(ch, 1, strlen(ch), pf);
	//fwrite(ch, 1, sizeof(ch), pf);// sizeof(ch)=4,只能写入kjgd4个字母
	fwrite(&k, 1, sizeof(k), pf);
	//fwrite(&l, 1, sizeof(l), pf);//很有问题
	fwrite(&l[0], sizeof(float), 1,pf);
	fwrite(l, sizeof(float),3, pf);
	fwrite(cs, 1, strlen(cs), pf);
	fwrite(css, 1, strlen(css), pf);
	fwrite(kcs, 1, strlen(kcs), pf);
	fprintf_s(pf, "\n%d", k);//int类型写入文件正确方法
	fprintf_s(pf, "%f %f %f\n", l[0], l[1], l[2]);
	//fprintf_s(pf, "%s %s %s\n", l[0], l[1], l[2]);//错误的写入,文件未写入任何数据
	//fprintf_s(pf, "%s", &l[0]);
	fwrite(&k, sizeof(k),1, pf);

	/*CString is;
	is = _itoa_s(k, is, 10);
	fwrite(is, 1, strlen(is), pf);*/

	//fwrite(s, 1, s.length(), pf);
	fwrite(s.c_str(), 1, s.length(), pf);//string类型写入文件正确方法
	/*const char *c_str();
	c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同.
		这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
		注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
	比如:最好不要这样 :
	char* c;
	string s = "1234";
	c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将一个const char *赋与一个char *。

	应该这样用:
		char c[20];
	string s = "1234";
	strcpy(c, s.c_str());
	这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作*/
	//string fs;
	//sprintf(fs, "%f", l[0]);//字符串在内存中是一个常量字符串数组
	fclose(pf);

	HANDLE hFile;
	hFile = CreateFile("2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);//创建文件
	DWORD dwWrites;//接收实际写入的字节数
	WriteFile(hFile, ch, strlen(ch), &dwWrites, NULL);
	WriteFile(hFile, &k, sizeof(k), &dwWrites, NULL);
	WriteFile(hFile, &l, sizeof(l), &dwWrites, NULL);
	CloseHandle(hFile);

	CFile file;
	file.Open("3.txt", CFile::modeCreate | CFile::modeWrite);
	file.Write(ch, strlen(ch));
	file.Write(&k, sizeof(k));
	file.Write(&l, sizeof(l));
	file.Close();

	//写入txt未乱码,可读
	ofstream m_outC;
	m_outC.open("4.txt", std::ios::out);
	m_outC << ch << endl;
	m_outC << k << endl;
	//m_outC << l << endl;//写入l数组地址
	m_outC << l[0] << endl;
	m_outC << l[1] << endl;
	m_outC << l[2] << endl;
	m_outC << ch << k << l[0] << l;
	m_outC.close();

	//序列化
	CFile fileA;
	fileA.Open("5.txt", CFile::modeCreate | CFile::modeWrite);
	CArchive ar(&fileA, CArchive::store);
	ar << *ch;//需要一个一个的系列化
	ar << *ch + 1;//写入l字母,相当于将k的ASCII码+1
	ar << *(ch + 1);//写入的j字母
	ar << k;
	ar << '\n' << l[0] << '\n' << l[1] << '\n' << l[2];//写入乱码
	ar << cs;
}

/*sizeof操作符的结果类型为size_t(The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types).
This keyword returns a value of type size_t.)(它在头文件用typedfe定义为unsigned int类型),计算的是分配空间的实际字节数。
strlen结果类型也为size_t(size_t strlen(const char *string)),但strlen是计算的空间中字符的个数(不包括‘\0’)。

sizeof是运算符,可以以类型、函数、做参数 。strlen是函数,只能以char*(字符串)做参数。而且,要想得到的结果正确必须包含 ‘\0’(通过strlen的实现得知)。

sizeof是在编译的时候就将结果计算出来了是类型所占空间的字节数,所以以数组名做参数时计算的是整个数组的大小。
而strlen是在运行的时候才开始计算结果,这是计算的结果不再是类型所占内存的大小,数组名就退化为指针了。

另外,sizeof不能计算动态分配空间的大小如:
char *s = (char *)malloc(20);
strcpy(s, str);
结果sizeof(s)=4
最后的sizeof计算的是指针(sizeof(char *)) 的大小,为4。当适用了于一个结构类型时或变量,
sizeof 返回实际的大小, 当适用一静态地空间数组, sizeof 归还全部数组的尺寸。 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸

char* s = "0123456789";
sizeof(s);     //结果 4    ===》s是指向字符串常量的字符指针
sizeof(*s);    //结果 1    ===》*s是第一个字符
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
strlen(*s);     //结果 10   ===》错误


char s[] = "0123456789";
sizeof(s);     //结果 11   ===》s是数组,计算到\0位置,因此是10+1
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
sizeof(*s);    //结果 1    ===》*s是第一个字符

char s[100] = "0123456789";
sizeof(s);     //结果是100 ===》s表示在内存中的大小 100×1
strlen(s);     //结果是10  ===》strlen是个函数内部实现是用一个循环计算到\0为止之前

int s[100] = "0123456789";
sizeof(s);     //结果 400  ===》s表示再内存中的大小 100×4
strlen(s);     //错误      ===》strlen的参数只能是char* 且必须是以‘\0‘结尾的

char q[]="abc";
char p[]="a\n";
sizeof(q),sizeof(p),strlen(q),strlen(p);\\结果是 4 3 3 2

char p[] = {'a','b','c','d','e','f','g','h'};
char q[] = {'a','b','c','d,'\0','e','f','g'};
sizeof(p);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(p);     //为一个随机值,结果与编译器有关,不同编译器结果一般不同
sizeof(q);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(q);     //结果为4 ===》存在'\0',遇到'\0'计算停止。
*/

 


为止之前 int s[100] = "0123456789"; sizeof(s); //结果 400 ===》s表示再内存中的大小 100×4 strlen(s); //错误 ===》strlen的参数只能是char* 且必须是以‘

 

 


#include<stdio.h>
#include<string>
#include<afxwin.h>
#include<fstream>
#include<iostream>
using namespace std;
 
int main()
{
	int k = 12;
	float l[3] = { 1.23, 3.28, 2.6 };
	char *ch = "kjgd好几个宽度激光kgaskga";
	string ss("gk好几个j");
	string s = "gg航空国际化kljh";//strlen无法使用
	CString cs = "kg汉口开个房kk";
	CString css("hjf好几个科技hj");
	CString kcs;
	kcs.Format("%d", k);

	//FILE *pf = fopen("1.txt", "wb");
	FILE *pf;
	fopen_s(&pf, "1.txt", "w");
	fwrite(ch, 1, strlen(ch), pf);
	//fwrite(ch, 1, sizeof(ch), pf);// sizeof(ch)=4,只能写入kjgd4个字母
	fwrite(&k, 1, sizeof(k), pf);
	//fwrite(&l, 1, sizeof(l), pf);//很有问题
	fwrite(&l[0], sizeof(float), 1,pf);
	fwrite(l, sizeof(float),3, pf);
	fwrite(cs, 1, strlen(cs), pf);
	fwrite(css, 1, strlen(css), pf);
	fwrite(kcs, 1, strlen(kcs), pf);
	fprintf_s(pf, "\n%d", k);//int类型写入文件正确方法
	fprintf_s(pf, "%f %f %f\n", l[0], l[1], l[2]);
	//fprintf_s(pf, "%s %s %s\n", l[0], l[1], l[2]);//错误的写入,文件未写入任何数据
	//fprintf_s(pf, "%s", &l[0]);
	fwrite(&k, sizeof(k),1, pf);

	/*CString is;
	is = _itoa_s(k, is, 10);
	fwrite(is, 1, strlen(is), pf);*/

	//fwrite(s, 1, s.length(), pf);
	fwrite(s.c_str(), 1, s.length(), pf);//string类型写入文件正确方法
	/*const char *c_str();
	c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同.
		这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
		注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
	比如:最好不要这样 :
	char* c;
	string s = "1234";
	c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将一个const char *赋与一个char *。

	应该这样用:
		char c[20];
	string s = "1234";
	strcpy(c, s.c_str());
	这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作*/
	//string fs;
	//sprintf(fs, "%f", l[0]);//字符串在内存中是一个常量字符串数组
	fclose(pf);

	HANDLE hFile;
	hFile = CreateFile("2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);//创建文件
	DWORD dwWrites;//接收实际写入的字节数
	WriteFile(hFile, ch, strlen(ch), &dwWrites, NULL);
	WriteFile(hFile, &k, sizeof(k), &dwWrites, NULL);
	WriteFile(hFile, &l, sizeof(l), &dwWrites, NULL);
	CloseHandle(hFile);

	CFile file;
	file.Open("3.txt", CFile::modeCreate | CFile::modeWrite);
	file.Write(ch, strlen(ch));
	file.Write(&k, sizeof(k));
	file.Write(&l, sizeof(l));
	file.Close();

	//写入txt未乱码,可读
	ofstream m_outC;
	m_outC.open("4.txt", std::ios::out);
	m_outC << ch << endl;
	m_outC << k << endl;
	//m_outC << l << endl;//写入l数组地址
	m_outC << l[0] << endl;
	m_outC << l[1] << endl;
	m_outC << l[2] << endl;
	m_outC << ch << k << l[0] << l;
	m_outC.close();

	//序列化
	CFile fileA;
	fileA.Open("5.txt", CFile::modeCreate | CFile::modeWrite);
	CArchive ar(&fileA, CArchive::store);
	ar << *ch;//需要一个一个的系列化
	ar << *ch + 1;//写入l字母,相当于将k的ASCII码+1
	ar << *(ch + 1);//写入的j字母
	ar << k;
	ar << '\n' << l[0] << '\n' << l[1] << '\n' << l[2];//写入乱码
	ar << cs;
}

/*sizeof操作符的结果类型为size_t(The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types).
This keyword returns a value of type size_t.)(它在头文件用typedfe定义为unsigned int类型),计算的是分配空间的实际字节数。
strlen结果类型也为size_t(size_t strlen(const char *string)),但strlen是计算的空间中字符的个数(不包括‘\0’)。

sizeof是运算符,可以以类型、函数、做参数 。strlen是函数,只能以char*(字符串)做参数。而且,要想得到的结果正确必须包含 ‘\0’(通过strlen的实现得知)。

sizeof是在编译的时候就将结果计算出来了是类型所占空间的字节数,所以以数组名做参数时计算的是整个数组的大小。
而strlen是在运行的时候才开始计算结果,这是计算的结果不再是类型所占内存的大小,数组名就退化为指针了。

另外,sizeof不能计算动态分配空间的大小如:
char *s = (char *)malloc(20);
strcpy(s, str);
结果sizeof(s)=4
最后的sizeof计算的是指针(sizeof(char *)) 的大小,为4。当适用了于一个结构类型时或变量,
sizeof 返回实际的大小, 当适用一静态地空间数组, sizeof 归还全部数组的尺寸。 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸

char* s = "0123456789";
sizeof(s);     //结果 4    ===》s是指向字符串常量的字符指针
sizeof(*s);    //结果 1    ===》*s是第一个字符
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
strlen(*s);     //结果 10   ===》错误


char s[] = "0123456789";
sizeof(s);     //结果 11   ===》s是数组,计算到\0位置,因此是10+1
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
sizeof(*s);    //结果 1    ===》*s是第一个字符

char s[100] = "0123456789";
sizeof(s);     //结果是100 ===》s表示在内存中的大小 100×1
strlen(s);     //结果是10  ===》strlen是个函数内部实现是用一个循环计算到\0为止之前

int s[100] = "0123456789";
sizeof(s);     //结果 400  ===》s表示再内存中的大小 100×4
strlen(s);     //错误      ===》strlen的参数只能是char* 且必须是以‘\0‘结尾的

char q[]="abc";
char p[]="a\n";
sizeof(q),sizeof(p),strlen(q),strlen(p);\\结果是 4 3 3 2

char p[] = {'a','b','c','d','e','f','g','h'};
char q[] = {'a','b','c','d,'\0','e','f','g'};
sizeof(p);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(p);     //为一个随机值,结果与编译器有关,不同编译器结果一般不同
sizeof(q);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(q);     //结果为4 ===》存在'\0',遇到'\0'计算停止。
*/

 


‘结尾的 char q[]="abc"; char p[]="a\n"; sizeof(q),sizeof(p),strlen(q),strlen(p);\结果是 4 3 3 2 char p[] = {'a','b','c','d','e','f','g','h'}; char q[] = {'a','b','c','d,'

 

 


#include<stdio.h>
#include<string>
#include<afxwin.h>
#include<fstream>
#include<iostream>
using namespace std;
 
int main()
{
	int k = 12;
	float l[3] = { 1.23, 3.28, 2.6 };
	char *ch = "kjgd好几个宽度激光kgaskga";
	string ss("gk好几个j");
	string s = "gg航空国际化kljh";//strlen无法使用
	CString cs = "kg汉口开个房kk";
	CString css("hjf好几个科技hj");
	CString kcs;
	kcs.Format("%d", k);

	//FILE *pf = fopen("1.txt", "wb");
	FILE *pf;
	fopen_s(&pf, "1.txt", "w");
	fwrite(ch, 1, strlen(ch), pf);
	//fwrite(ch, 1, sizeof(ch), pf);// sizeof(ch)=4,只能写入kjgd4个字母
	fwrite(&k, 1, sizeof(k), pf);
	//fwrite(&l, 1, sizeof(l), pf);//很有问题
	fwrite(&l[0], sizeof(float), 1,pf);
	fwrite(l, sizeof(float),3, pf);
	fwrite(cs, 1, strlen(cs), pf);
	fwrite(css, 1, strlen(css), pf);
	fwrite(kcs, 1, strlen(kcs), pf);
	fprintf_s(pf, "\n%d", k);//int类型写入文件正确方法
	fprintf_s(pf, "%f %f %f\n", l[0], l[1], l[2]);
	//fprintf_s(pf, "%s %s %s\n", l[0], l[1], l[2]);//错误的写入,文件未写入任何数据
	//fprintf_s(pf, "%s", &l[0]);
	fwrite(&k, sizeof(k),1, pf);

	/*CString is;
	is = _itoa_s(k, is, 10);
	fwrite(is, 1, strlen(is), pf);*/

	//fwrite(s, 1, s.length(), pf);
	fwrite(s.c_str(), 1, s.length(), pf);//string类型写入文件正确方法
	/*const char *c_str();
	c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同.
		这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
		注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
	比如:最好不要这样 :
	char* c;
	string s = "1234";
	c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将一个const char *赋与一个char *。

	应该这样用:
		char c[20];
	string s = "1234";
	strcpy(c, s.c_str());
	这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作*/
	//string fs;
	//sprintf(fs, "%f", l[0]);//字符串在内存中是一个常量字符串数组
	fclose(pf);

	HANDLE hFile;
	hFile = CreateFile("2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);//创建文件
	DWORD dwWrites;//接收实际写入的字节数
	WriteFile(hFile, ch, strlen(ch), &dwWrites, NULL);
	WriteFile(hFile, &k, sizeof(k), &dwWrites, NULL);
	WriteFile(hFile, &l, sizeof(l), &dwWrites, NULL);
	CloseHandle(hFile);

	CFile file;
	file.Open("3.txt", CFile::modeCreate | CFile::modeWrite);
	file.Write(ch, strlen(ch));
	file.Write(&k, sizeof(k));
	file.Write(&l, sizeof(l));
	file.Close();

	//写入txt未乱码,可读
	ofstream m_outC;
	m_outC.open("4.txt", std::ios::out);
	m_outC << ch << endl;
	m_outC << k << endl;
	//m_outC << l << endl;//写入l数组地址
	m_outC << l[0] << endl;
	m_outC << l[1] << endl;
	m_outC << l[2] << endl;
	m_outC << ch << k << l[0] << l;
	m_outC.close();

	//序列化
	CFile fileA;
	fileA.Open("5.txt", CFile::modeCreate | CFile::modeWrite);
	CArchive ar(&fileA, CArchive::store);
	ar << *ch;//需要一个一个的系列化
	ar << *ch + 1;//写入l字母,相当于将k的ASCII码+1
	ar << *(ch + 1);//写入的j字母
	ar << k;
	ar << '\n' << l[0] << '\n' << l[1] << '\n' << l[2];//写入乱码
	ar << cs;
}

/*sizeof操作符的结果类型为size_t(The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types).
This keyword returns a value of type size_t.)(它在头文件用typedfe定义为unsigned int类型),计算的是分配空间的实际字节数。
strlen结果类型也为size_t(size_t strlen(const char *string)),但strlen是计算的空间中字符的个数(不包括‘\0’)。

sizeof是运算符,可以以类型、函数、做参数 。strlen是函数,只能以char*(字符串)做参数。而且,要想得到的结果正确必须包含 ‘\0’(通过strlen的实现得知)。

sizeof是在编译的时候就将结果计算出来了是类型所占空间的字节数,所以以数组名做参数时计算的是整个数组的大小。
而strlen是在运行的时候才开始计算结果,这是计算的结果不再是类型所占内存的大小,数组名就退化为指针了。

另外,sizeof不能计算动态分配空间的大小如:
char *s = (char *)malloc(20);
strcpy(s, str);
结果sizeof(s)=4
最后的sizeof计算的是指针(sizeof(char *)) 的大小,为4。当适用了于一个结构类型时或变量,
sizeof 返回实际的大小, 当适用一静态地空间数组, sizeof 归还全部数组的尺寸。 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸

char* s = "0123456789";
sizeof(s);     //结果 4    ===》s是指向字符串常量的字符指针
sizeof(*s);    //结果 1    ===》*s是第一个字符
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
strlen(*s);     //结果 10   ===》错误


char s[] = "0123456789";
sizeof(s);     //结果 11   ===》s是数组,计算到\0位置,因此是10+1
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
sizeof(*s);    //结果 1    ===》*s是第一个字符

char s[100] = "0123456789";
sizeof(s);     //结果是100 ===》s表示在内存中的大小 100×1
strlen(s);     //结果是10  ===》strlen是个函数内部实现是用一个循环计算到\0为止之前

int s[100] = "0123456789";
sizeof(s);     //结果 400  ===》s表示再内存中的大小 100×4
strlen(s);     //错误      ===》strlen的参数只能是char* 且必须是以‘\0‘结尾的

char q[]="abc";
char p[]="a\n";
sizeof(q),sizeof(p),strlen(q),strlen(p);\\结果是 4 3 3 2

char p[] = {'a','b','c','d','e','f','g','h'};
char q[] = {'a','b','c','d,'\0','e','f','g'};
sizeof(p);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(p);     //为一个随机值,结果与编译器有关,不同编译器结果一般不同
sizeof(q);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(q);     //结果为4 ===》存在'\0',遇到'\0'计算停止。
*/

 


','e','f','g'}; sizeof(p); //结果是8 ===》p表示在内存中的大小 8×1 strlen(p); //为一个随机值,结果与编译器有关,不同编译器结果一般不同 sizeof(q); //结果是8 ===》p表示在内存中的大小 8×1 strlen(q); //结果为4 ===》存在'

 

 


#include<stdio.h>
#include<string>
#include<afxwin.h>
#include<fstream>
#include<iostream>
using namespace std;
 
int main()
{
	int k = 12;
	float l[3] = { 1.23, 3.28, 2.6 };
	char *ch = "kjgd好几个宽度激光kgaskga";
	string ss("gk好几个j");
	string s = "gg航空国际化kljh";//strlen无法使用
	CString cs = "kg汉口开个房kk";
	CString css("hjf好几个科技hj");
	CString kcs;
	kcs.Format("%d", k);

	//FILE *pf = fopen("1.txt", "wb");
	FILE *pf;
	fopen_s(&pf, "1.txt", "w");
	fwrite(ch, 1, strlen(ch), pf);
	//fwrite(ch, 1, sizeof(ch), pf);// sizeof(ch)=4,只能写入kjgd4个字母
	fwrite(&k, 1, sizeof(k), pf);
	//fwrite(&l, 1, sizeof(l), pf);//很有问题
	fwrite(&l[0], sizeof(float), 1,pf);
	fwrite(l, sizeof(float),3, pf);
	fwrite(cs, 1, strlen(cs), pf);
	fwrite(css, 1, strlen(css), pf);
	fwrite(kcs, 1, strlen(kcs), pf);
	fprintf_s(pf, "\n%d", k);//int类型写入文件正确方法
	fprintf_s(pf, "%f %f %f\n", l[0], l[1], l[2]);
	//fprintf_s(pf, "%s %s %s\n", l[0], l[1], l[2]);//错误的写入,文件未写入任何数据
	//fprintf_s(pf, "%s", &l[0]);
	fwrite(&k, sizeof(k),1, pf);

	/*CString is;
	is = _itoa_s(k, is, 10);
	fwrite(is, 1, strlen(is), pf);*/

	//fwrite(s, 1, s.length(), pf);
	fwrite(s.c_str(), 1, s.length(), pf);//string类型写入文件正确方法
	/*const char *c_str();
	c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同.
		这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
		注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
	比如:最好不要这样 :
	char* c;
	string s = "1234";
	c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将一个const char *赋与一个char *。

	应该这样用:
		char c[20];
	string s = "1234";
	strcpy(c, s.c_str());
	这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作*/
	//string fs;
	//sprintf(fs, "%f", l[0]);//字符串在内存中是一个常量字符串数组
	fclose(pf);

	HANDLE hFile;
	hFile = CreateFile("2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);//创建文件
	DWORD dwWrites;//接收实际写入的字节数
	WriteFile(hFile, ch, strlen(ch), &dwWrites, NULL);
	WriteFile(hFile, &k, sizeof(k), &dwWrites, NULL);
	WriteFile(hFile, &l, sizeof(l), &dwWrites, NULL);
	CloseHandle(hFile);

	CFile file;
	file.Open("3.txt", CFile::modeCreate | CFile::modeWrite);
	file.Write(ch, strlen(ch));
	file.Write(&k, sizeof(k));
	file.Write(&l, sizeof(l));
	file.Close();

	//写入txt未乱码,可读
	ofstream m_outC;
	m_outC.open("4.txt", std::ios::out);
	m_outC << ch << endl;
	m_outC << k << endl;
	//m_outC << l << endl;//写入l数组地址
	m_outC << l[0] << endl;
	m_outC << l[1] << endl;
	m_outC << l[2] << endl;
	m_outC << ch << k << l[0] << l;
	m_outC.close();

	//序列化
	CFile fileA;
	fileA.Open("5.txt", CFile::modeCreate | CFile::modeWrite);
	CArchive ar(&fileA, CArchive::store);
	ar << *ch;//需要一个一个的系列化
	ar << *ch + 1;//写入l字母,相当于将k的ASCII码+1
	ar << *(ch + 1);//写入的j字母
	ar << k;
	ar << '\n' << l[0] << '\n' << l[1] << '\n' << l[2];//写入乱码
	ar << cs;
}

/*sizeof操作符的结果类型为size_t(The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types).
This keyword returns a value of type size_t.)(它在头文件用typedfe定义为unsigned int类型),计算的是分配空间的实际字节数。
strlen结果类型也为size_t(size_t strlen(const char *string)),但strlen是计算的空间中字符的个数(不包括‘\0’)。

sizeof是运算符,可以以类型、函数、做参数 。strlen是函数,只能以char*(字符串)做参数。而且,要想得到的结果正确必须包含 ‘\0’(通过strlen的实现得知)。

sizeof是在编译的时候就将结果计算出来了是类型所占空间的字节数,所以以数组名做参数时计算的是整个数组的大小。
而strlen是在运行的时候才开始计算结果,这是计算的结果不再是类型所占内存的大小,数组名就退化为指针了。

另外,sizeof不能计算动态分配空间的大小如:
char *s = (char *)malloc(20);
strcpy(s, str);
结果sizeof(s)=4
最后的sizeof计算的是指针(sizeof(char *)) 的大小,为4。当适用了于一个结构类型时或变量,
sizeof 返回实际的大小, 当适用一静态地空间数组, sizeof 归还全部数组的尺寸。 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸

char* s = "0123456789";
sizeof(s);     //结果 4    ===》s是指向字符串常量的字符指针
sizeof(*s);    //结果 1    ===》*s是第一个字符
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
strlen(*s);     //结果 10   ===》错误


char s[] = "0123456789";
sizeof(s);     //结果 11   ===》s是数组,计算到\0位置,因此是10+1
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
sizeof(*s);    //结果 1    ===》*s是第一个字符

char s[100] = "0123456789";
sizeof(s);     //结果是100 ===》s表示在内存中的大小 100×1
strlen(s);     //结果是10  ===》strlen是个函数内部实现是用一个循环计算到\0为止之前

int s[100] = "0123456789";
sizeof(s);     //结果 400  ===》s表示再内存中的大小 100×4
strlen(s);     //错误      ===》strlen的参数只能是char* 且必须是以‘\0‘结尾的

char q[]="abc";
char p[]="a\n";
sizeof(q),sizeof(p),strlen(q),strlen(p);\\结果是 4 3 3 2

char p[] = {'a','b','c','d','e','f','g','h'};
char q[] = {'a','b','c','d,'\0','e','f','g'};
sizeof(p);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(p);     //为一个随机值,结果与编译器有关,不同编译器结果一般不同
sizeof(q);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(q);     //结果为4 ===》存在'\0',遇到'\0'计算停止。
*/

 


',遇到'

 

 


#include<stdio.h>
#include<string>
#include<afxwin.h>
#include<fstream>
#include<iostream>
using namespace std;
 
int main()
{
	int k = 12;
	float l[3] = { 1.23, 3.28, 2.6 };
	char *ch = "kjgd好几个宽度激光kgaskga";
	string ss("gk好几个j");
	string s = "gg航空国际化kljh";//strlen无法使用
	CString cs = "kg汉口开个房kk";
	CString css("hjf好几个科技hj");
	CString kcs;
	kcs.Format("%d", k);

	//FILE *pf = fopen("1.txt", "wb");
	FILE *pf;
	fopen_s(&pf, "1.txt", "w");
	fwrite(ch, 1, strlen(ch), pf);
	//fwrite(ch, 1, sizeof(ch), pf);// sizeof(ch)=4,只能写入kjgd4个字母
	fwrite(&k, 1, sizeof(k), pf);
	//fwrite(&l, 1, sizeof(l), pf);//很有问题
	fwrite(&l[0], sizeof(float), 1,pf);
	fwrite(l, sizeof(float),3, pf);
	fwrite(cs, 1, strlen(cs), pf);
	fwrite(css, 1, strlen(css), pf);
	fwrite(kcs, 1, strlen(kcs), pf);
	fprintf_s(pf, "\n%d", k);//int类型写入文件正确方法
	fprintf_s(pf, "%f %f %f\n", l[0], l[1], l[2]);
	//fprintf_s(pf, "%s %s %s\n", l[0], l[1], l[2]);//错误的写入,文件未写入任何数据
	//fprintf_s(pf, "%s", &l[0]);
	fwrite(&k, sizeof(k),1, pf);

	/*CString is;
	is = _itoa_s(k, is, 10);
	fwrite(is, 1, strlen(is), pf);*/

	//fwrite(s, 1, s.length(), pf);
	fwrite(s.c_str(), 1, s.length(), pf);//string类型写入文件正确方法
	/*const char *c_str();
	c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同.
		这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
		注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
	比如:最好不要这样 :
	char* c;
	string s = "1234";
	c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将一个const char *赋与一个char *。

	应该这样用:
		char c[20];
	string s = "1234";
	strcpy(c, s.c_str());
	这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作*/
	//string fs;
	//sprintf(fs, "%f", l[0]);//字符串在内存中是一个常量字符串数组
	fclose(pf);

	HANDLE hFile;
	hFile = CreateFile("2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);//创建文件
	DWORD dwWrites;//接收实际写入的字节数
	WriteFile(hFile, ch, strlen(ch), &dwWrites, NULL);
	WriteFile(hFile, &k, sizeof(k), &dwWrites, NULL);
	WriteFile(hFile, &l, sizeof(l), &dwWrites, NULL);
	CloseHandle(hFile);

	CFile file;
	file.Open("3.txt", CFile::modeCreate | CFile::modeWrite);
	file.Write(ch, strlen(ch));
	file.Write(&k, sizeof(k));
	file.Write(&l, sizeof(l));
	file.Close();

	//写入txt未乱码,可读
	ofstream m_outC;
	m_outC.open("4.txt", std::ios::out);
	m_outC << ch << endl;
	m_outC << k << endl;
	//m_outC << l << endl;//写入l数组地址
	m_outC << l[0] << endl;
	m_outC << l[1] << endl;
	m_outC << l[2] << endl;
	m_outC << ch << k << l[0] << l;
	m_outC.close();

	//序列化
	CFile fileA;
	fileA.Open("5.txt", CFile::modeCreate | CFile::modeWrite);
	CArchive ar(&fileA, CArchive::store);
	ar << *ch;//需要一个一个的系列化
	ar << *ch + 1;//写入l字母,相当于将k的ASCII码+1
	ar << *(ch + 1);//写入的j字母
	ar << k;
	ar << '\n' << l[0] << '\n' << l[1] << '\n' << l[2];//写入乱码
	ar << cs;
}

/*sizeof操作符的结果类型为size_t(The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types).
This keyword returns a value of type size_t.)(它在头文件用typedfe定义为unsigned int类型),计算的是分配空间的实际字节数。
strlen结果类型也为size_t(size_t strlen(const char *string)),但strlen是计算的空间中字符的个数(不包括‘\0’)。

sizeof是运算符,可以以类型、函数、做参数 。strlen是函数,只能以char*(字符串)做参数。而且,要想得到的结果正确必须包含 ‘\0’(通过strlen的实现得知)。

sizeof是在编译的时候就将结果计算出来了是类型所占空间的字节数,所以以数组名做参数时计算的是整个数组的大小。
而strlen是在运行的时候才开始计算结果,这是计算的结果不再是类型所占内存的大小,数组名就退化为指针了。

另外,sizeof不能计算动态分配空间的大小如:
char *s = (char *)malloc(20);
strcpy(s, str);
结果sizeof(s)=4
最后的sizeof计算的是指针(sizeof(char *)) 的大小,为4。当适用了于一个结构类型时或变量,
sizeof 返回实际的大小, 当适用一静态地空间数组, sizeof 归还全部数组的尺寸。 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸

char* s = "0123456789";
sizeof(s);     //结果 4    ===》s是指向字符串常量的字符指针
sizeof(*s);    //结果 1    ===》*s是第一个字符
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
strlen(*s);     //结果 10   ===》错误


char s[] = "0123456789";
sizeof(s);     //结果 11   ===》s是数组,计算到\0位置,因此是10+1
strlen(s);     //结果 10   ===》有10个字符,strlen是个函数内部实现是用一个循环计算到\0为止之前
sizeof(*s);    //结果 1    ===》*s是第一个字符

char s[100] = "0123456789";
sizeof(s);     //结果是100 ===》s表示在内存中的大小 100×1
strlen(s);     //结果是10  ===》strlen是个函数内部实现是用一个循环计算到\0为止之前

int s[100] = "0123456789";
sizeof(s);     //结果 400  ===》s表示再内存中的大小 100×4
strlen(s);     //错误      ===》strlen的参数只能是char* 且必须是以‘\0‘结尾的

char q[]="abc";
char p[]="a\n";
sizeof(q),sizeof(p),strlen(q),strlen(p);\\结果是 4 3 3 2

char p[] = {'a','b','c','d','e','f','g','h'};
char q[] = {'a','b','c','d,'\0','e','f','g'};
sizeof(p);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(p);     //为一个随机值,结果与编译器有关,不同编译器结果一般不同
sizeof(q);     //结果是8 ===》p表示在内存中的大小 8×1
strlen(q);     //结果为4 ===》存在'\0',遇到'\0'计算停止。
*/

 


'计算停止。 */ #include<stdio.h> #include<string> #in



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: