下面的代碼編譯沒有錯誤或警告,我也可以執行程序,它會按預期行事,它會在預期的位置返回錯誤消息,例如提供參數到不存在的文件。這讓我知道代碼工作儘可能線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