Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Raspberry Pi-C程序未写入文本文件_C_Linux_Io_Raspbian - Fatal编程技术网

Raspberry Pi-C程序未写入文本文件

Raspberry Pi-C程序未写入文本文件,c,linux,io,raspbian,C,Linux,Io,Raspbian,我是linux/raspbian编程新手,我正在尝试制作一个程序,每当引脚23变高时,提取Pi系统时间并将其写入文本文件。插脚23连接到S-R闩锁,插脚24发出重置信号以重置闩锁 我遇到的问题是,它似乎没有向创建的文本文件写入任何内容。程序可以创建文件,但不向其中写入任何内容。这是我的密码: using namespace std; FILE *f; struct timeval curTime; int main(int argc, char *argv[]){ char dateina

我是linux/raspbian编程新手,我正在尝试制作一个程序,每当引脚23变高时,提取Pi系统时间并将其写入文本文件。插脚23连接到S-R闩锁,插脚24发出重置信号以重置闩锁

我遇到的问题是,它似乎没有向创建的文本文件写入任何内容。程序可以创建文件,但不向其中写入任何内容。这是我的密码:

using namespace std; 
FILE *f; 
struct timeval curTime;

int main(int argc, char *argv[]){
char dateiname[256] = "";
int i=0;
int milli;
int seconds_in_day;

wiringPiSetupGpio();

time_t t = time(0);
struct tm * now = localtime(&t);

//Create and open file
sprintf(dateiname, "/home/raspbian/Desktop/%02d%02d%02d_%02d_%02d.txt", 
                       now -> tm_year+1900, 
                       now -> tm_mon+1, 
                       now -> tm_mday, 
                       now -> tm_hour, 
                       now -> tm_min); 

f = fopen(dateiname, "w");
//write heading to file before loop
fprintf(f, "Picture, system time\n");

//Set 23 & 24 as input/ output
pinMode(23, INPUT);
pullUpDnControl(23, PUD_DOWN);
pinMode(24, OUTPUT);

while(1){ 
if(digitalRead(23)){ //If 23 is high
   i=i+1;
   gettimeofday(&curTime, NULL);
   milli = curTime.tv_usec / 1000; //Get time in milliseconds
   seconds_in_day = curTime.tv_sec % 86400; //Get seconds since midnight
   fprintf(f, "&d &d.%d\n", i, seconds_in_day, milli); //Write to file

   //send out reset signal
   digitalWrite(24, HIGH); 
   //pause for 1 second
   delay(1000);
}
}
fclose(f);
return(0);
}
有人在这里看到任何明显的错误吗?我还在终端上通过

sudo /home/raspbian/Desktop/program 

然后退出终端窗口退出程序。谢谢

它可能正在缓冲输出。在执行
fclose
之前,缓冲区不一定会被写入,这是永远不会发生的


如果您想在引脚23高时每秒用一行更新一次文件,请将文件
fopen
fclose
放入循环中。如果希望每秒添加一行,则添加
fflush(f)
fprintf
之后,它可能正在缓冲输出。在执行
fclose
之前,缓冲区不一定会被写入,这是永远不会发生的


如果您想在引脚23高时每秒用一行更新一次文件,请将文件
fopen
fclose
放入循环中。如果希望每秒添加一行,则添加
fflush(f)
fprintf

之后,我怀疑针脚24上的输出需要是一个“高”的“短”周期,以使锁存器复位,然后返回到“低”,为下一次需要复位锁存器做准备

像这样的行:
while(!digitalRead(23))
将消耗大量CPU周期,因此可能需要在每个循环的主体中加入一些“delay()”或
yield()

using namespace std;
FILE *f;
struct timeval curTime;

int main(int argc, char *argv[]){
    char dateiname[256] = "";
    int i=0;
    int milli;
    int seconds_in_day;

    wiringPiSetupGpio();

    time_t t = time(0);
    struct tm * now = localtime(&t);

    //Create and open file
    sprintf(dateiname, "/home/raspbian/Desktop/%02d%02d%02d_%02d_%02d.txt",
                           now -> tm_year+1900,
                           now -> tm_mon+1,
                           now -> tm_mday,
                           now -> tm_hour,
                           now -> tm_min);

    if( NULL == (f = fopen(dateiname, "w") )
    { // then fopen failed
        perror( "fopen failed for output file");
        exit(EXIT_FAILURE);
    }

    // implied else, fopen successful

    //write heading to file before loop
    fprintf(f, "Picture, system time\n");
    fflush( f );

    //Set 23 & 24 as input/ output
    pinMode(23, INPUT);
    pullUpDnControl(23, PUD_DOWN);
    pinMode(24, OUTPUT);

    // assure latch is reset
    digitalWrite(24, LOW);
    digitalWrite(24, HIGH);
    digitalWrite(24, LOW);

    while(1)
    {
        // wait while pin23 is low
        while( !digitalRead(23));

        // 23 is high
        i=i+1;
        gettimeofday(&curTime, NULL);
        milli = curTime.tv_usec / 1000; //Get time in milliseconds
        seconds_in_day = curTime.tv_sec % 86400; //Get seconds since midnight
        fprintf(f, "&d &d.%d\n", i, seconds_in_day, milli); //Write to file
        fflush( f );

        // if a latch 'set' signal is received during the following
        // three instructions, then could get locked into 
        // the while pin23 high loop

        // reset latch
        digitalWrite(24, HIGH);
        digitalWrite(24, LOW);

        // wait for pin 23 to be low
        while( digitalRead(23) );
    } // end while

    fclose(f);
    return(0);
}

我怀疑引脚24上的输出需要是一个“高”的“短”周期,以使闩锁复位,然后返回到“低”,为下次闩锁需要复位做准备

像这样的行:
while(!digitalRead(23))
将消耗大量CPU周期,因此可能需要在每个循环的主体中加入一些“delay()”或
yield()

using namespace std;
FILE *f;
struct timeval curTime;

int main(int argc, char *argv[]){
    char dateiname[256] = "";
    int i=0;
    int milli;
    int seconds_in_day;

    wiringPiSetupGpio();

    time_t t = time(0);
    struct tm * now = localtime(&t);

    //Create and open file
    sprintf(dateiname, "/home/raspbian/Desktop/%02d%02d%02d_%02d_%02d.txt",
                           now -> tm_year+1900,
                           now -> tm_mon+1,
                           now -> tm_mday,
                           now -> tm_hour,
                           now -> tm_min);

    if( NULL == (f = fopen(dateiname, "w") )
    { // then fopen failed
        perror( "fopen failed for output file");
        exit(EXIT_FAILURE);
    }

    // implied else, fopen successful

    //write heading to file before loop
    fprintf(f, "Picture, system time\n");
    fflush( f );

    //Set 23 & 24 as input/ output
    pinMode(23, INPUT);
    pullUpDnControl(23, PUD_DOWN);
    pinMode(24, OUTPUT);

    // assure latch is reset
    digitalWrite(24, LOW);
    digitalWrite(24, HIGH);
    digitalWrite(24, LOW);

    while(1)
    {
        // wait while pin23 is low
        while( !digitalRead(23));

        // 23 is high
        i=i+1;
        gettimeofday(&curTime, NULL);
        milli = curTime.tv_usec / 1000; //Get time in milliseconds
        seconds_in_day = curTime.tv_sec % 86400; //Get seconds since midnight
        fprintf(f, "&d &d.%d\n", i, seconds_in_day, milli); //Write to file
        fflush( f );

        // if a latch 'set' signal is received during the following
        // three instructions, then could get locked into 
        // the while pin23 high loop

        // reset latch
        digitalWrite(24, HIGH);
        digitalWrite(24, LOW);

        // wait for pin 23 to be low
        while( digitalRead(23) );
    } // end while

    fclose(f);
    return(0);
}

如果在(1)
时删除
,会发生什么情况?这可能会有所帮助。根据您写入文件的方式,shell的终止可能会导致
fclose
未被调用。通过检查是否返回空指针(失败时)来检查fopen是否成功打开文件。@ChristophSommer删除
while(1)
循环导致系统时间被写入一次,但我写入头的第一个fprintf语句不起作用。谢谢@Mathematicsian1975,我会检查它,如果在(1)
时删除
,会发生什么?也许这会有所帮助。根据您写入文件的方式,shell的终止可能会导致
fclose
未被调用。通过检查是否返回空指针(失败时)来检查fopen是否成功打开文件。@ChristophSommer删除
while(1)
循环导致系统时间被写入一次,但我写入头的第一个fprintf语句不起作用。感谢@Mathematicsian1975,我会检查它。每次
fprintf
之后,我都会输入
fflush(f)
,头会写入文本文件。我仍然有程序的问题没有写系统时间,但你已经解决了原来的问题。Thanks@oodan123首先,格式说明符是错误的,
“&d&d.%d\n”
->%d.%d\n”我在每次
fprintf
之后放入
fflush(f)
,并且头确实写入文本文件。我仍然有程序的问题没有写系统时间,但你已经解决了原来的问题。Thanks@oodan123首先,格式说明符是错误的
“&d&d.%d\n”
->%d.%d\n