Hello everyone!
Sorry for my newbie question, but I'm trying to find a string on a text file stored on a SD-Card.
I'm using the Arduino Ethernet Shield with SD-Card Slot!
Here is the code created by David A. Mellis and modified by Tom Igoe (SD card read/write)
/*
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 <SD.h>
File myFile;
char buf[8];
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.");
// 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()) {
myFile.read(buf,8);
if(strncmp(&buf[0],"18429880",8)==0)
{
Serial.println("Match!");
}
}
// 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
}
Hello MarkT, actually I would like to know if there's a way to find the string on .txt file (I'll use the txt file as a small Database).
Thanks,
Rodrigo.
actually I would like to know if there's a way to find the string on .txt file
You are doing fine, except that you are not skipping the carriage return and line feed that are between each set of characters that look like a number to you.
Change your code so that you read 10 characters, instead.
You also need to append a NULL to buf after each read, since buf is NOT a string, and strncmp() expects a string.
Don't forget to make buf bigger, to handle the two extra characters AND the NULL.
By the way buf and &buf[0] point to the same memory location, so your strncmp() call could be simpler:
hello, trying to use this same method and code to find a string in a text file but when i do it, i only get to the Serial.println("mydata.txt:"); line.
Thank you PaulS, code works now there we extra spaces between numbers in the text file. Is there a way to search for a string in a text file then delete it?
Is there a way to search for a string in a text file
You can read the data from the text file, storing it in a string (NULL terminated array of char), and then determine if that string contains the string of interest.
Deleting it is a bit trickier.You'd need to read the data from one file, and then write it, or a modified version of it, to another file.
ok thanks, trying to use a php in web page to edit the text file
How? The PHP engine runs before the page is displayed in the browser. It runs on the server. If the Arduino is the server, how are you running the PHP engine on the Arduino?
i am in the research process, i am new to arduino and web design. What i need to do is edit the textfile stored on the arduino ethernet sheild SD card remotely over a local network.
What i need to do is edit the textfile stored on the arduino ethernet sheild SD card remotely over a local network.
Sorry. This still doesn't make sense. The only way for the client to know what is in the text file is for the Arduino-as-server to send the client the contents of the file. In which case, the client MIGHT send back a request to remove some data from the file.
At which point, the Arduino-as-server has no more role, and the Arduino-as-file-owner takes over and modifies the file. PHP has NOTHING to do with editing the file.
well thats what i was asking about at first..... if there is a way to remove a specific string from a text file with an arduino function call from a webpage.
My plan B is to edit copy of the text file on a remote server then upload it to the arduino each time it is saved.
if there is a way to remove a specific string from a text file with an arduino function call from a webpage.
You can't call an Arduino function from a webpage.
I think you need to put the Arduino away for a while, and learn about client/server architecture, and getting a client to make a server do something, using a PC as web server. When you understand that, you'll understand that your questions don't make sense.
Right now what i'm trying to do is make a simple database to .CSV in SDcard..
now, i'm still learning how to write and read on CSV file.
So I tried some example on SDcard that read some data on files
/*
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
}
but how i can make modification to the code, so that i can read these data and make something cool..
(example)
[ID] [Name]
[1] [James]
[2] [Rudy]
So lets say if i receive data '2' from serial, arduino will open the somerandom.CSV file and will search for id number 2, and says that "Hello Rudy"
Sorry if i'm madly beginner to arduino and C language..
Please give me some support
Thanks !