我尝试使用传递函数作为参数,但我被卡住了。我有两个问题: 首先,我尝试在tfm中调用大写并打开.txt。第二,如何从in.txt中读取字符作为字符串并分配给char内容[]?
#include <stdio.h>
void tfm( char str_filename[], void(*pf_convertion)( char content[]));
void uppercase(char content[]); //converts all letters to uppercase
int main(){
puts("-------------------------------");
printf("tfm:\n");
tfm("in.txt", uppercase);
puts("-------------------------------");
return 0;
}
void tfm( char str_filename[], void(*pf_convertion)( char content[])){
FILE *fptr_in;
if((fptr_in=fopen(content,"r"))==NULL){
printf("Error reading file\n");
}
else{
(*pf_convertion)(str_filename);
}
}
void uppercase(char content[]){
char ch;
int st;
for(st=fscanf(fptr_in,"%c",&ch);
st==1;
st=fscanf(fptr_in,"%c",&ch)){
if('a'<=ch && ch<='z'){
ch-=32;
printf("%c",ch);
}
else
printf("%c",ch);
}
}