如何在 Arduino 的 SD 库中使用 char*?
How to use char* with SD library with Arduino?
我正在编写一个数据记录器,并希望将文件限制在特定数量的条目内。我正在尝试在设置中编写这段代码,这样当 Arduino 启动时,它会写入一个新文件以保持简单。但是,当我尝试打开文件时却打不开,尽管我不确定原因。任何人都可以提供任何解释吗?
char *fileName; //global name
File logFile; //global file
//everything else is in setup()
char * topPart = "/Data/Data"; //first part of every file
char * lowerPart = ".txt"; // jus the extention
char * itter; //used to hold the char of i later
fileName = "/Data/Data.txt"; //start with the first file possible.
for(int i=0; i<=100;i++) {
if(!SD.exists(fileName)) {
Serial.print("opening file: ");
Serial.print(fileName);
logFile = SD.open(fileName, FILE_WRITE);
if(logFile) {
logFile.println("I made it");
Serial.println("in the file");
}
if(!logFile) {
Serial.println("somthing bad");
}
break;
} else {
itter = (char *)(i+48);
strcpy(fileName,topPart);
strcpy(fileName,itter);
strcpy(fileName,lowerPart);
Serial.println(i);
}
}
很多问题。
itter
的构造是错误的。
strcpy
不只是附加 cpy。
这是一个代码示例,用于构建您的文件名。这是一个基本的 C 程序。去掉Arduino的#include和main,这样可以在电脑上测试程序是否正常。
#include <string.h>
#define TOPPART "/Data/Data"
#define LOWERPART ".txt"
int main(void) {
char buf[64];
snprintf(buf, sizeof(buf), "%s%s", TOPPART, LOWERPART);
for (int i = 0; i < 100; i++) {
/* here your stuff to check if the filename froml buf exists*/
snprintf(buf, sizeof(buf), "%s%d%s", TOPPART, i, LOWERPART);
}
return 0;
}
我正在编写一个数据记录器,并希望将文件限制在特定数量的条目内。我正在尝试在设置中编写这段代码,这样当 Arduino 启动时,它会写入一个新文件以保持简单。但是,当我尝试打开文件时却打不开,尽管我不确定原因。任何人都可以提供任何解释吗?
char *fileName; //global name
File logFile; //global file
//everything else is in setup()
char * topPart = "/Data/Data"; //first part of every file
char * lowerPart = ".txt"; // jus the extention
char * itter; //used to hold the char of i later
fileName = "/Data/Data.txt"; //start with the first file possible.
for(int i=0; i<=100;i++) {
if(!SD.exists(fileName)) {
Serial.print("opening file: ");
Serial.print(fileName);
logFile = SD.open(fileName, FILE_WRITE);
if(logFile) {
logFile.println("I made it");
Serial.println("in the file");
}
if(!logFile) {
Serial.println("somthing bad");
}
break;
} else {
itter = (char *)(i+48);
strcpy(fileName,topPart);
strcpy(fileName,itter);
strcpy(fileName,lowerPart);
Serial.println(i);
}
}
很多问题。
itter
的构造是错误的。strcpy
不只是附加 cpy。
这是一个代码示例,用于构建您的文件名。这是一个基本的 C 程序。去掉Arduino的#include和main,这样可以在电脑上测试程序是否正常。
#include <string.h>
#define TOPPART "/Data/Data"
#define LOWERPART ".txt"
int main(void) {
char buf[64];
snprintf(buf, sizeof(buf), "%s%s", TOPPART, LOWERPART);
for (int i = 0; i < 100; i++) {
/* here your stuff to check if the filename froml buf exists*/
snprintf(buf, sizeof(buf), "%s%d%s", TOPPART, i, LOWERPART);
}
return 0;
}