How to create a file in %temp% folder using C program. Please provide me some Ideas on it.
I am programming in windows using Mingw.
Update:
I want the file to be in .log or .txt format. How to do that?
GetTempPath + GetTempFilename
Example here
You can use Windows API GetTempPath() (see http://msdn.microsoft.com/en-us/library/windows/desktop/aa364992(v=vs.85).aspx ), build full path and then either use Win API or C functions to create file
You can use the WinAPI function GetTempPath to get the path of the temp directory.
Use getenv() to read the environment variable, then just figure out a filename and fopen() it, for example.
Note that the part where you "figure out a filename" is hard, it's better to use e.g. mkstemp() or tmpfile().
char * pTemp;
pTemp = getenv ("TEMP"); /*Get temp folder path*/
if (pTemp!=NULL)
{
printf ("Temp folder path is %s",pTemp);
}
Related
I am working on a project that uses Huffman algorithm to compress files, and I am doing my project using Java, what I want is to create my own file extension say (.huff) for the compressed file, and when I right click a file if it has the (.huff) extension, I want to add a new option which decompresses it, I searched the web but I did not find anything useful.
Any help would be appreciated.
To set the file extension just use one of the String methods like append(".yourExtension") (append it to the filename) and set as filename. Simple as that.
String filename = filename.append(extension);
To decompress the compressed file, I suggest you write a metod to which you provide a path to file as argument, check if the file extension is correct and then in another method you decompress this file.
There is nothing special about a file extension, it's just a part of the file name. To create a .huff file extension, just add .huff to the end of the file name.
To add the windows context menu, that's explained in the question linked in the comments How can I add a context menu to the Windows Explorer for a Java application?
I would recommend creating a batch script that will launch your program taking in the file to decompress as an argument.
Something similar to:
#echo off
java -cp <path-to-jar> <decompression main class> %1
Adding in any other setup or program arguments you need. Then a registry entry might look like.
HKEY_CLASSES_ROOT\.huff\shell\Decompress huffman encoded file\command
"<path to batch file>" "%1"
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 need to run a .lua file with another .lua file.
Here is my code:
Program = io.read()
dofile(program)
I type the name of the file that I want to run correctly, but for some reason my compiler says that the file I typed does not exist. (Which it does)
Lua is case sensitive; Program is not program
Otherwise the code should work.
Try putting the full path instead of the relative path. i.e. /home/nick/script.lua
I am using rename function (C, under ubuntu) to move file from one folder to another when trying:
rename("./t2.c", "./this/then_this/it_works.c");
it works wonders, removes the file from current folder and moves it to the then_this folder under name.
but when i try this:
rename("./t2.c", "~/.local/share/Trash/files/it_works.c");
it just doesn't work, but in terminal typing in "cd ~/.local/share/Trash/files/it_works.c" does open the trash bin.
So what i'm trying to do is move a file to trash bin(delete it). Could anyone tell me what i am doing wrong?
The problem is that ~ only works in the shell. Replace with the full absolute path (e.g. /home/user1031204/.local/...) & re-try.
realpath() will be helpful here. You may want to look at glob() and wordexp() as well.
~ is an handy shortcut available only in shell. You can achieve a similar behaviour by using getenv("HOME") and concatenate it with the target directory.
snprintf(buffer, size_of_buffer, "%s/.local/share/Trash/files/it_works.c",
getenv("HOME"));
Remember to #include <stdlib.h> and have a buffer which is at least PATH_MAX bytes long (limits.h) to store the result.
How can i get the paths of a folder and its content. Say i have folder named MyFolder as
/tmp/MyFolder/ where it has subfolders SubFolder1, SubFolder2... and some files
You may take a look at the opendir() family functions.
A more efficient way than {open,read,close}dir() is Linux' getdirentries() function. See getdirentries(3) for details.
Use dirent and dir structures. You can use opendir(), readdir() etc for manipulation.
readdir() will give one name at a time and you can keep calling it iteratively.