下面的代码编译没有错误或警告,我也可以执行程序,它会按预期行事,它会在预期的位置返回错误消息,例如提供参数到不存在的文件。这让我知道代码工作尽可能线28(关闭!FPC段)调试没有错误或警告
含义必须有从
register int ch, i;
一个问题之前,向下
return (1);
printf("\"%s\"\n",line);\
该程序预计将采取程序名称本身的命令行参数和两个文件名s,然后打开这两个文件,然后应该将字符串从第一个文件复制到第二个文件的最大长度,同时将"
添加到新文件中字符串的开头和结尾。
我的代码是
fgetline.c
#include "fgetline.h"
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("usage: enquote filetocopy filetowrite \n");
exit(1);
}
fp = fopen(argv[1], "r");
if (!fp) {
printf("Couldn't open copy file: (%d) %s\n", errno, strerror(errno));
return -1;
}
fpc = fopen(argv[2], "r+");
if (!fpc) {
printf("Couldn't open write file: (%d) %s\n", errno, strerror(errno));
return -1;
}
register int ch, i;
ch = getc(fp);
if (ch == EOF)
return -1;
i = 0;
while (ch != '\n' && ch != EOF && i < max) {
line[i++] = ch;
ch = getc(fp);
}
line[i] = '\0';
while (ch != '\n' && ch != EOF) {
ch = getc(fp);
i++;
}
return(i);
printf("\"%s\"\n",line);
fclose(fp);
fclose(fpc);
return 0;
}
fgetline.h
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int fgetline(FILE *fp, char *line, int max);
FILE *fp, *fpc;
#define max 30
char line[max + 1];
我与
debian:~/uni/Ass0$ gcc fgetline.c -Wall -o enquote
debian:~/uni/Ass0$ cd/
编译
测试我所做的就是
debian:~/uni/Ass0$ ./enquote
usage: enquote filetocopy filetowrite
debian:~/uni/Ass0$ ./enquote test
usage: enquote filetocopy filetowrite
debian:~/uni/Ass0$ ./enquote test frog
Couldn't open write file: (2) No such file or directory
debian:~/uni/Ass0$ ./enquote monkey frog
Couldn't open copy file: (2) No such file or directory
debian:~/uni/Ass0$ cat test
ting
test
123
[email protected]:~/uni/Ass0$ cat test2
[email protected]:~/uni/Ass0$ ./enquote test test2
[email protected]:~/uni/Ass0$ cat test2
预期的结果将是,当我运行./enquote测试TEST2,会从test
到test2
复制
ting
test
123
所以会出现像
"ting"
"test"
"123"
谢谢,不知道要给多少信息。
1)启用**全部**警告! '-Wall'远离“全部”2)没有错误/警告不能保证正确的代码。 3)重新格式化你的代码。 GNU风格非常80年代。 – Olaf
@Olaf,所有警告的标记是什么,快速查找出来了--Wextra还有什么,可以用Wextra试过,还是没有运气。 – Ausghostdog
@Ausghostdog我当前设置的是'gcc -pedantic -Wall -Wextra -Wbad-function-cast -Wcast-align -Wdisabled-optimization -Wendif-labels -Winline -Wmissing-prototypes -Wanested-externs -Wshadow -Wstrict-prototypes- Wundef -Wwrite-strings -Wformat = 2 -Wullull-dereference -Winit-self -Whift-negative-value -Wshift-overflow = 2 -Wduplicated-cond -O2'。 – melpomene