i have to make a program which can accept input like this:
2012-01-20 10:50:52 127.0.0.1(via UDP: [127.0.0.1]:65008->[0.0.0.0]:0) TRAP, SNMP v1, community telros
SNMPv2-SMI::enterprises.3.1.1 Enterprise Specific Trap (1) Uptime: 73 days, 21:16:00.11
2012-01-20 10:50:53 127.0.0.1(via UDP: [127.0.0.1]:57487->[0.0.0.0]:0) TRAP, SNMP v1, community telros
SNMPv2-SMI::enterprises.3.1.1 Enterprise Specific Trap (1) Uptime: 73 days, 21:16:01.15
2012-01-20 10:50:54 127.0.0.1(via UDP: [127.0.0.1]:34207->[0.0.0.0]:0) TRAP, SNMP v1, community telros
SNMPv2-SMI::enterprises.3.1.1 Enterprise Specific Trap (1) Uptime: 73 days, 21:16:01.92
and then put it into Postgres: get two lines devided by empty (\n) line and put it into db table.
what i’m trying to do is:
const char *conninfo;
const char *paramValues[1];
char *str;
char *sql;
...
while(fgets(str, 126, stdin)) {
if (0 != strcmp(str,"\n")){
strcat(sql,str);
}else{
paramValues[0] = (const char *)sql;
res = PQexecParams(conn,
"insert into traps (trap_content, trap_timestamp) values ($1, localtimestamp);",
1,
NULL,
paramValues,
NULL,
NULL,
1);
sql = "";
}
}
but paramValues[0] seems to be empty on insert and empty line appears in the table.
how can i get two strings before the empty one in paramValues[0]?
i’m not good in C, so sorry if it’s silly question =)
This line:
will cause the char* variable ‘sql’ to point at the address of the string literal “”. It’s incorrect to then call strcat(sql,str) with sql now pointing to the address of this literal; that could even segfault. Try instead:
or equivalently
Also, you don’t show where ‘sql’ is defined, but it has to point to a buffer big enough to hold as many adjacent lines as you expect to encounter.