1

I'm using Arduino IDE, and I'm trying to read from a text file using fopen function, but it does not work, it shows this error "std::fopen is not a member of std"

void tst() {
    FILE *fp = std::fopen("test.txt", "r");
    char str[60];

    if (fgets(str, 60, fp) == 0) {
        digitalWrite(pin, HIGH); 
    } else {
        digitalWrite(pin, LOW); 
    }
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Afnan Humdan
  • 195
  • 3
  • 12

4 Answers4

4

You have to include cstdio via #include <cstdio> in the file where std::fopen is supposed to be used.

sperber
  • 661
  • 6
  • 20
3

If you use:

#include <stdio.h>

then you need to use fopen, without the std namespace.

If you use

#include <cstdio>

then you can use std::fopen.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Well, Eventually, I recognised that the Arduino IDE can not read a file from my PC, there is no other way to do that except from an SD card.

Thank you all

Afnan Humdan
  • 195
  • 3
  • 12
-1

Instead use this:

FILE* fp = fopen("test.txt", "r");

Classes and namespaces are C++ features. Not C.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • The OP is likely using C++, or the error message would look very different. `std::fopen` [works in C++](http://stackoverflow.com/q/28227006/1600898) as long as the appropriate header is included. – user4815162342 Jan 19 '16 at 23:27
  • @user4815162342: In the Arduino environment, programmers do not write any `#include`s. The environment guesses which .H files to include automatically, but it is subject to many assumptions. Even in `c++`, fopen() works just fine. – wallyk Jan 19 '16 at 23:30
  • even after I included the library stdio.h, the compilers does not recognise the fopen – Afnan Humdan Jan 19 '16 at 23:33
  • @AfnanHumdan: Did you also remove the `std::` before `fopen()`? – wallyk Jan 20 '16 at 00:51
  • Thanks for the clarification. (The downvote is not from me.) – user4815162342 Jan 20 '16 at 07:22