C语言文件操作练习

C语言文件操作练习

#include "stdafx.h"
#include 
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	FILE* fp = fopen("xx.c", "w+");
	if (!fp)
		cout << "open error" << endl;

	fputc('a', fp);
	fputc('b', fp);
	fputs("china\n", fp);
	fwrite("itcast", 6, 1, fp);

	rewind(fp); //重置指针,指向开头
	fseek(fp, 0, SEEK_SET); //重置指针,指向开头

	//char ch;
	//while ((ch = fgetc(fp)) != EOF)
	//{
	//	cout << ch;
	//}

	//while (ch = fgetc(fp), !feof(fp))
	//{
	//	//先读 再判断写
	//	cout << ch;
	//}

	//读多个数据
	char buf[1024];
	while (fgets(buf, 1024, fp) != NULL)
	{
		cout << buf;
	}

	fclose(fp);

	return 0;
}

你可能感兴趣的:(C语言文件操作练习)