用fgetc计算一个文件的大小,要求封装成函数
main.c
#include<stdio.h>
#include"work.h"
int main(int argc, const char *argv[])
{
FILE *FP = fopen(argv[1],"r");
int num=lenth(FP);
printf("字符数为%d\n",num);
fclose(FP);
return 0;
}
fun1.c
#include<stdio.h>
#include<errno.h>
#include"work.h"
int lenth(FILE*FP)
{
if(NULL==FP)
{
perror("fopen");
return -1;
}
char c;
int count=0;
for(c=fgetc(FP);c!=EOF;c=fgetc(FP))
{
count++;
}
return count;
}
fun.h
#ifndef __work_h_
#define __work_h_
int lenth(FILE*FP);
#endif
用fgetc计算一个文件有几行,要求封装成函数(linux操作系统以\n结尾,就算是最后一行也有一个’\n’)
mian.c
#include<stdio.h>
#include"work.h"
int main(int argc, const char *argv[])
{
FILE *FP = fopen(argv[1],"r");
int num=lenth(FP);
printf("字符数为%d\n",num);
fclose(FP);
FP = fopen(argv[1],"r");
int num2=line_num(FP);
printf("行数为%d\n",num2);
fclose(FP);
return 0;
}
fun.h
#ifndef __work_h_
#define __work_h_
int lenth(FILE*FP);
int line_num(FILE*FP);
#endif
fun.c
#include<stdio.h>
#include<errno.h>
#include"work.h"
int line_num(FILE*FP)
{
if(NULL==FP)
{
perror("fopen");
return -1;
}
char c;
int count=0;
for(c=fgetc(FP);c!=EOF;c=fgetc(FP))
{
if(c == '\n')
{
count++;
}
}
return count;
}
用fgets和fputs实现,拷贝一个文件,例如将1.c的内容拷贝给2.c
main.c
#include<stdio.h>
#include"work.h"
int main(int argc, const char *argv[])
{
char arr[999];
FILE *FP = fopen(argv[1],"r");
FILE *EP = fopen("./fun3.c","a");
int num=lenth(FP);
printf("字符数为%d\n",num);
fclose(FP);
FP = fopen(argv[1],"r");
int num2=line_num(FP);
printf("行数为%d\n",num2);
fclose(FP);
FP = fopen(argv[1],"r");
fgets_cp(arr,num,FP,EP);
fclose(FP);
return 0;
}
fun.c
#include<stdio.h>
#include<errno.h>
#include"work.h"
int fgets_cp(char *arr,int lenth,FILE*FP,FILE*EP)
{
if(NULL==FP || NULL==EP)
{
perror("fopen");
return -1;
}
while(1)
{
if(fgets(arr,lenth+1,FP)==NULL)
break;
fputs(arr,EP);
}
return 0;
}
fun.h
#ifndef __work_h_
#define __work_h_
int lenth(FILE*FP);
int line_num(FILE*FP);
int fgets_cp(char *arr,int num,FILE*FP,FILE*EP);
#endif
用fgets计算一个文件的大小,要求封装成函数
fun.c
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include"work.h"
int fgets_lenth(char *arr,int lenth,FILE*FP)
{
if(NULL==FP)
{
perror("fopen");
return -1;
}
int size=0;
while(1)
{
if(fgets(arr,lenth,FP)==NULL)
{
break;
}
size+=strlen(arr);
}
return size;
}
fun.h
#ifndef __work_h_
#define __work_h_
int lenth(FILE*FP);
int line_num(FILE*FP);
int fgets_cp(char *arr,int num,FILE*FP,FILE*EP);
int fgets_lenth(char *arr,int lenth,FILE*FP);
#endif
main.c
#include<stdio.h>
#include"work.h"
#include<string.h>
int main(int argc, const char *argv[])
{
char arr[999];
FILE *FP = fopen(argv[1],"r");
FILE *EP = fopen("./fun3.c","a");
int num=lenth(FP);
printf("字符数为%d\n",num);
fclose(FP);
FP = fopen(argv[1],"r");
int num2=line_num(FP);
printf("行数为%d\n",num2);
fclose(FP);
FP = fopen(argv[1],"r");
fgets_cp(arr,num,FP,EP);
fclose(FP);
FP = fopen(argv[1],"r");
int size=fgets_lenth(arr,num,FP);
printf("%d\n",size);
fclose(FP);
return 0;
}
用fgets计算一个文件有几行,要求封装成函数(linux操作系统以\n结尾,就算是最后一行也有一个’\n’)
main.c
#include<stdio.h>
#include"work.h"
#include<string.h>
int main(int argc, const char *argv[])
{
char arr[999];
FILE *FP = fopen(argv[1],"r");
FILE *EP = fopen("./fun3.c","a");
int num=lenth(FP);
printf("字符数为%d\n",num);
fclose(FP);
FP = fopen(argv[1],"r");
int num2=line_num(FP);
printf("行数为%d\n",num2);
fclose(FP);
FP = fopen(argv[1],"r");
fgets_cp(arr,num,FP,EP);
fclose(FP);
FP = fopen(argv[1],"r");
int size=fgets_lenth(arr,num,FP);
printf("字符数为%d\n",size);
fclose(FP);
FP = fopen(argv[1],"r");
int count=fgets_line(arr,num,FP);
fclose(FP);
printf("行数为%d\n",count);
return 0;
}
fun.c
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include"work.h"
int fgets_line(char *arr,int lenth,FILE*FP)
{
if(NULL==FP)
{
perror("fopen");
return -1;
}
int count=0;
while(1)
{
if(fgets(arr,lenth,FP)==NULL)
{
break;
}
count++;
}
return count;
}
fun.h
#ifndef __work_h_
#define __work_h_
int lenth(FILE*FP);
int line_num(FILE*FP);
int fgets_cp(char *arr,int num,FILE*FP,FILE*EP);
int fgets_lenth(char *arr,int lenth,FILE*FP);
int fgets_line(char *arr,int lenth,FILE*FP);
#endif