gpt4 book ai didi

c - 字符串出现异常错误

转载 作者:行者123 更新时间:2023-11-30 19:45:52 24 4
gpt4 key购买 nike

我正在用 C 语言为我的 beaglebone black 编写一个程序来操作 GPIO 引脚。这是一个非常粗糙的程序,但如果你愿意的话,它只是一个“测试版”。只是为了让它启动并运行。我的问题是我有两个字符数组。一个保存要传递给 system() 函数的命令,另一个保存我要编辑的文件的路径,它会转到 fopen 函数。这些字符数组被操纵以根据从调用函数传递给它们的内容来更改两个数字。由于某种原因,文件名字符数组与命令连接在一起。我正在浏览该程序,但没有发现任何明显的错误。

这是我的代码

/*
* gpio.c
*
* Created on: Aug 26, 2014
* Author: Christian Macias
*
* Description: This will control your GPIO (General Purpose IO) pins on the beagle bone. Please
* ensure you are on a kernel that supports device trees.
*
* Usage: gpio(PIN, Value "1" or "0", Inverted? "0" false or "1" for true)
*
* The return value is 0 for success and -1 for failure
*
* GO UTEP!!
*/

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

int gpio(int pin, int value, int inv)
{
int sucfail=0;
int pinInt1, pinInt2=-1;
int counterOne=NULL;
char filename[28]= "/sys/class/gpio/gpio00/value";//27 characters


/*
* Checks if file is within the gpio pin range.
*/

if(pin<0 || pin>=99)
{
fprintf(stderr, "\n%s\n\t%s\n\t%s\n", "Error in gpio.c(GPIO): ", "The gpio pin selected is not "
"within the availabilty of the app", "Please select a pin from 0-99");
return -1;

}

/*
* checks to see if value is boolean aka 1 or 0
*/
if(value<0 || value>1)
{
fprintf(stderr, "\n%s\n\t%s\n\t%s", "Error in gpio.c(GPIO): ", "The Selected value is invalid", "Please"
" select a 1 or a 0");
return -1;
}

/*
* Writes the pin to a file so that it can used later
*/

FILE *PINWRITE;
PINWRITE=fopen("pinWRITE", "w+");
fprintf(PINWRITE,"%i", pin);
fclose(PINWRITE);

/*
* This section will check for pre-existence of the the PIN file to prevent errors. or
* opens it if it doesnt exist.
* First it will set up the filenames
*/

PINWRITE=fopen("pinWRITE","r");

fscanf(PINWRITE, "%1i%1i", &pinInt1, &pinInt2);//Checks the pin and sets each digit to its according variable
fclose(PINWRITE);
filename[20]='0'+pinInt1;
if(pinInt2==-1)//If it is a one digit pin, it will move the letters to fit the file name correctly and remove one of the digits
{
for(counterOne=21;counterOne<28;counterOne++)
{
filename[counterOne]=filename[counterOne+1];
}
filename[27]='\0';
}
else//If two digits it will just change the second digit
{
filename[21]='0'+pinInt2;
}

FILE *PINVALUE;//FILE pointer to the files with the value
PINVALUE=fopen(filename,"w+");

/*
* At this point the the actual checking and creation occurs
*/
char exportCommand[32]="echo 00 > /sys/class/gpio/export";//31 characters
if(PINVALUE==NULL)
{
//this runs if the file didnt exist.
exportCommand[5]='0'+pinInt1;
if(pinInt2==-1)//If it is a one digit pin, it will move the letters to fit the file name correctly and remove one of the digits
{
for(counterOne=6;counterOne<32;counterOne++)
{
exportCommand[counterOne]=exportCommand[counterOne+1];
}
exportCommand[31]='\0';
}
else//If two digits it will just change the second digit
{
exportCommand[6]='0'+pinInt2;
exportCommand[32]='\0';
}

system(exportCommand);
printf("\n%s\n", exportCommand);
printf("\n%s\n", filename);
PINVALUE=fopen(filename,"w+");
}

if(PINVALUE==NULL)
{
fprintf(stderr,"\n%s\n\t%s", "Error in gpio.c(GPIO)", "The PINVALUE (.../gpioXX/value) could not be opened");
return -1;
}

/*
* Some pins may be set up backward... on is off and off is on. To correct this we must adjust a file...
* This takes to long and i dont have the time for it so i'm doing a hot fix... sorry but its 11 and i have
* school tomorrow. The correction process it too long and i want to finish today :)
*/

if(inv==1 && value==1)
{
value=0;
}
else if(inv==1 && value==0)
{
value=1;
}

/*
* At this point the file is set up and ready to be written to.
* We will write the value now and close it.
*/

fprintf(PINVALUE, "%i", value);
fclose(PINVALUE);

return sucfail;
}

最佳答案

您低估了要复制到数组中的字符串文字的长度,没有为 nul 终止符留下空间。例如这里

char filename[28]= "/sys/class/gpio/gpio00/value"; //27 characters

文字实际上有 28+1 个字符(28 个可见字符,加上一个 nul 终止符)。

您需要创建大小为 29 的数组。您可以明确地执行此操作,

char filename[29] = "/sys/class/gpio/gpio00/value";

或隐式:

char filename[] = "/sys/class/gpio/gpio00/value";

与此类似,您需要数组的长度为 33:

char exportCommand[32]="echo 00 > /sys/class/gpio/export";

很可能还有其他错误。我首先要解决这些问题。这将使找到其他人变得更容易。

关于c - 字符串出现异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25519757/

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