9 years, 8 months ago.

fopen local file system not working

Having issues with fopen when trying to open a file in binary mode. When using binary mode the *fp pointer returns NULL every time. The file to open does exist and is located on the local file system. What am I doing wrong?

This fails

#include "mbed.h"
#include "LPC17xx.h"          /* LPC17xx definitions */
#include "PPD_CAN.h"
#include "rtos.h"

#include <iostream> // library that contain basic input/output functions
#include <fstream>  // library that contains file input/output functions
using namespace std;

 //Open binary file to write
    FILE *fp = fopen(PATH_TO_BINARY, "rb"); // This fails and returns NULL every pass
     if (fp == NULL) {
        pc.printf("Failed to open binary.\n");
        return -1;
    } else {
        pc.printf("File opened successfully\n");
//Get some work done here
        fclose(fp);
    }

This works

#include "mbed.h"
#include "LPC17xx.h"          /* LPC17xx definitions */
#include "PPD_CAN.h"
#include "rtos.h"

#include <iostream> // library that contain basic input/output functions
#include <fstream>  // library that contains file input/output functions
using namespace std;

 //Open binary file to write
    FILE *fp = fopen(PATH_TO_BINARY, "r"); // This works every pass
     if (fp == NULL) {
        pc.printf("Failed to open binary.\n");
        return -1;
    } else {
        pc.printf("File opened successfully\n");
//Get some work done here
        fclose(fp);
    }

Just tried the following which also does not work when Binary mode is used. Is this a bug? Is the local file system not accessible when reading a file as a binary.

Does not work

streampos size;
  char * memblock;

  ifstream file ("/local/data.BIN", ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();

    cout << "the entire file content is in memory";

    delete[] memblock;

  }
  else cout << "Unable to open file" << endl;
posted by Rob Crain 31 Mar 2015

1 Answer

5 years, 8 months ago.

Hi Rob - did you ever manage to get to the bottom of this problem? I may be encountering a similar issue...