使用字符串获取stange错误

时间:2014-08-27 05:46:29

标签: c beagleboneblack

我正在用C编写一个程序,用于操作gpio引脚的beaglebone black。这是一个非常粗糙的程序,但如果你愿意,它只是一个“测试版”。只是为了让它运行起来。我的问题是我有两个字符数组。一个持有要传递给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;
}

1 个答案:

答案 0 :(得分:0)

您低估了要复制到数组中的字符串文字的长度,没有为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";

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