c++写文件

#include
#include
int main() {
	const char* filename = "G:/test.txt";
	FILE *fp = fopen(filename,"wb");
	if (fp == NULL) {
		printf(" no such file");
	}
	char buf[] = "hello";
	int n=fwrite(buf, 1, 5, fp);

	//写入数字
	int data[] = { 0x100,0x200,0x300,0x400 };
	fwrite(data, 1, sizeof(data), fp);

	//以字符串的形式写入
	for (int i = 0; i < 4; i++) {
		char* text=(char*)malloc(100);
		sprintf(text, "%d", data[i]);
		fwrite(text, 1, sizeof(text), fp);
	}
	double a = 4 / 3.0;
	fwrite(&a, 1, sizeof(a), fp);

	char* text = (char*)malloc(100);
	sprintf(text, "%lf", a);
	fwrite(text, 1, sizeof(text), fp);

	char st[16] = "hello";
	fwrite(st, 1, 5, fp);
	fclose(fp);
}

 

你可能感兴趣的:(c++写文件)