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 }
%sis for a string. You want%cfor a character.currto be anint. That is the type thatgetc()returns, and you need to use that type to be able to distinguishEOFfrom a potentially-valid character.