suoma 发表于 2016-9-14 14:40:53

关于sd卡创建文件时用变量当文件名问题

我的SD数据存储中要实现数据的定时保存和文件新建,但是在文件名的创建上出了点问题。
在论坛看到有人说
sd卡创建文件怎么用一个变量当文件名?
file=SD.open(“                ”,FILE_WRITE);比如说
int a++;创建第一个文件名是1.txt第二个是2.txt
方法1:用"sprintf"语句能搞定.可以创建诸如以"年月日"或"时分秒"的组合数字为文件名的文件!
方法2:Sd.open函数的第一个变量可以是字符串指针!例如 char *a0="1.txt";file= Sd.open(a0,FILE_WRITE);
这样剩下的就是构建字符串了!

我查了下资料,有人说sprintf语句()会占用太多内存,然后用第二种方法来创建文件名,但是不成功,SD.h中关于文件打开函数的语法如下,
File open(const char *filename, uint8_t mode = FILE_READ);

想通过把文件名放到字符串数组中,通过对数组元素的引用实现文件名建立,但是编译报错如下

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
sketch_sep14a.ino: In function 'void loop()':
sketch_sep14a:35: error: no matching function for call to 'SDClass::open(String&, int)'
D:\Arduino\libraries\SD/SD.h:73: note: candidates are: File SDClass::open(const char*, uint8_t)

SD.h中还有这个函数openNextFile,但是和File open(const char *filename, uint8_t mode = FILE_READ)功能不一样,前者是打开已存在的文件后操作读写,后者是打开文件操作,包括已存在和未存在的
File openNextFile(uint8_t mode = O_RDONLY);

如何用变量名做文件名建立文件?求各位高手解答一下

zjz5717 发表于 2016-9-14 19:33:37

我测试了一下,直接是编译通过没有任何问题啊

zjz5717 发表于 2016-9-14 19:41:04

错误提示,没有匹配到函数
我看了一下我的库文件
是File SDClass::open(const char *filepath, uint8_t mode)
你看下你的库文件是不是有问题
我看好像uint8_t mode 这里你那里写的是int

suoma 发表于 2016-9-14 22:45:04

zjz5717 发表于 2016-9-14 19:41
错误提示,没有匹配到函数
我看了一下我的库文件
是File SDClass:pen(const char *filepath, uint8_t m ...

filepath是文件路径,我的是filename

zjz5717 发表于 2016-9-15 14:12:02

suoma 发表于 2016-9-14 22:45
filepath是文件路径,我的是filename

那只是一个变量名,示例上都是只写的文件名

suoma 发表于 2016-9-15 17:19:41

zjz5717 发表于 2016-9-15 14:12
那只是一个变量名,示例上都是只写的文件名

在SD.CPP中是这样
File SDClass::open(const char *filepath, uint8_t mode)
但是在SD.h中是我文中那样filename

zjz5717 发表于 2016-9-16 08:40:48

suoma 发表于 2016-9-15 17:19
在SD.CPP中是这样
File SDClass:pen(const char *filepath, uint8_t mode)
但是在SD.h中是我文中那样 ...

那后半部分是mode吗

suoma 发表于 2016-9-16 21:11:54

zjz5717 发表于 2016-9-16 08:40
那后半部分是mode吗

                     是的

zjz5717 发表于 2016-9-17 11:12:20

这样你看一下sd库需不需要更新

suoma 发表于 2016-9-17 11:49:59

zjz5717 发表于 2016-9-17 11:12
这样你看一下sd库需不需要更新

好吧,谢谢

zjz5717 发表于 2016-9-17 19:17:40

本帖最后由 zjz5717 于 2016-9-17 19:18 编辑

/*
SD card read/write

This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created   Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

*/

#include <SPI.h>
#include <SD.h>

File myFile;
String a="test.txt";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
}


Serial.print("Initializing SD card...");

if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
}
Serial.println("initialization done.");

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open(a, FILE_WRITE);

// if the file opened okay, write to it:
if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
} else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
}

// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
} else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
}
}

void loop() {
// nothing happens after setup
}
你看下我的

suoma 发表于 2016-9-17 21:20:57

zjz5717 发表于 2016-9-17 19:17
你看下我的

这个例子官方库里有的

zjz5717 发表于 2016-9-18 17:26:10

suoma 发表于 2016-9-17 21:20
这个例子官方库里有的

你这个可以编译通过吗

zjz5717 发表于 2016-9-18 17:26:43

suoma 发表于 2016-9-17 21:20
这个例子官方库里有的

如果也不行的话可能是库的问题,如果可以的话只能说明你写的代码有问题

suoma 发表于 2016-9-18 20:30:24

zjz5717 发表于 2016-9-18 17:26
你这个可以编译通过吗

官方的库肯定可以编译过。我的问题是如何用变量名做文件名建立文件?
页: [1] 2 3
查看完整版本: 关于sd卡创建文件时用变量当文件名问题