我正在try 编译一个新的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;
}