简体   繁体   English

调试时没有错误或警告

[英]debug with no errors or warnings

The following code compiles with no error or warnings, I can also execute the program and it will act as expected in that it will return the error messages at locations it is expected, for example, providing arguments to non-existent files. 以下代码可以编译,没有错误或警告,我也可以执行该程序,并且它将按预期方式运行,因为它将在预期的位置返回错误消息,例如,为不存在的文件提供参数。 This lets me know the code is working as far as line 28 (close of the !fpc section) 这让我知道代码可以运行到第28行(!fpc部分的关闭部分)

Meaning there must be an issue from the 意味着必须有一个问题

register int ch, i; 

Down to 向下

return (1);

before 之前

printf("\"%s\"\n",line);\

The program is expected to take command line arguments of the program name itself and two file names, it then opens both of these files, and should then copy strings from the first file up to a max length to the second file while adding " to both the start and end of the string in the new file. 该程序应采用程序名称本身和两个文件名的命令行参数,然后打开这两个文件,然后应将字符串从第一个文件复制到最大长度,再复制到第二个文件,同时在两个文件中都添加"新文件中字符串的开头和结尾。

The code I have is 我的代码是

fgetline.c 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 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];

I am compiling with 我正在编译

debian:~/uni/Ass0$ gcc fgetline.c -Wall -o enquote
debian:~/uni/Ass0$ cd /

testing I did was 我做的测试是

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

tim@debian:~/uni/Ass0$ cat test2
tim@debian:~/uni/Ass0$ ./enquote test test2
tim@debian:~/uni/Ass0$ cat test2

expected result would be when I run ./enquote test test2, would copy 预期的结果是当我运行./enquote test test2时,将复制

ting
test
123

from test to test2 so it would appear like testtest2所以它看起来像

"ting"
"test"
"123"

Thanks, not sure how much more info to give. 谢谢,不确定要提供多少信息。

There are many issues with your code, compiling with all warnings enabled would have spotted some of them: 您的代码有很多问题,在启用所有警告的情况下进行编译会发现其中的一些问题:

  • Declaring global variables in a header file is good practice, but not defining them there. 在头文件中声明全局变量是一种好习惯,但不要在其中定义它们。 The extern keyword is used for declarations. extern关键字用于声明。 The definitions belong in the C file. 这些定义属于C文件。 In this case, variables such as fp , fp1 , line should be defined as local variables, not global variables. 在这种情况下,应将诸如fpfp1line变量定义为局部变量,而不是全局变量。
  • Output file argv[2] should be open with "w" mode, "r+" is used for updated mode and will fail if the file does not exist. 输出文件argv[2]应该以"w"模式打开, "r+"用于更新模式,如果文件不存在,它将失败。 Update mode is very tricky and confusing, avoid using it. 更新模式非常棘手和混乱,请避免使用它。
  • Do not use the register keyword, it is obsolete now as compilers are smart enough to determine how to best use registers. 不要使用register关键字,因为编译器足够聪明地确定如何最好地使用寄存器,所以现在已经过时了。
  • Your while loops will read just 2 lines from the input file, storing the first into the line array and discarding the second one. while循环将从输入文件中仅读取2行,将第一line存储到line数组中并丢弃第二line
  • The return (i); return (i); statement exits the program, no output is performed, the remaining statements in the function are ignored completely (-Wall might have spotted this error). 语句退出程序,不执行任何输出,该函数中的其余语句被完全忽略(-Wall可能已发现此错误)。

You can simplify the problem by considering this: You want to output a " at the beginning of each line and before the '\\n' at the end of each line. You do not need to buffer the line in memory, which would impose a limit on line length. Just output the " whenever you start a line and before you end one: 您可以考虑以下问题来简化问题:您想在每行的开头和每行的'\\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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM