gpt4 book ai didi

c - 解析char数据时出现Segmentation fault (core dumped)

转载 作者:太空宇宙 更新时间:2023-11-04 06:02:29 25 4
gpt4 key购买 nike

我需要你的帮助。我有一个程序可以从端口读取 gps 和加速度计数据。程序是这样工作的:当我发送“a”时,程序将接收 gps 数据并发送到数据库,当我发送“b”时,程序将发送加速度计数据并保存到数据库。但是当我运行程序时,gps 数据成功接收并发送到数据库,但加速度计数据无法接收,错误是“段错误”。你能帮我解决这个问题吗..?

int main(void) {

int fd;
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open port ");
} else {
fcntl(fd, F_SETFL, 0);
}

char a[] = "a";
char b[] = "b";

int n,m,cnt;
char in[30];
//char in2[50];
//char *in;
//char in2[100];
//in = (char*) malloc(i+1);
//if (in == NULL) exit(1);


MYSQL *conn;

const char *localhost = "127.0.0.1";
const char *user = "root";
const char *password = "";
const char *database = "arduino1";

conn = mysql_init(NULL);

// Connect to database
if (!mysql_real_connect(conn, localhost, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}


for(cnt=0; cnt<5; cnt++)
{
if (cnt < 1) {
sleep(2);
n = write(fd, a, sizeof(a));
printf("Send : %s \n", a);

//terima data gps dari port
sleep(1);
n = read(fd, in, 100);
if (n < 0)
{
perror("read");
break;
}

//query gps
char c_lat[50],c_lon[50];
tokenizer(in,c_lat,c_lon);


//Isi nilai gps ke database

char query[255];

strcat(query,"INSERT INTO gps (latitude, longitude) VALUES (");
strcat(query,c_lat);
strcat(query,",");
strcat(query,c_lon);
strcat(query,")");

if (mysql_query(conn, query));
{
printf("%s\n", query);
}


} else {
m = write(fd, b, sizeof(b));
printf("Send : %s \n", b);

//terima data accelerometer dari port
sleep(1);
m = read(fd, in, 100);

in[m] = '\0';

char str[255];

//query accelerometer
char c_nilai_x[10],c_nilai_y[10],c_nilai_z[10],c_teg[6];
tokenizer_acm(in,c_nilai_x,c_nilai_y,c_nilai_z,c_teg);


//Isi nilai accelerometer ke database



strcat(str,"INSERT INTO highcharts_php (x_axis, y_axis, z_axis, tegangan) VALUES (");
strcat(str,c_nilai_x);
strcat(str,",");
strcat(str,c_nilai_y);
strcat(str,",");
strcat(str,c_nilai_z);
strcat(str,",");
strcat(str,c_teg);
strcat(str,")");

if (mysql_query(conn, str));
{
printf("%s\n", str);
}

}


// Close database connection
mysql_close(conn);

}

return 0;
}

感谢您的帮助。但我仍然收到同样的错误。我的改变是这样的:int main(void) {

int fd;
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open port ");
} else {
fcntl(fd, F_SETFL, 0);
}

char a[] = "a";
char b[] = "b";

int n,m,cnt;
char in[50];

MYSQL *conn;

const char *localhost = "127.0.0.1";
const char *user = "root";
const char *password = "";
const char *database = "arduino1";

conn = mysql_init(NULL);

// Connect to database
if (!mysql_real_connect(conn, localhost, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}


for(cnt=0; cnt<5; cnt++)
{
if (cnt < 1) {
sleep(2);
n = write(fd, a, sizeof(a));
printf("Send : %s \n", a);

//terima data gps dari port
sleep(1);
n = read(fd, in, 20);
if (n < 0)
{
perror("read");
break;
}

//query gps
char c_lat[10],c_lon[9];
tokenizer(in,c_lat,c_lon);


//Isi nilai gps ke database

char query[255] = "INSERT INTO gps (latitude, longitude) VALUES (";
strcat(query,c_lat);
strcat(query,",");
strcat(query,c_lon);
strcat(query,")");

if (mysql_query(conn, query));
{
printf("%s\n", query);
}


} else {
m = write(fd, b, sizeof(b));
printf("Send : %s \n", b);

//terima data accelerometer dari port
sleep(1);
m = read(fd, in, 30);

in[m] = '\0';



//query accelerometer
char c_nilai_x[8],c_nilai_y[8],c_nilai_z[8],c_teg[4];
tokenizer_acm(in,c_nilai_x,c_nilai_y,c_nilai_z,c_teg);


//Isi nilai accelerometer ke database


char str[255] = "INSERT INTO highcharts_php (x_axis, y_axis, z_axis, tegangan) VALUES (";
strcat(str,c_nilai_x);
strcat(str,",");
strcat(str,c_nilai_y);
strcat(str,",");
strcat(str,c_nilai_z);
strcat(str,",");
strcat(str,c_teg);
strcat(str,")");

if (mysql_query(conn, str));
{
printf("%s\n", str);
}

}


// Close database connection
mysql_close(conn);

}

return 0;

我运行程序的结果是:

发送:一个插入 gps(纬度、经度)值(-6.889760,107.619659)发送:b段错误(核心转储)

我需要帮助....谢谢

最佳答案

这是一个错误:

char query[255];
strcat(query,"INSERT INTO gps (latitude, longitude) VALUES (");

作为strcat()要求目标缓冲区在进入时为 null 终止,并且未初始化。这意味着 strcat() 将在 query 中搜索一个空字符,谁知道它会在哪里找到一个空字符,可能超出了 query 的范围这将导致 strcat() 写入不应该的内存。更改为:

char query[255] = "INSERT INTO gps (latitude, longitude) VALUES (";

代码后面的 str 也有同样的错误。

不要使用多个 strcat() 调用,而是使用更安全的 snprintf() :

char query[255];
int result = snprintf(query,
255,
"INSERT INTO gps (latitude, longitude) VALUES (%s,%s)",
c_lat,
c_lon);

/* See following paragraph for explanation of this condition. */
if (result > 0 && result < 255)
{
}

snprintf() 返回值(来自 C99 标准的 7.19.6.5 The snprintf function 部分):

The snprintf function returns the number of characters that would have been written had n been sufficiently large, not counting the terminating null character, or a negative value if an encoding error occurred. Thus, the null- terminated output has been completely written if and only if the returned value is nonnegative and less than n.

关于c - 解析char数据时出现Segmentation fault (core dumped),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16666865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com