如何逐行读/写文本?

时间:2014-09-15 08:30:46

标签: arduino arduino-uno

我尝试使用Auduino Uno通过线路ID读取/写入SD卡中的txt文件存储。但我没有成功。 请帮我。感谢

4 个答案:

答案 0 :(得分:4)

您可以使用

if (myFile) {
 while (myFile.available()) {
  String line = myFile.readStringUntil('\n');
 }
}

答案 1 :(得分:0)

请从 Arduino>开始例子> SD>读写

/*
  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;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

  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("test.txt", 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
}

代码应该有效。确保您的引脚连接正确。

答案 2 :(得分:0)

不确定你是否仍需要这方面的帮助,但现在就去了。

第一个建议 - 更改login.txt

请尝试在ID之间插入“\ r \ n”,而不是使用br标记。例如,如果要通过创建PHP + SQL数据库查询并将结果写入文件来创建login.txt,请将代码中的br替换为“\ r \ n”。或者,如果您能够打开并编辑login.txt,请执行此操作,并使用新行替换每个br。

然后您需要做的就是逐个读取文件中的每一行。

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

int CS_PIN = 10;
char logfile[] = {"login.txt"};

File file;

void setup() {

  Serial.begin(9600);
  initializeSD();

  openFile(logfile);
  while(file.available()) {
    // print the lines to serial monitor
    Serial.println(readLine());
  }
  closeFile();
}
void loop() {
}

void initializeSD() {
  Serial.println("Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);

  if (SD.begin()) {
    Serial.println("SD card is ready to use.");
  } else {
    Serial.println("SD card initialization failed");
    return;
  }
}

void closeFile() {
  if (file) {
    file.close();
    Serial.println("File closed");
  }
}

int openFile(char filename[]) {
  file = SD.open(filename);
  if (file) {
    Serial.println("File opened with success!");
    return 1;
  } else {
    Serial.println("Error opening file...");
    return 0;
  }
}

String readLine() {
  String received = "";
  char ch;
  while (file.available()) {
    ch = file.read();
    if (ch == '\n') {
      return String(received);
    } else {
      received += ch;
    }
  }
  return "";
}

这是一个缩短的,未经测试的,并在此YouTube link上发布的代码稍加修改(请参阅视频说明以获取完整代码的链接)。另外,请务必仔细检查接线,如图所示。

第二个建议 - 更改传入字符的读取方式

如果您无法更改login.txt文件的结构,并且只能从SD卡读取它,请尝试使用此代码以便从文件中读取(第一个建议中的文件的修改版本):

String readLine() {
  String received = "";
  char ch;
  while (file.available()) {
    ch = file.read();
    if (ch == '<') {            // Is the tag starting?
      return String(received);  // Return what you have so far (ID)
    } else if(ch == '>') {      // Is the tag ending?
      received = "";            // Empty the string, we don't need br
    } else {                    // Everything's OK, append to the string
      received += ch;
    }
  }
  return "";
}   

请注意,两个建议都不会将这些行存储到单独的变量中。我建议您使用文件中的每一行执行您想要执行的操作(打印到串行,闪烁LED等),阅读时

答案 3 :(得分:0)

塔文森的答案是正确的

if (myFile) {
 while (myFile.available()) {
  String line = myFile.readStringUntil('\n');
 }
}