Skip to content

Specifying a directory when using fopen()

Question

I am having trouble using the fopen() command to write a new file to a specific directory. I can't seem to find the file after I create it. Do you have some advice or examples I can follow?

Synopsis

Specifying a directory when using fopen()

Solution

NOTE: There are examples in this note that you can cut and paste into your OSLO Text Editor (Window>>Editor>>Open). The lines of code can then be executed as a group from there (select the group of lines and use "Edit>>Execute Selection" - there can be no blank lines in the block of lines you have selected). If you don't use the OSLO text editor, you will have to cut and paste individual lines into the command line. ANSWER: The file that is created with the fopen() command is placed in your "current directory" as defined by the preference items "Current_directory" (cdir) and "Current_text_directory" (tdir). Issue the command:
Show_Preference Current_directory;
...as well as the command:  
Show_Preference Current_text_directory;
...to locate your current directories. You can't change these preferences directly because they are "read-only" attributes. However, you can change them through the change_directory() command. Note that the file that is created through the fopen() command gets placed in the directory that was last defined with the change_directory() command. Examples follow. Run the following lines of code:  
j = strcpy(str1,str_pref(prid)); j = strcat(str1,"len"); change_directory(len, str1); j = fopen("aatestfile.txt",w); k = fclose(j);
...and the file will be created in your "...privatelen" directory Now try the following lines of code:  
j = strcpy(str1,str_pref(prid)); j = strcat(str1,"ccl"); change_directory(txt, str1); j = fopen("aatestfile.txt",w); k = fclose(j);
...and the file will be created in your "...privateccl" directory Whichever directory is defined last through change_directory is the directory where the file will be created. Of course, you can just specify the path as part of the name:  
j = strcpy(str1,str_pref(prid)); j = strcat(str1,"aatestfile.txt"); j = fopen(str1,w); k = fclose(j);
...and the file will be created in your "...private" directory