我编译了一个新的c程序并收到以下错误

问题描述 投票:0回答:1

我正在尝试编译一个新的c程序并收到以下错误,并且不知道如何处理它们。

[{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "comparison with string literal results in unspecified behavior [-Waddress]",
    "source": "gcc",
    "startLineNumber": 49,
    "startColumn": 26,
    "endLineNumber": 49,
    "endColumn": 26
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "pointer of type 'void *' used in arithmetic [-Wpointer-arith]",
    "source": "gcc",
    "startLineNumber": 52,
    "startColumn": 37,
    "endLineNumber": 52,
    "endColumn": 37
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "comparison with string literal results in unspecified behavior [-Waddress]",
    "source": "gcc",
    "startLineNumber": 53,
    "startColumn": 33,
    "endLineNumber": 53,
    "endColumn": 33
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "pointer of type 'void *' used in arithmetic [-Wpointer-arith]",
    "source": "gcc",
    "startLineNumber": 56,
    "startColumn": 37,
    "endLineNumber": 56,
    "endColumn": 37
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "comparison with string literal results in unspecified behavior [-Waddress]",
    "source": "gcc",
    "startLineNumber": 57,
    "startColumn": 33,
    "endLineNumber": 57,
    "endColumn": 33
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "pointer of type 'void *' used in arithmetic [-Wpointer-arith]",
    "source": "gcc",
    "startLineNumber": 60,
    "startColumn": 37,
    "endLineNumber": 60,
    "endColumn": 37
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "comparison with string literal results in unspecified behavior [-Waddress]",
    "source": "gcc",
    "startLineNumber": 61,
    "startColumn": 33,
    "endLineNumber": 61,
    "endColumn": 33
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "pointer of type 'void *' used in arithmetic [-Wpointer-arith]",
    "source": "gcc",
    "startLineNumber": 64,
    "startColumn": 37,
    "endLineNumber": 64,
    "endColumn": 37
}]

我不是 C 程序员,我可以处理现有程序的调试,但创建新程序是一种学习体验。

任何帮助都会有帮助,我们将不胜感激。

我真的不知道我需要改变什么。

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

// Define the structure for table1
typedef struct {
    char customer_code[31];
    char firstname[101];
    char lastname[101];
} table1_t;

// Define the structure for table2
typedef struct {
    char customer_code[31];
    char invoice_code[31];
    float amount;
    char date[11]; // Assuming DATE is in 'YYYY-MM-DD' format
} table2_t;

// Define the structure for table3
typedef struct {
    char invoice_code[31];
    char item_code[31];
    float amount;
    int quantity;
} table3_t;

// Define the structure for table4
typedef struct {
    char customer_code[31];
} table4_t;

int rows;

// Function to read CSV file and populate the table name passed
int parse_csv(const char *filename, void *table, const char *structure) {
    FILE *fp = fopen(filename, "r");
    if (!fp) {
        perror("Error opening file");
        return -1;
    }

    // Skip the first record
    char line[4096];
    fgets(line, sizeof(line), fp);

    int count = 0;
    while (fgets(line, sizeof(line), fp)) {
        if (structure == "table1_t") {
            table1_t *t1 = (table1_t *)table;
            sscanf(line, "%[^,],%[^,],%s", t1->customer_code, t1->firstname, t1->lastname);
            table += sizeof(table1_t);
        } else if (structure == "table2_t") {
            table2_t *t2 = (table2_t *)table;
            sscanf(line, "%[^,],%[^,],%f,%s", t2->customer_code, t2->invoice_code, &t2->amount, t2->date);
            table += sizeof(table2_t);
        } else if (structure == "table3_t") {
            table3_t *t3 = (table3_t *)table;
            sscanf(line, "%[^,],%[^,],%f,%d", t3->invoice_code, t3->item_code, &t3->amount, &t3->quantity);
            table += sizeof(table3_t);
        } else if (structure == "table4_t") {
            table4_t *t4 = (table4_t *)table;
            sscanf(line, "%[^,]", t4->customer_code);
            table += sizeof(table4_t);
        } else {
            fprintf(stderr, "Invalid table structure\n");
            fclose(fp);
            return -1;
        }
        count++;
    }

    fclose(fp);
    return count;
}

int main() {
    // Process table1
    table1_t table1[500000];
    rows = parse_csv("table1.csv", table1, "table1_t");
    printf("Number of rows in table1: %d\n", rows);

    //Process table2
    table2_t table2[1000000];
    rows = parse_csv("table2.csv", table2, "table2_t");
    printf("Number of rows in table2: %d\n", rows);

    //Process table3
    table3_t table3[5000000];
    rows = parse_csv("table3.csv", table3, "table3_t");
    printf("Number of rows in table3: %d\n", rows);   

    //Process table4
    table4_t table4[1000];
    rows = parse_csv("table4.csv", table4, "table4_t");
    printf("Number of rows in table4: %d\n", rows);   

    return 0;
}
c csv
1个回答
0
投票

您有两个问题:

首先,通过

structure == "table1_t"
(等等)与字符串文字进行比较。请改用
strcmp(structure, "table1_t")
。对于您之前直接与字符串文字进行比较的其他实例,请执行此操作。

第二,你有你的

void *table
参数,当你做类似的事情时

table += sizeof(table1_t);

那么你实际上是在说你想将

table
的指针在内存中进一步移动你所增加的幅度。在这里查看更多相关信息:警告:算术中使用了“void *”类型的指针[-Wpointer-arith]|

© www.soinside.com 2019 - 2024. All rights reserved.