I am writing a version of the Unix utility expandthat replaces tabs with spaces in a file. To do this, I read each character and check if it is a tab character. If so, it replaces the tab with the specified number of spaces, otherwise the character is printed.
My main method is like
int main(int argc, char *argv[]){
FILE *fp;
char *help1="-help";
char *help2= "--help";
fp= fopen(argv[1], "rw");
parse_file(fp, 4);
fclose(fp);
return 0;
}
parse_file method is similar to
void parse_file(FILE *fp, int spaces)
{
int i;
char c;
while (c!= EOF)
{
c= getchar();
if (c=='\t')
{
for (i=0; i< spaces; i++)
putchar(" ");
}
else
putchar(c);
}
}
When compiling, I get an integer from the pointer without warning warning for putchar(" ");, and when the program runs, segfault occurs.
So my questions are:
1- What is the warning "does an integer from a pointer without a cast"? What can I do to solve this problem?
2- segfault , . - , ?