0

I wrote some code in C++ (Used libraries like fstream, vectors etc.) Now when I transferred the code to Arduino it wouldn't compile so I added the following library: https://github.com/maniacbug/StandardCplusplus I downloaded the IDE from here: https://www.arduino.cc/en/main/software

Now almost all my code works: All the libraries I've used work except for fstream which I've used to read a specific file.

Now is there a certain problem with the 'StandardCplusplus' library and how can I counter this problem.

Plus can someone give me an alternative for fstream that works with Arduino. Was reading a bmp image using C++. Now fstream works in a certain way (reads the file in a certain way). Is there some alternative to fstream.

  • Work with Atmel Studio and their compiler? – MathieuL Aug 11 '15 at 15:55
4

When you run that library on the Arduino and ask it to open a file, where do you imagine it would read the file from? Your Arduino has no hard disk or other file system.

One way to solve this is to include a library that emulates a read-only file system in the Arduino's flash. I dunno whether this exists (such libs do exist for for some other targets, for instance for the Nintendo DS), but it would be a bit of overkill IMO.

Another way is to include the data that is in the file in your program, as a const (expr) array of bytes, and read from that array instead of from the file. But if you have a library that is coded to use file read calls this would require some rewriting.

A third approach would be to do the reading and interpreting of the file on your host PC, output the interpreted content to a const (expr) byte array, and include that in your application. This frees you from having to include the bmp-read code in your image.

  • seem a lot easier to use atmel studio and their compiler that let you use Macros... – MathieuL Aug 11 '15 at 15:56
  • How would that solve the problem of not having a file system? – Wouter van Ooijen Aug 11 '15 at 17:32
  • AVR GCC can compile c++, but Arduino IDE isn't the most suitable tool to do that all. With a tool like Atmel studio, he could create a "memory partition" in the internal memory, i see project were dev kit where transform into fat32 USB stick,therefore he would have a "file system". – MathieuL Aug 11 '15 at 18:38
  • Well you can add a filesystem if you add an SD Card reader. You'd just have to replace part of the code with the SD library... However this better be done on a Mega 256 or better. – dda Aug 12 '15 at 5:36
2

If you want to write C++ for an Arduino, I'd recommend you cease using the Arduino IDE and instead use Atmel Studio. The microcontroller on board is an Atmel AVR, and you can write for that using the gcc-avr toolchain that comes with Atmel studio.

You'll lose the spiffy libraries that come with the arduino platform that abstract away a lot of the intricacies of the hardware, and you'll instead be looking at the AVR datasheet and the schematic of the arduino, but you'll learn quite a bit in the process and have direct control over the hardware.

With all that said though, the AVR does not have a file system so using fstream doesn't make any sense to begin with.

1

The Arduino IDE does compile your code using the avr-g++ compiler. In other words, it is C++.

Was reading a bmp image using C++.

This isn't practical on the smaller Arduinos, at least, because they have so little RAM. They also don't have a file system. There is a streaming library for output, which is very handy for streaming out things to Serial, LCDs, etc.

One of the replies here mentioned that if you switch to Atmel Studio you can look at the datasheet and get down to the intricacies of the hardware. Well, you can certainly do that as well with the IDE. All the hardware registers are available for use. I often use the "bare" registers for timers, watchdog, interrupts etc.

Also Atmel Studio only runs on Windows. You are out of luck if you use a Mac or Linux.

There is also a port of the Standard Template Library for the Arduino, if you want to use vectors, lists, maps etc.

There is also another port of the STL which may work better and be easier to install.

1

Are you using Arduino UNO? Look at the datasheet and see how much RAM the Arduino has. Assuming you are using Arduino Uno, it should be around 2kilobyte or less. So it is highly likely not to have enough memory to read the BMP file. Even if you are trying to read very small BMP file, the code reading the file would vary depending where you are reading the BMP file from (SD Card, SPI Flash memory, etc.) It cannot read files directly from your computer using just fstream.

0

To answer your compile problem.. Are you using c++11? The issue could be that Arduino Compiler may not support entire C++11 features.

So if you cant find an alternative for fstream (I dont know any) then you have to find a different way compile the C++11 code and upload it. You can do this with a Makefile, avr-gcc. Its plenty of generators out there.

  • While C++ version features can be an issue, in this case it is far overshadowed by the fact that the library is trying to perform tasks that aren't possible on the Arduino. – Jake C Aug 28 '15 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.