0

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); 

There's the exact error I'm getting. I'm trying to print curr to an output file using fprintf. I run the program and try to output from my input file and I end up getting s segmentation fault. I'm using c for the first time and have no idea whats going on. Here's my code:

#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 } 
2
  • 2
    %s is for a string. You want %c for a character. Commented Sep 4, 2015 at 18:05
  • 1
    Moreover, you actually do need curr to be an int. That is the type that getc() returns, and you need to use that type to be able to distinguish EOF from a potentially-valid character. Commented Sep 4, 2015 at 18:08

3 Answers 3

2

Your definition is:

char filename[30], curr; 

Here filename is an array but curr is not. Here is what is correct if you want to put a string:

char filename[30], curr[size]; 

But you want to put a char only. In this case, don't touch to the definiton of the curr above and instead change the fprintf section as follows:

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

%s is not okay because you are going to put a char, not a string.

Sign up to request clarification or add additional context in comments.

Comments

1

Everything is in here

Pass1.c: In function ‘main’:

Pass1.c:53:6: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]

This is actually telling you that you used '%s' to print but you should use '%c'.

So all you have to do is change: fprintf(ofp, "%s", curr); to fprintf(ofp, "%c", curr);

Your compiler basically said it all, you simply had to read the warning. It is also just a warning so it still compiles even though it is not correct. These warnings are here to help you.

Comments

1

Change the line

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

to

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

to print a single character.


Additional comments: You should use int main(void) instead of void main() and the variables perDec and decChar are not used in your program.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.