极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 56019|回复: 32

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

[复制链接]
发表于 2016-9-14 14:40:53 | 显示全部楼层 |阅读模式
我的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中关于文件打开函数的语法如下,
  1. File open(const char *filename, uint8_t mode = FILE_READ);
复制代码


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

  1.   This report would have more information with
  2.   "Show verbose output during compilation"
  3.   enabled in File > Preferences.
  4. Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
  5. sketch_sep14a.ino: In function 'void loop()':
  6. sketch_sep14a:35: error: no matching function for call to 'SDClass::open(String&, int)'
  7. 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)功能不一样,前者是打开已存在的文件后操作读写,后者是打开文件操作,包括已存在和未存在的
  1. File openNextFile(uint8_t mode = O_RDONLY);
复制代码


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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2016-9-14 19:33:37 | 显示全部楼层
我测试了一下,直接是编译通过没有任何问题啊
回复 支持 反对

使用道具 举报

发表于 2016-9-14 19:41:04 | 显示全部楼层
错误提示,没有匹配到函数
我看了一下我的库文件
是File SDClass:pen(const char *filepath, uint8_t mode)
你看下你的库文件是不是有问题
我看好像uint8_t mode 这里你那里写的是int
回复 支持 反对

使用道具 举报

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

filepath是文件路径,我的是filename
回复 支持 反对

使用道具 举报

发表于 2016-9-15 14:12:02 | 显示全部楼层
suoma 发表于 2016-9-14 22:45
filepath是文件路径,我的是filename

那只是一个变量名,示例上都是只写的文件名
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-9-15 17:19:41 | 显示全部楼层
zjz5717 发表于 2016-9-15 14:12
那只是一个变量名,示例上都是只写的文件名

在SD.CPP中是这样
File SDClass:pen(const char *filepath, uint8_t mode)
但是在SD.h中是我文中那样filename
回复 支持 反对

使用道具 举报

发表于 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吗
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-9-16 21:11:54 | 显示全部楼层
zjz5717 发表于 2016-9-16 08:40
那后半部分是mode吗

                       是的
回复 支持 反对

使用道具 举报

发表于 2016-9-17 11:12:20 | 显示全部楼层
这样你看一下sd库需不需要更新
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-9-17 11:49:59 | 显示全部楼层
zjz5717 发表于 2016-9-17 11:12
这样你看一下sd库需不需要更新

好吧,谢谢
回复 支持 反对

使用道具 举报

发表于 2016-9-17 19:17:40 | 显示全部楼层
本帖最后由 zjz5717 于 2016-9-17 19:18 编辑
  1. /*
  2.   SD card read/write

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

  10. created   Nov 2010
  11. by David A. Mellis
  12. modified 9 Apr 2012
  13. by Tom Igoe

  14. This example code is in the public domain.

  15. */

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

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


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

  27.   if (!SD.begin(4)) {
  28.     Serial.println("initialization failed!");
  29.     return;
  30.   }
  31.   Serial.println("initialization done.");

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

  35.   // if the file opened okay, write to it:
  36.   if (myFile) {
  37.     Serial.print("Writing to test.txt...");
  38.     myFile.println("testing 1, 2, 3.");
  39.     // close the file:
  40.     myFile.close();
  41.     Serial.println("done.");
  42.   } else {
  43.     // if the file didn't open, print an error:
  44.     Serial.println("error opening test.txt");
  45.   }

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

  50.     // read from the file until there's nothing else in it:
  51.     while (myFile.available()) {
  52.       Serial.write(myFile.read());
  53.     }
  54.     // close the file:
  55.     myFile.close();
  56.   } else {
  57.     // if the file didn't open, print an error:
  58.     Serial.println("error opening test.txt");
  59.   }
  60. }

  61. void loop() {
  62.   // nothing happens after setup
  63. }
复制代码
你看下我的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-9-17 21:20:57 | 显示全部楼层

这个例子官方库里有的
回复 支持 反对

使用道具 举报

发表于 2016-9-18 17:26:10 | 显示全部楼层
suoma 发表于 2016-9-17 21:20
这个例子官方库里有的

你这个可以编译通过吗
回复 支持 反对

使用道具 举报

发表于 2016-9-18 17:26:43 | 显示全部楼层
suoma 发表于 2016-9-17 21:20
这个例子官方库里有的

如果也不行的话可能是库的问题,如果可以的话只能说明你写的代码有问题
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-9-18 20:30:24 | 显示全部楼层
zjz5717 发表于 2016-9-18 17:26
你这个可以编译通过吗

官方的库肯定可以编译过。我的问题是如何用变量名做文件名建立文件?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-24 15:34 , Processed in 0.042213 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表