我正在编写Unix expand实用程序的一个版本,它将文件中的制表符替换为空格。为此,我读取每个字符,并测试它是否是制表符。如果是,则用给定的空格量替换制表符,否则将打印字符。
我的main方法是这样的
int main(int argc, char *argv[]){
FILE *fp;
char *help1="-help";
char *help2= "--help";
//int spaces; //number of spaces to replace tabs
fp= fopen(argv[1], "rw");
parse_file(fp, 4);
fclose(fp);
return 0;
}parse_file方法如下所示
void parse_file(FILE *fp, int spaces)
{
int i; //loop counter
char c; //current character
while (c!= EOF)
{
c= getchar(); //get char from stream
if (c=='\t') //if char is a tab
{
for (i=0; i< spaces; i++)
putchar(" "); //replace with spaces
}
else
putchar(c); //otherwise, print the character
}}
编译时,我从指针得到一个整数,没有putchar(" ");的强制转换警告,程序在执行时遇到了段错误。
所以,我的问题是:
1-警告"makes from pointer without cast“是关于什么的?我能做些什么来解决这个问题?
2-代码在执行时生成一个段错误,并将一个文本文件作为参数传入。这段代码中有没有什么东西会导致这种情况呢?
发布于 2011-02-10 23:18:19
正如其他人所说的,re。char与string的使用。至于您看到的实际错误消息背后的逻辑,该字符串是指向常量字符数组的指针。因此,错误是说它正在将指针转换为int。大多数char函数都使用int。
https://stackoverflow.com/questions/4958793
复制相似问题