Skip to content

Instantly share code, notes, and snippets.

@b8jdkal
Created September 20, 2017 10:16
Show Gist options
  • Select an option

  • Save b8jdkal/99af844f9fe7b1519f911fa79ba5460e to your computer and use it in GitHub Desktop.

Select an option

Save b8jdkal/99af844f9fe7b1519f911fa79ba5460e to your computer and use it in GitHub Desktop.
用c来读文件, 运行./testcread然后输入文件名
#include <stdio.h>
#include <stdlib.h>
int main()
{
int d = 0;
char ch, file_name[525];
FILE *fp;
printf("Enter the name of file you wish to see\n");
gets(file_name);
fp = fopen(file_name,"r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name);
//while( ( ch = fgetc(fp) ) != EOF )
while((ch=fgetc(fp)) != EOF )
d+=1;
printf("%d",d);
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment