c - C 中涉及标签、语句和声明的奇怪错误,代码清理

标签 c sorting file-io declaration

我正在编写一个程序,它将读取给定的文本文件,显示一个菜单,其中包含使用 C 或 Perl 对文件(名称列表)进行排序的选项。并将排序后的列表输出到用户指定的新文件中。我在第 38 行和第 60 行的 FILE 标签后遇到了一个奇怪的错误。我把';'在它之后因为我读到一些东西说 Label 只能后跟一个语句,所以插入一个空白语句应该可以解决这个问题。在我放';'之前在我收到这些错误:

Struct.c: In function ‘main’:
Struct.c:38:17: error: a label can only be part of a statement and a declaration is not a statement
Struct.c:40:17: error: expected expression before ‘char’
Struct.c:44:30: error: ‘line’ undeclared (first use in this function)
Struct.c:44:30: note: each undeclared identifier is reported only once for each function it appears in
Struct.c:48:17: warning: passing argument 4 of ‘qsort’ from incompatible pointer type [enabled by default]
/usr/include/stdlib.h:761:13: note: expected ‘__compar_fn_t’ but argument is of type ‘int (*)(const char *, const char *)’
Struct.c:60:17: error: a label can only be part of a statement and a declaration is not a statement

在我输入';'之后在,我收到了这些错误:

Struct.c: In function ‘main’:
Struct.c:38:17: error: a label can only be part of a statement and a declaration is not a statement
Struct.c:48:17: warning: passing argument 4 of ‘qsort’ from incompatible pointer type [enabled by default]
/usr/include/stdlib.h:761:13: note: expected ‘__compar_fn_t’ but argument is of type ‘int (*)(const char *, const char *)’
Struct.c:60:17: error: a label can only be part of a statement and a declaration is not a statement

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

FILE *popen(const char *command, const char *mode);
int pclose(FILE *stream);

int main(void) {
    char strIn[128], *p;
    printf("Enter a source filename: ");
    fgets(strIn, sizeof strIn, stdin);
    p = strchr(strIn, '\n');
    if (p != NULL) *p = '\0';
    printf("You entered: %s \n", strIn);

    char strOut[128], *q;
    printf("Enter a destination filename: ");
    fgets(strOut, sizeof strOut, stdin);
    p = strchr(strOut, '\n');
    if (q != NULL) *q = '\0';
    printf("You entered: %s \n", strOut);  

    while (1){
        int sel;
        printf("MENU\n");
        printf("=====\n");
        printf("1. Sort Using C code\n");
        printf("2. Sort Using Perl\n");
        printf("3. Search for a Word Using Perl\n");
        printf("4. Exit\n");
        printf("=====\n");
        printf("Enter selection: ");
        sel = getchar();
        sel = sel-48;
        switch(sel){
            case 1:
                FILE * inFile, * outFile;
                 ;
                char line[25];
                char lines[60][25];
                int i, items = 0;
                inFile = fopen(strIn, "r");
                while (fgets(line, sizeof(line), inFile)){
                    strcpy(lines[items], line);
                    items++;
                }
                qsort(lines, items, 25, strcmp);

                outFile = fopen(strOut, "w");
                for (i=0; i<items; i++) {
                    fputs(lines[i], outFile);
                }

                fclose(inFile);
                fclose(outFile);
                break;

            case 2:
                FILE *cmd;

                snprintf(strOut, sizeof strOut, "perl -e 'print sort <>' %s", strIn);
                cmd = popen(strOut, "r");
                if (cmd == NULL) {
                    perror("popen");
                    exit(EXIT_FAILURE);
                }
                while (fgets(strOut, sizeof(strOut), cmd)) {
                    printf("%s", strOut);
                }
                pclose(cmd);

                break;
            case 3:
                //Find a Word and Return line Number using Perl
                break;
            case 4:
                exit(0);
            case -38:
                printf("Code is reading in LineFeed and displaying Menu again\n");
                break;
            default:
                printf("Error: Input was not a valid selection.\n");
                break;
        }
    }
return 0;
}

所有这些错误是怎么回事?我也不确定为什么 qsort 不起作用。

谢谢,

编辑:我将两个 FILE 标签直接移到 while 循环上方,这解决了标签语句问题,但程序仍然没有正常运行。说“你输入:”的两个 printfs 不打印出来并立即退出?在我输入目标文件并按回车键之后。

最佳答案

简短的回答是,您不能将变量声明为 switch 语句的 case 语句(本质上只是一个标签)内的第一行;

您可以在 case 语句中声明它们,它们不能直接跟在标签后面。

如果您尝试在程序中的任何位置放置标签,情况也是如此。尝试在 main() 的第一行添加“IM_A_LABEL:”。你会在那里得到同样的错误

编辑:

您在评论中提到,在输入第二个文件名后,您的程序在某个地方崩溃了。我构建了你的代码并通过 gdb 运行了它,而你却崩溃了

if (q != NULL) *q = '\0';

这是有道理的,因为你这样声明 q:

char strOut[128], *q;

q 只是一个指针,不指向任何实际空间。您需要为此指针分配空间。

除了检查之外,你不会对 q 做任何事情,所以我删除了它。之后,您的程序正确地通过了菜单循环。我没有尝试测试逻辑,因为我没有你的输入文件,但我选择了退出,我确实没有崩溃就退出了。

这是一个很好的例子,说明了为什么指针在声明时应该被初始化为 NULL。在尝试取消引用它之前检查 NULL,但这并不重要,因为声明中有一些垃圾、非 NULL 值。我注释掉那行是因为你不使用它,但你也可以很容易地把那行留在里面,并将你的声明更改为

char strIn[128], *q = NULL;

这将使您的正确检查真正起作用。让我知道这是否适合您。

关于c - C 中涉及标签、语句和声明的奇怪错误,代码清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26680226/

相关文章:

php - 尝试在 php 中对 mysql 表中的数据表进行排序

mysql - SQL:如果没有结束日期,则按开始日期排序

c - 如何在用户空间程序和 Linux 内核模块之间进行通信,以便打印从用户程序发送到内核的消息

c - 如何在 Netbeans IDE 中用 C 编程语言触发 EOF(文件结束符)

python - 计算丢失的比特币私钥的校验和比特币

c - recvfrom : Bad address, sendto: 协议(protocol)不支持的地址族

r - 按大小写对一系列列进行排序并将该排序顺序应用于另一列范围

python - 在python脚本中,如何将字符串的内容保存到文件中,并且该文件位于当前目录中?

c++ - ifstream::open() 到底做了什么?

javascript - fs.createRead/WriteStream() 与 fs.promises.open()