gpt4 book ai didi

c - 我正在尝试将 char 打印到输出文件,但是,我的编译器说我正在打印 int

转载 作者:太空宇宙 更新时间:2023-11-04 06:22:28 25 4
gpt4 key购买 nike

Pass1.c: In function ‘main’:

Pass1.c:53:6: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘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
}

最佳答案

你的定义是:

char filename[30], curr;

这里 filename 是一个数组,但是 curr 不是。如果你想放一个字符串,这是正确的:

char filename[30], curr[size];

但是你只想放一个字符。在这种情况下,不要触及上面 curr 的定义,而是更改 fprintf 部分,如下所示:

fprintf(ofp,"%c", curr);

%s 不行,因为您要放置一个字符,而不是一个字符串。

关于c - 我正在尝试将 char 打印到输出文件,但是,我的编译器说我正在打印 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32404023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com