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

时间:2015-09-04 17:59:08

标签: c io printf

  

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

     

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

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

我得到的确切错误。我正在尝试使用fprintf将curr打印到输出文件。我运行程序并尝试从我的输入文件输出,我最终得到s分段错误。我第一次使用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
}

3 个答案:

答案 0 :(得分:2)

您的定义是:

char filename[30], curr;

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

char filename[30], curr[size];

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

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

%s不合适,因为你要放一个字符,而不是一个字符串。

答案 1 :(得分:1)

更改行

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

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

打印单个字符。

其他评论:您应该使用int main(void)而不是void main(),并且您的计划中不会使用变量perDecdecChar

答案 2 :(得分:1)

一切都在这里

  

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

     

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

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

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

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