2016-07-23 91 views
-1

下面的代码编译没有错误或警告,我也可以执行程序,它会按预期行事,它会在预期的位置返回错误消息,例如提供参数到不存在的文件。这让我知道代码工作尽可能线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,会从testtest2复制

ting 
test 
123 

所以会出现像

"ting" 
"test" 
"123" 

谢谢,不知道要给多少信息。

+3

1)启用**全部**警告! '-Wall'远离“全部”2)没有错误/警告不能保证正确的代码。 3)重新格式化你的代码。 GNU风格非常80年代。 – Olaf

+0

@Olaf,所有警告的标记是什么,快速查找出来了--Wextra还有什么,可以用Wextra试过,还是没有运气。 – Ausghostdog

+0

@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

回答

3

有许多问题与您的代码,启用会发现他们中的一些所有的警告编译:

  • 声明全局变量在头文件中是很好的做法,但不能确定他们那里。关键字extern用于声明。这些定义属于C文件。在这种情况下,应将诸如fp,fp1,line之类的变量定义为局部变量,而不是全局变量。
  • 输出文件argv[2]应该使用"w"模式打开,"r+"用于更新模式,如果文件不存在将会失败。更新模式非常棘手和混乱,请避免使用它。
  • 不要使用register关键字,现在它已经过时了,因为编译器足够聪明,可以确定如何最好地使用寄存器。
  • 您的while循环将从输入文件中只读取2行,将第一行存入line阵列并丢弃第二行。
  • return (i);语句退出程序,没有执行输出,函数中的其余语句完全被忽略(-Wall可能已经发现了这个错误)。

您可以通过考虑这个简化的问题:您在每一行的开头和'\n'在每行年底前要输出"。您不需要在内存中缓冲行,这会对行长度施加限制。只要输出",只要你开始一条线并在结束之前:

#include <errno.h> 
#include <stdio.h> 
#include <string.h> 

int main(int argc, char *argv[]) { 
    FILE *fp, *fpc; 
    int ch, last; 

    if (argc != 3) { 
     printf("usage: enquote filetocopy filetowrite\n"); 
     exit(1); 
    } 

    fp = fopen(argv[1], "r"); 
    if (!fp) { 
     fprintf(stderr, "Could not open input file: (%d) %s\n", 
       errno, strerror(errno)); 
     return 2; 
    } 

    fpc = fopen(argv[2], "w"); 
    if (!fpc) { 
     fprintf(stderr, "Could not open output file: (%d) %s\n", 
       errno, strerror(errno)); 
     return 2; 
    } 

    last = '\n'; // we are at the beginning of a line 
    while ((ch = fgetc(fp)) != EOF) { 
     if (last == '\n') { 
      fputc('"', fpc); // " at the beginning of a line 
     } 
     if (ch == '\n') { 
      fputc('"', fpc); // " at the end of a line 
     } 
     fputc(ch, fpc); 
     last = ch; 
    } 
    if (last != '\n') { 
     // special case: file does not end with a \n 
     fputc('"', fpc); // " at the end of a line 
     fputc('\n', fpc); // put a \n at the end of the output file 
    } 

    fclose(fp); 
    fclose(fpc); 
    return 0; 
} 
+0

非常感谢你,也为了解释。似乎我还有很长的路要走C. – Ausghostdog

+1

@Aghghostdog:熟练掌握C需要很多工作,但是掌握的技能对其他各种更宽容的语言都很有用。继续学习,这是值得的! – chqrlie

相关问题