实施“猫”;在c中-wwwmc? (我的代码有什么问题)
我的代码在某种程度上运行良好。不过我有两个问题。一,在将文件打印到标准输出结束时,它给了我一个分段错误。
第二,我无法使用 fputs 打印数据,因为我立即遇到分段错误。因此,为了修复它,我使用了 put ,它可以很好地打印它,但在每行后面添加一个 '\n' ,使文本单行间隔以及末尾的段错误。
#include <stdio.h>
#include <string.h>
void concat(char *arg){
char string[256];
FILE *fp = fopen(arg, "r");
while(!feof(fp)){
fgets(string, 256, fp);
//fputs(string, fp);
puts(string);
}
fclose(fp);
}
void stdincat(){
char string[256];
while(!feof(stdin)){
fgets(string, 256, stdin);
fputs(string, stdout);
}
}
int main(int argc, char *argv[]){
char argvalues[256][40];
if(argv[1] == NULL)
stdincat();
else if(argv[1] != NULL){
int i;
for(i=1;i<=(argc);i++){
concat(argv[i]);
}
}
return 0;
}
My code works fine in a way. I have two issues with it though. One, at the end of printing the files to the standard output, it gives me a segmentation fault.
Two, I can't use fputs to print out the data because I get a segmentation fault right away. So to fix it I use puts which prints it fine, but adds a '\n' after every line making the text single line spaced as well as the seg fault at the end.
#include <stdio.h>
#include <string.h>
void concat(char *arg){
char string[256];
FILE *fp = fopen(arg, "r");
while(!feof(fp)){
fgets(string, 256, fp);
//fputs(string, fp);
puts(string);
}
fclose(fp);
}
void stdincat(){
char string[256];
while(!feof(stdin)){
fgets(string, 256, stdin);
fputs(string, stdout);
}
}
int main(int argc, char *argv[]){
char argvalues[256][40];
if(argv[1] == NULL)
stdincat();
else if(argv[1] != NULL){
int i;
for(i=1;i<=(argc);i++){
concat(argv[i]);
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在
concat
中注释掉的对fputs
的调用正在尝试写入fp
,您打开它只是为了阅读,所以毫不奇怪它不会/不起作用。除此之外,您的阅读循环:
while(!feof(fp)) {
(以及类似的,除了来自stdin
而不是fp
)遵循熟悉的、广泛存在的反模式——像这样的循环不能/不会/不能正常工作。您通常希望在同一操作中阅读并测试是否成功:编辑:我还应该提到,我希望避免在
concat
和stdincat
中重复代码。我宁愿传递FILE *
作为参数进行读取,因此您可以使用相同的代码从stdin
或其他任意文件读取:最后,我需要注意的是,如果您只是要复制整个文件,
fgets
可能不是完成这项工作的最有效方法。fread
可能更合适。当您这样做时,您也可以使用二进制模式读取和写入。The call to
fputs
you have commented out inconcat
is attempting to write tofp
, which you opened only for reading, so it's no surprise that it won't/doesn't work.Other than that, your reading loops:
while(!feof(fp)) {
(and similar except fromstdin
instead offp
) follow a familiar, widespread anti-pattern -- loops like this don't/won't/can't work correctly. You normally want to read and test for success in the same operation:Edit: I should also mention that I would prefer to avoid the duplication of code in
concat
andstdincat
. I'd rather pass theFILE *
to read from as the parameter, so you'd use the same code to read fromstdin
or from other arbitrary files:Finally, I'd note that if you're just going to copy an entire file,
fgets
probably is not the most efficient way to do the job.fread
may be more suitable. While you're at it, you might as well use binary mode reading and writing as well.您不能在仅用于读取而打开的流上写入,您应该使用 stdout 作为函数 concat 中的 fput
You cannot write on the stream that you opened only for read, you should use stdout for the fputs in the function concat