Tags:
Porque não usar um formato CSV? É um protocolo externo?
um exemplo de leitura de CSV da biblioteca SDFat.
/*
* This example reads a simple CSV, comma-separated values, file.
* Each line of the file has three values, a long and two floats.
*/
#include <SdFat.h>
// SD chip select pin
const uint8_t chipSelect = SS;
// file system object
SdFat sd;
// create Serial stream
ArduinoOutStream cout(Serial);
char fileName[] = "3V_FILE.CSV";
//------------------------------------------------------------------------------
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
//------------------------------------------------------------------------------
// read and print CSV test file
void readFile() {
long lg;
float f1, f2;
char c1, c2;
// open input file
ifstream sdin(fileName);
// check for open error
if (!sdin.is_open()) error("open");
// read until input fails
while (sdin >> lg >> c1 >> f1 >> c2 >> f2) {
// error in line if not commas
if (c1 != ',' || c2 != ',') error("comma");
// print in six character wide columns
cout setw(6) lg setw(6) f1 setw(6) f2 endl;
}
// Error in an input line if file is not at EOF.
if (!sdin.eof()) error("readFile");
}
//------------------------------------------------------------------------------
// write test file
void writeFile() {
// create or open and truncate output file
ofstream sdout(fileName);
// write file from string stored in flash
sdout pstr(
"1,2.3,4.5\n"
"6,7.8,9.0\n"
"9,8.7,6.5\n"
"-4,-3.2,-1\n") flush;
// check for any errors
if (!sdout) error("writeFile");
// file is closed by destructor when it goes out of scope.
}
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
// create test file
writeFile();
// read and print test
readFile();
cout "Done" endl;
}
void loop() {}
Bom dia Jonatas, muito obrigado por sua disposição em responder, vou tentar com CSV.
Se alguem puder enviar um exemplo para TXT eu ficarei agradecido tambem.
Muito obrigado aos amigos!
Pessoal por favor me dê uma força nisso. Estou precisando muito.
Obrigado
É a mesma coisa.
Só q você usou "espaço" como separador, o CSV usa uma virgula. Exatamente a mesma coisa.
Tudo arquivo texto (TXT).
Ao usar espaço você complica as coisas, pois não pode armazenar uma variavel que contenha espaço, como o nome de uma pessoa.
Pois é, alem de usar espaço usou aparentemente mais de um...
Ou pode ser um TAB.
Se vc puder gerar o arquivo com virgulas ou ";" fica mais facil mesmo.
O Excel gera normalmente com ";" para não confundir com a virgula do numero. Qualquer caracter que não seja pertencente ao texto vale...
Para editar seu arqvuivo vc pode abrir com o Notepad e usar o Procurar|substituir. Se alguma linha der errado tem que arrumar "com a mão".
Bem-vindo a
Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)
© 2024 Criado por Marcelo Rodrigues. Ativado por