reading txt from computer

Hi,
I just want to ask that for Arduino IDE, is it able to read certain texts from a txt file from a PC or laptop.
Like what the C++ compiler use the iostream library to read and write from and into the files?

Can arduino do this?

Not sure what you mean. You can use copy and paste to transfer text between an Arduino IDE and any other program.

An Arduino program has no access to files on a PC. If you want to transfer data from a PC file you will need a program running on the PC to read the file and send the data to the Arduino.

This Python - Arduino demo may give you some ideas.

...R

There are many ways to feed Arduino from your PC through the USB/COM port cable.

Just offhand, a good terminal program (or even Hyperterm) will let you do it.

Another route is connect an SD adapter (buy or make) and read the file(s) off the card without needing a PC. SD is good cheap mass storage for Arduino!

As others mentioned, Arduino IDE only compiles programs running on an arduino. Your experience with c++ and iostream is using a compiler that compiles code that runs on a computer, often the same computer the compiler runs on. Arduino IDE is a cross compiler. It compiles code that does NOT run on the pc it runs on. If the code doesn't run on the pc, it can't open files using an iostream.

There is streaming io for Arduino though, link from the Arduino site Reference Page.

But caution if you work on smaller Arduinos with less than 64K RAM, use of C++ Container Classes and dynamic allocation as a way of coding is a good way to have mystery crashes. The smart thing is to avoid them and do all text work with C string arrays and string.h functions. Yes, String is a Container Class with dynamic allocation behavior, it sucks on small environments, avoid it on Arduino.

OP wanted to open iostream to files on PC, not serial I/O. It's not possible.

I think he just wants to get text across.

In command line DOS (shell) you use mode to set up COM ports usually in a batch file that would take a command line arg as the name of the file to send. It worked for me a few years ago.

SD is quicker unless your Arduino has a USB-native chip (Leo, Due?...) and you can roll your own or buy an adapter cheap, you can pass a LOT of text on a small SD card and put the results back on the same or another dedicated SD card (faster) or show them in a led display as some have done. :slight_smile:

But on the Arduino end our OP may want to see about code familiarity. If you've never learned C strings they might seem scary or something. With C++ you are protected from knowing all the cycles spent shuffling data around shotgunning heap RAM while C forces you to know and do all that, or not, by yourself.

C string is a char array, the elements get ASCII text values with a terminating NULL (0) at end of text.

And that's pretty much it. If you do your own array manipulation, you don't even need string.h. With state machines and SPI, you should be able to run text through Arduino like twigs through a tree chipper.