【发布时间】:2015-09-04 17:59:07
【问题描述】:
Pass1.c:在函数'main'中:
Pass1.c:53:6:警告:格式“%s”需要“char *”类型的参数,但参数 3 的类型为“int”[-Wformat=]
fprintf(ofp,"%s", curr);
这是我得到的确切错误。我正在尝试使用 fprintf 将 curr 打印到输出文件。我运行程序并尝试从我的输入文件中输出,但最终得到了分段错误。我第一次使用c,不知道发生了什么。这是我的代码:
#include <stdio.h>
#include <ctype.h>
void main() {
int qflag, zflag, punctflag;
int skip;
int orgChar, decChar, codeChar; //# of original characters, decoded characters, and code sequences
double perDec;
FILE *ifp, *ofp; //input & output file pointers
char filename[30], curr; // filename and the current character input from the file
printf("Enter the filename to be scanned: "); // ask user for filename
scanf("%s", filename); //user filename input
ifp = fopen(filename, "r"); // open the file as read-only
ofp = fopen("output.txt", "w"); // open output file as write-only
while ((curr = getc(ifp)) != EOF) { // get the next char and as long as it is not the EOF, continue
if (qflag && isdigit(curr)) { //qflag is true and is digit is true
skip = (int) curr - 48; //skip # of digits
codeChar++; //add to coded char index
qflag = 0; //qflag now flase
} else if (qflag) { //if q isnt followed by a interger
fprintf(ofp, "q"); //print q
decChar++; //added to the decoded index
qflag = 0; //qflag now false
}
if (punctflag == 1 && isdigit(curr)) {
skip = (int) curr - 48;
punctflag = 0;
}
//If there is a special case where we have something z^g the else would be here
if (zflag && ispunct(curr)) {
punctflag = 1;
codeChar += 2;
zflag = 0;
} else if (zflag) {
fprintf(ofp, "z");
decChar++;
zflag = 0;
}
//must put in the X variable!!!!!!!!!!
if (curr == 'q' || curr == 'Q') {
qflag = 1;
} else if (curr == 'z' || curr == 'Z') {
zflag = 1;
}
if (zflag == 0 && qflag == 0 && skip == 0) { //need x here
fprintf(ofp, "%s", curr); //<------ getting issue here!
decChar++;
} else {
skip--;
}
}
fclose(ifp); //closes input file
fclose(ofp); //closes output file
}
【问题讨论】:
-
%s用于字符串。你想要%c作为一个角色。 -
此外,您确实需要
curr才能成为int。这是getc()返回的类型,您需要使用该类型才能将EOF与潜在有效字符区分开来。