Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Highlighted
Developer
Posts: 580
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA
Accepted Solution

Writing/reading data to files in the application's "data" directory

[ Edited ]

I'm trying to write and then read a bit of data, and it's not working. The file size is coming back as "0" after I write the file:

 

char* ptr = NULL;
write_file("./data/test.txt", "testing!");
read_file("./data/test.txt", &ptr);
fprintf(stderr, "HERE: %s\n", ptr);

void write_file(char* file_name, char* data)
{
    FILE* file = fopen(file_name, "w");

    if (file == NULL)
    {
        fprintf(stderr, "Cannot write to file: %s\n", file_name);
    }
    else
    {
        fprintf(file, data);
    }

    close(file);
}

int read_file(char* file_name, char** data)
{
    FILE* file = fopen(file_name, "r");

    if (file == NULL)
    {
        fprintf(stderr, "Cannot read from file: %s\n", file_name);

    }
    else
    {
        struct stat st;
        int rc = stat(file_name, &st);

        if (rc)
        {
            return -1;
        }

        long num_bytes = st.st_size;

        *data = malloc(num_bytes + 1);

        fread(*data, 1, num_bytes, file);

        (*data)[num_bytes] = NULL;
    }

    close(file);

    return 0;
}

Not sure what I'm doing wrong here...

Developer
Posts: 101
Registered: ‎02-25-2011
My Device: Playbook, q5, z10
My Carrier: TM

Re: Writing/reading data to files in the application's "data" directory

[ Edited ]

can you edit your post and place your code inside a code field? (the symbol with the small c)

 

have you set the permission in the bar-descriptor.xml?

________________
mappau Dev Team
blackberry@mappau.com
twitter: mappau
Developer
Posts: 62
Registered: ‎03-31-2012
My Device: Playbook
My Carrier: None

Re: Writing/reading data to files in the application's "data" directory

[ Edited ]

No real Idea, but some hints:

  • Did you create the directory data?
  • Put an output if writing is successful, so you can be sure your code is executed
  • close the file only if opened successfully
  • close the file with fclose. Maybe close doesn't do it and cause it's not flushed, the content may be empty. See http://www.cplusplus.com/reference/clibrary/cstdio/fopen/
  • add a (normally not used) fflush after writing
Developer
Posts: 580
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Re: Writing/reading data to files in the application's "data" directory

Not sure what the "small c" thing is, but I flipped to the HTML tab and put it in <pre>...</pre>, and that seems to have accomplished the same thing.

 

I haven't set that permission in bar-descriptor.xml, since I read that is only necessary if you try and write to the shared folders.

Developer
Posts: 580
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Re: Writing/reading data to files in the application's "data" directory

By "directory data", you mean create the directory?  I didn't, since "data" is listed as one of the standard folders for each app, but that is worth checking in terms of whether it's actually there. The "fopen" call does seem to succeed, so I would think that indicates the directory is there.

 

I have stepped through the code so I know it isn't returning error codes, at least upon opening the file.

 

Oh, interesting... fclose... I had copied and pasted some other sample code, maybe they were wrong to use "close". I'll check that.

Developer
Posts: 580
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Re: Writing/reading data to files in the application's "data" directory

[ Edited ]

I applied your "fclose" suggestion and that appears to have worked:

 

 

int write_file(char* file_name, char* data)
{
    int rc = 0;

    FILE* file = fopen(file_name, "w");

    if (file == NULL)
    {
        fprintf(stderr, "Cannot open file for writing: %s\n", file_name);
        rc = -1;
    }
    else
    {
        rc = fprintf(file, data);

        if (rc)
        {
            fprintf(stderr, "Cannot write to file: %s\n", file_name);
        }

        fclose(file);
    }

    return rc;
}

int read_file(char* file_name, char** data)
{
    int rc = 0;

    FILE* file = fopen(file_name, "r");

    if (file == NULL)
    {
        fprintf(stderr, "Cannot open file for reading: %s\n", file_name);
        rc = -1;
    }
    else
    {
        struct stat st;
        int rc = stat(file_name, &st);

        if (rc)
        {
            fprintf(stderr, "Cannot read from file. File size couldn't be determined: %s\n", file_name);
        }
        else
        {
            long num_bytes = st.st_size;

            *data = malloc(num_bytes + 1);

            rc = fread(*data, 1, num_bytes, file);

            if (rc)
            {
                fprintf(stderr, "Cannot read from file: %s\n", file_name);
            }
            else
            {
                (*data)[num_bytes] = NULL;
            }

            fclose(file);
        }
    }

    return rc;
}

I should go back and find that stack overflow code and note this issue. The IDE actually underlined that call to close with a warning... one of those times I should have looked closer. Thanks!

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Writing/reading data to files in the application's "data" directory

Just found this thread in my email history... I wanted to confirm the solution with fclose() was correct, and that none of the other suggestions were relevant here (though good to have, of course).

The close() call is used with raw file descriptors, which are integers returned from open() and friends. They are low-level, and share nothing with the "f" functions like fopen() and fclose(), which are higher-level and operate on the FILE* type.

The data directory does always exist (created either at install time, or at first launch, before your code runs), and since it's in the app sandbox area there's no special permission required to access it (and no other app can access it, even the system apps).

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 580
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Re: Writing/reading data to files in the application's "data" directory

Yes, it was simply "fclose" that did the trick. I didn't employ any of the other suggestions.