标准I/O

作者:追风剑情 发布于:2020-3-25 10:21 分类:C

示例:读取文件


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//exit()
#include <stdlib.h>

int main(int argc, char* argv[])
{
	int ch; //读取文件时,储存每个字符的地方
	FILE* fp; //文件指针
	unsigned long count = 0;
	if (argc != 2)
	{
		printf("Usage: %s filename\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	//printf("file path: %s", argv[1]);
	if ((fp = fopen(argv[1], "r")) == NULL)
	{
		printf("Can't open %s\n", argv[1]);
		exit(EXIT_FAILURE);
	}
	while ((ch = getc(fp)) != EOF)
	{
		putc(ch, stdout); //与putchar(ch);相同
		count++;
	}
	//如果成功关闭,fclose()函数返回0,否则返回EOF
	//如果磁盘已满,移动硬盘被移除或出现I/O错误,都会导致调用fclose()函数失败
	if (fclose(fp) != 0) //关闭fp指定的文件,必要时刷新缓冲区
		printf("Error in closing file %s\n", argv[1]);
	printf("\nFile %s has %lu characters\n", argv[1], count);

	system("pause");
	return 0;
}


运行测试

2222.png

1111.png

标签: C语言

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号