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.
Related
I need something that can copy a specified file any and everywhere on my drive (or computer) where that file already exists; i.e. update a file. I tried to search this site, in case I'm not the first, and found this:
CMD command line: copy file to multiple locations at the same time
But not quite the same.
Example:
Say I have a file called CurrentList.txt, and I have copies of it all over my hard drive. But then I change it and I want all the copies to update. So I want to copy the newer one over all the others. It could 'copy if newer', but generally I know it's newer, so it could also just find every instance and copy over it.
I was originally going to use some kind of .bat file that would have to iterate over every folder seeking the file in question, but my batch file programming is limited/rusty. Then I looked to see if xcopy could do it, but I don't think so...
For how I will use it most, I generally know where those files are going to be, so it actually might be as good or better if I could specify it to (using example), "copy CurrentList.txt, overwriting all other copies wherever found in the C:\Lists folder and all subfolders".
I would really like to be able to have it in a context menu, so I could (from a file explorer) right click on a file or selected files and choose the option to distribute it.
Thanks in advance for any ideas.
Use the "replace" command...
replace CurrentList.txt C:\Lists /s
i want to move a file from a directory to anther directory with C Coding.
I search and find rename(); function , but when working it doesnt work and have a error:
my code:
#include <stdio.h>
int main() {
if(rename("/root/tmpfile.php", "/home/check-tmp.php"))
perror( NULL );
}
the code well compiled but when running this code showing this error:
Invalid cross-device link
How to move a file from a directory to anther directory without using System for fopen?
Aslo , i finded many codes and ways to do it but doesnt working all codes.
Please say me a way and make sure it will work
Thanks.
Many aspects of the behavior of `rename' are inherently platform-dependent: The rename operation might not be able to move a file from one file system to another , it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists.
In other words, your system does not support rename files cross different partitions (your root partition and your home partition are different.)
So the solution is when it fails, copy the file to the destination and delete the original.
The rename call can only rename and move files within a single disk partition. The error "cross-device link" indicates that you attempted to move a file from one partition to another. (If you were on a Windows system, you can imagine if you tried to rename a file from C: to D:.)
When you use the Unix mv command to move files, it first tries a rename, but if it fails in this way, it falls back and makes a new copy of the file in the new location, then deletes the original. That's what you would have to have your code do in this situation, too.
(Copying a file is easy enough, but there are plenty of library functions out there that will do it for you, and also take care of things like preserving the last-modified time and other file attributes.)
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");
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".
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.