【问题标题】:I'm trying to print a char to a output file, however, my compilers says I'm printing an int我正在尝试将 char 打印到输出文件,但是,我的编译器说我正在打印 int
【发布时间】: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 与潜在有效字符区分开来。

标签: c io printf


【解决方案1】:

你的定义是:

char filename[30], curr;

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

char filename[30], curr[size];

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

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

%s 不行,因为你要输入一个字符,而不是一个字符串。

【讨论】:

    【解决方案2】:

    一切都在这里

    Pass1.c:在函数'main'中:

    Pass1.c:53:6:警告:格式“%s”需要“char *”类型的参数, 但参数 3 的类型为“int”[-Wformat=]

    这实际上是在告诉您使用'%s' 进行打印,但您应该使用'%c'

    所以你所要做的就是改变: fprintf(ofp, "%s", curr);fprintf(ofp, "%c", curr);

    您的编译器基本上说明了一切,您只需阅读警告即可。它也只是一个警告,因此即使它不正确,它仍然可以编译。这些警告旨在帮助您。

    【讨论】:

      【解决方案3】:

      换行

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

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

      打印单个字符。


      其他 cmets:您应该使用 int main(void) 而不是 void main(),并且您的程序中不使用变量 perDecdecChar

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-09
        • 1970-01-01
        • 1970-01-01
        • 2019-11-13
        • 1970-01-01
        • 1970-01-01
        • 2019-05-06
        • 1970-01-01
        相关资源
        最近更新 更多