语言

Menu
Sites
Language
File creation problem

How do i create two files in app data directory ?

Im unable to create two files and store the file path pointer of both.

char* get_write_filepath(char *filename)
{

    char write_filepath[10000] = {0,};
    char *resource_path =NULL;
    resource_path=app_get_data_path();
    if(resource_path!=NULL)
    {
        snprintf(write_filepath,10000,"%s%s",resource_path,filename);
        free(resource_path);
    }

    return write_filepath;
}

 char* file_path1=NULL;
    char* file_path2=NULL;
    file_path1=get_write_filepath("TestApplication1.txt");
    file_path2=get_write_filepath("TestApplication2.txt");

 

When i print  file_path1 and file_path2 ,both are same.How ?

编辑者为: Rohit Shinde 08 2月, 2019
查看选择的答案

响应

4 回复
Mark as answer
Paul L

You cannot return local arrays in c. Allocate write_filepath dynamically or use static variable. 

char* get_write_filepath(char *filename){

write_filepath = new char[1000];

instr ; instr2;...

return write_filepath;

}

 char*   file_path1=get_write_filepath("TestApplication1.txt");

free(file_path1);

 

 

Rohit Shinde

Thanks.That problem got solved.I wrote some text in both files but when I use the following code to open both the files sequentially,the files cannot be opened.

file_path1=get_write_filepath("TestApplication1.txt");
 file_path2=get_write_filepath("TestApplication2.txt");

read_file(file_path1);

read_file(file_path2);

void read_file(char* file){
    FILE *fptr;
    ssize_t read;
    size_t len = 0;
    char * line = NULL;

    if ((fptr = fopen(file, "r")) == NULL)
    {
            dlog_print(DLOG_ERROR, LOG_TAG,"Error! opening file");
        // Program exits if file pointer returns NULL.
        return;
    }

    // reads text until newline
    while ((read = getline(&line, &len, fptr)) != -1) {
            dlog_print(DLOG_ERROR, LOG_TAG,"%s", line);
    }
    fclose(fptr);
}

 

 

Paul L

Did you change get_write_filepath. Do you have correct file path?

Here you have info about available dir structure:

https://developer.tizen.org/development/training/native-application/understanding-tizen-programming/file-system-directory-hierarchy

Rohit Shinde

yes,I got it now.It had to do somthing with the file path itself.The problem is solved.Thanks!!!