Languages

Menu
Sites
Language
How to access file to read and write from Internal storage

Hello Developer,

I need to know how can I read or write to a file that is to be saved in the internal storage of the phone? And also I've go through this documentation ( https://developer.tizen.org/dev-guide/2.4/org.tizen.native.mobile.apireference/group__CAPI__SYSTEM__STORAGE__MODULE.html ) but I found it a bit confusing and unsure how to do a simple read write operation on the file. A simple solution for this problem will be appreciated.

Thank you

Responses

1 Replies
Mehedi Alamgir

Hi Ashfaq

If you want to read and write text data in a file, use the FILE structure. You can read files in the (/res) folder, but cannot write them. The files in the (/data) folder are available for  both reading and writing. 

Using following two function you can read and write file. 

static char* write_file(const char* filepath, const char* buf)
{
    FILE *fp;
    fp = fopen(filepath, "w");
    fputs(buf, fp);
    fclose(fp);
}

static char* read_file(const char* filepath)
{
    FILE *fp = fopen(filepath, "r");
    if (fp == NULL)
        return NULL;
    fseek(fp, 0, SEEK_END);
    int bufsize = ftell(fp);
    rewind(fp);
    if (bufsize < 1)
        return NULL;
    char *buf = malloc(sizeof(char) * (bufsize));
    memset(buf, '\0', sizeof(buf));
    char str[200];
    while(fgets(str, 200, fp) != NULL) {
        dlog_print(DLOG_ERROR, "tag", "%s", str);
        sprintf(buf + strlen(buf), "%s", str);
    }
    fclose(fp);
    return buf;
}


You can get the absolute path to the application resource directory (/res) using app_get_resource_path() api and and to get absolute path of data directory (/data) use app_get_data_path() api. 


Hope this will help you.
If you find my answer is helpful for you, Please mark it as BEST ANSWER so that other may find it easier from the next time.