Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 打印到标准输出时去掉问号_C - Fatal编程技术网

C 打印到标准输出时去掉问号

C 打印到标准输出时去掉问号,c,C,我想去掉问号。STDIN和STDOUT。但我似乎失败了 #define MAXLEN 256 #define widthh argv[2] // fixed void wordWrap(int col, char *providedStr);<- my algo for wordWrap int main(int argc, char **argv) { int infd, outfd, bytes; char buf[MAXLEN]; int num =

我想去掉问号。STDIN和STDOUT。但我似乎失败了

#define MAXLEN 256
#define widthh argv[2] // fixed 
void wordWrap(int col, char *providedStr);<- my algo for wordWrap

int 
main(int argc, char **argv) {

    int infd, outfd, bytes;
    char buf[MAXLEN];
    int num = atoi(widthh);

    for (int i = 1; i < argc; i++) {
        infd = open(argv[i], O_RDONLY);
        bytes = read(infd, buf, MAXLEN);
       
        if (bytes > 0) {
            wordWrap(num, buf);
        }
        printf("\n"); //just for adding new line to print new file moving forward
    }
}
#定义MAXLEN 256
#定义宽度argv[2]//已修复
void wordWrap(int col,char*providedStr);0) {
字包装(num,buf);
}
printf(“\n”)//仅用于添加新行以打印前进的新文件
}
}
错误:
了解更多有关宝马的信息
328i可靠性
评级。
?&??? <代码>//您可以根据需要设置最大值1024很好 #定义MAXLEN 1024 char buf[MAXLEN]=“”; 对于(int i=1;i0){ 字包装(num,buf); } printf(“\n”);//仅用于添加新行以向前打印新文件 } 这很有效!
谢谢@Weather Vane

你意识到如果(EOF)总是正确的吗
EOF
-1
。当传递到
printf()
时,字符串没有终止符,因此它可能会打印垃圾。尝试用
char buf[MAXLEN]=“初始化它或带有
buf[字节]=0在read.bro之后,我是c新手,我只是把它作为一个例子来展示我试图实现的目标。但值得注意的是,您打开每个文件时没有关闭前一个文件。@Weather Vane这是我现在实际使用的代码。然后呢?&???正在末尾打印,然后其他选项
buf[bytes]=0
更好,因为字符串可能比前一个短。但是用
MAXLEN-1
调用
read
来腾出空间。@Weather Vane wait我很快就发布了答案,但是当我打印更多文件时,问题标记又回来了,buf[bytes]=0将是一个整数?否?好的,请阅读我之前的评论。请注意,
'a'
也是一个整数。MAXLEN-1不会打印文件中的整个字符串。我理解你的意思,它不允许我初始化int buf[bytes]=0;我的大脑也不同意这样做。请解释请仔细阅读:
buf[bytes]=0
// you can set max whatever you want for this purpose 1024 is good
#define MAXLEN 1024 
char buf[MAXLEN] = "";
for (int i = 1; i < argc; i++) {
    infd = open(argv[i], O_RDONLY);
    bytes = read(infd, buf, MAXLEN - 1);
    buf[bytes] = 0;

    if (bytes > 0) {
        wordWrap(num, buf);
    }
    printf("\n"); //just for adding new line to print new file moving forward
}