Raspberry Pi - C 程序不写入文本文件
Raspberry Pi - C program not writing to text file
我是使用 linux/raspbian 编程的新手,我正在尝试制作一个程序来提取 Pi 系统时间,并在引脚 23 变高时将其写入文本文件。引脚 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
并通过退出终端 window 来退出程序。谢谢
它可能正在缓冲输出。在 fclose
执行之前,缓冲区不一定会被写入,而这永远不会。
如果您希望文件在 23 号针为高电平时每秒更新一次单行,请将文件 fopen
和 fclose
放入循环中。如果你想每秒添加一行,那么在fprintf
之后添加fflush(f);
。
我怀疑引脚 24 上的输出需要 'short' 周期 'high' 才能使锁存器复位,然后是 return 到 'low'为下次需要重置锁存器做准备。
像这样的行: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);
}
我是使用 linux/raspbian 编程的新手,我正在尝试制作一个程序来提取 Pi 系统时间,并在引脚 23 变高时将其写入文本文件。引脚 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
并通过退出终端 window 来退出程序。谢谢
它可能正在缓冲输出。在 fclose
执行之前,缓冲区不一定会被写入,而这永远不会。
如果您希望文件在 23 号针为高电平时每秒更新一次单行,请将文件 fopen
和 fclose
放入循环中。如果你想每秒添加一行,那么在fprintf
之后添加fflush(f);
。
我怀疑引脚 24 上的输出需要 'short' 周期 'high' 才能使锁存器复位,然后是 return 到 'low'为下次需要重置锁存器做准备。
像这样的行: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);
}