File open with relative path fails - c

File open with fopen() fails when I provide a relative path. I am running this code on a windows machine with Visual Studio 2010.
const char* OUTPUT_FILE = "output/PERFORMANCE.txt";
FILE* f = fopen(OUTPUT_FILE, "w");
Is this way of specifying relative path incorrect? Should I be using "\" separator?
Specifying the absolute path using the same format works fine. (e.g "C:/output/PERFORMANCE.txt")

Check what directory you are currently in. With Windows there are at least two ways:
_getcwd
GetCurrentDirectory
Make sure you are in the directory you think you are, and make sure that the directory output exists in the directory, otherwise you'll need to create it. With Windows, there are at least two ways:
_mkdir
CreateDirectory
The file will fail to open (even for writing) if any intermediate directories are not present. The file may also fail to open if you do not have permissions to the target directory.

Does the output subdirectory exist? Opening a file for write will create the file if it doesn't exist, but not the path to it.

Related

Cannot open a file in C- Error in opening a file in C

The main function that opens the file is located in "project" directory. Then I have a "cmake-build-bug" directory( located in "project" directory) where is "data.txt" file located. I want to read it, but for some reason it keeps wrinting Error to open file. I never understand how to write the correct path to the file. Or is there another problem that gives the error?
FILE *file[2] ;
char *fileName2 = "data.txt";
file[1] = fopen(fileName2, "r");
if (file[1] == NULL){
printf("Error to open file\n");
}
printf("Reading..\n");
fclose(file[1]);
When using an IDE (and since you have a cmake-build-debug directory then I guess you're using the CLion IDE) the default working directory is seldom what is expected. And for CLion it seems it's not where you currently store the data.txt file. (I don't remember the default for CLion.)
Assuming CLion, you need to edit your "Run/Debug Configuration" for the program (menu "Run" and item "Edit Configuration"), and set the "Working Directory" to where you have the data.txt file.
According to http://man7.org/linux/man-pages/man3/fopen.3.html the first param is a pathname and not a filename as a lot of people believe.
Let's assume that c:\your_ project\a_subfolder\data.txt is the pathname, just remember that \ is an escape char, so I believe in general, when the file is in a different folder, you could try char *fileName2 = "c:\\your_ project\\a_sub_folder\\data.txt"
Just keep an eye on the real path of the file. Cmake tends to create folders to put the executable elements in. For example, look at this case where the file was in a local folder and cmake created a copy in the debug folder.

How to save file in custom place in C

I'm working with C right now. And there's a problem. I don't know how to save a FILE in custom place. When I run *.exe file, it saves them where code is placed. So how to make it save FILEs where I want it to be?(I can input a path)
a FILE is actually a long type that addresses a path on your computer.
Whether you use linux, windows, etc, the common thing about the paths is the idea that there are relative paths or absolute paths.
From what I've understood you probably did use the relative path, and I can guess you didn't specified a path at all, but only the file name.
Notice that a file's name alone is placed relatively to the path of the program you are running.
To fix your problem you might want to give an absolute path (such as "/home/user/" on linux or "C:\Users\user" on windows [pay attention for the escaping backslash]).
You can do it by something like this:
FILE *output = fopen("/home/user/output.txt", "w");
(where "w" means writing permissions to the file at the given path).
Hope this answers your question.

The system cannot find the file specified

The file is inside the directory where the software is. I am trying to add the text file to the memo box.
procedure TForm4.FormCreate(Sender: TObject);
var
dir : string;
begin
Form4.Caption:='Abateri instrumente';
dir := GetCurrentDir;
Memo1.Lines.LoadFromFile(dir+'\abateri.txt');
end;
In your specific situation, you should load the file with the code
Memo1.Lines.LoadFromFile(dir+'\abateri.txt.txt');
This is because in the below screenshot that you provided, the extension of the Project3 file is hidden, which loads to the conclusion that the option to hide known file extensions is enabled. Yet the one for the abateri.txt file is shown, which can only lead to the often seen double extension mistake.
Either rename your file and remove the redundant part (the first .txt, which is preferred) or use the double extension in your code.
I would also suggest disabling that option in Windows Explorer:
Tools > Folder Options > View > Uncheck the "Hide extensions of known file types"
In addition to the above, you should always build up paths with the TPath.Combine function call to ensure that they are correct.
You can see the documentation of it here
The file is inside the directory where the software is.
In that case, looking in the working directory is the wrong approach. There's no reason why the working directory should be directory where your executable resides. You need to use:
Dir := ExtractFilePath(ParamStr(0)); // the directory where the executable resides
TPath.Combine(Dir, FileName); // TPath is from the System.IOUtils unit
Of course, your other problem is that you got your file name wrong. The file is actually named abateri.txt.txt.

Relative path in C file handling

I need to read file in my program so while providing path I want to give relative paths because all files to be opened will be in some folder within current folder.
I tried this:
FILE *f=fopen("./abc/p.txt","r")
abc is folder withing current folder, but fopen returns NULL. How to do this thing?
This comes from either one of those:
. or ./abc/ is not readable or traversable
./abc/p.txt is not readable
./abc/p.txt does not exist
./abc/p.txt is a broken link
Look at errno to know what's the real problem.
this will run:
FILE *f=fopen("...\\abc\\p.txt","r");

What is a relative path like in C?

I am now using Visual C++ 2010 to open a txt file.
fp = fopen("E:\\CProg\\Huffman\\Debug\\Huffman.txt","r"); //Right
//Wrong
//fp = fopen(".\\Huffman.txt","r");
//fp = fopen("\\Huffman.txt","r");
//fp = fopen("Huffman.txt","r");
In VB.NET, I used to write like this: Application.Startpath & "\". Then how do I make it in C?
Relative to what? On "all relevant platforms", if you use a filename that's not absolute it will be resolved relative to the current directory.
The following is for Windows.
If you are writing a console application, the application will start with the current directory set to whatever the command prompt shows. Without further research I can't tell what the initial current directory for a GUI application will be.
If you want your filename to be relative to the Installation directory you'll have to use something like the Win32 function GetModuleFileNameW() and work your way from there (the function gives you the pathname to the exe file; remove the last component to get the directory, and append whatever path you want to append)-
There is no direct way to say "I want this filename to be used relative to the installation directory".

Resources