Create files in fuse using C - c

Im trying to have fuse create files when I mount the system using the init function. However whenever I mount my system the process never ends. Then i cant unmount it afterwards. How do i fix this.
void file_init(struct fuse_conn_info *conn){
FILE *fp;
fp=fopen("/data1/fuse/file.txt","w+");
fclose(fp);
}
Thats the code im using. I need to create multiple files but I cant even get this 1 file to work.

Check the return of fopen if you want to know why it fails:
fp = fopen("/data1/fuse/file.txt", "w+");
if (fp == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
fclose(fp);
If data1 is in the same directory (not in the root path), change
fp=fopen("/data1/fuse/file.txt","w+");
to
fp=fopen("data1/fuse/file.txt","w+"); /* remove the first backslash */

Related

cant write text file in c

Im trying to learn c, I'm using tutorialspoint, and the function they give me doesn't do anything on my computer, the function is:
#include <stdio.h>
int main (){
FILE *fp;
fp = fopen("/tmp/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
Am I missing something?
It is good to introduce some error checking with file streams
Do
fp = fopen("test.txt", "w+");
/*
* Try creating the file in the same folder for a start
*/
if(fp!=NULL)
{
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
}
else
{
/* There are multiple reasons you can't open a file like :
* You don't have permission to open it
* A parent directory doesn't exist and so on.
*/
printf("Can't open the file for write\n");
}
fclose(fp);
It creates a new file test.txt in /tmp directory and writes two lines using two different functions. Try find test.txt inside /tmp folder.
fopen() won't create directories for you.
You need to create a tmp folder at the root of your current disk before running this program.
First you need to create the temp directory from where you executing this code because fopen is not creating the directory so,than after you need to check following code:
#include <stdio.h>
int main ()
{
FILE *fp;
fp = fopen("/tmp/test.txt", "w+");
if(fp == NULL)
{
printf("Usage Message: File is not open temp/test.txt");
}
else
{
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
}
Also remember that when you dealing with file operation always you have to check your file is opened/create or not using Usage message. Actually it is good sign of programming.
I know this is a bit late, but I found the code work with the following on my PC...
instead of "/tmp/test.txt", make it "tmp/test.txt".
Yes, just remove the "/".

Can not open files with c

Im supposed to write a program that opens an excel file, reads the numbers on the file, multiplies them by 9.8 and the shows the answer in another excel gile.
I wrote this, and I did not get any errors in the compiler, but when I run it, it does not open any files. How do I make it open the files?
#include <stdio.h>
int main() {
FILE *archivo;
FILE *archivoSalida;
int masa;
float peso;
archivo = fopen("C:/Users/nacho/Documents/UNAM/Informatica/proyecto/archivoEntrada.txt", "r");
archivoSalida = fopen("C:/Users/nacho/Documents/UNAM/Informatica/proyecto/archivoSalida.txt", "r");
if (archivo != NULL)
{
printf("The file was opened succesully");
while (fscanf(archivo,"%d", &masa)!= EOF)
{
peso=masa*9.81;
fprintf(archivoSalida, "%f\n", peso);
}
}
else
{
printf ("Error");
}
fclose(archivo);
fclose(archivoSalida);
return 0;
}
You'll want to fopen the output file ("archivoSalida") with mode "w" (for write) instead of "r" (for read). See e.g. http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html.
You do check if the input file could be opened (if (archivo != NULL)). Why don't you do the same for the output file?
Upon an error, you should output which error occured from errno, e.g. via perror(...). That should help in finding the actual problem.
Your file denominated by archivoSalida is opened in read mode ('r').
You should also check the return codes of read/writes functions to be sure everything happen as wanted.
The file names look Windows-ish. Is it possible that all of the forward slashes (/) that you have in both file names should really be back slashes (\)?

Writing the correct path in fprintf function

Apparently, there's no data regarding my question (I tried searching it out here but none of the threads I've read answered my doubt). Here it is: I'm trying desperately to figure out how can I put a correct path into the fprintf function and none of my tries have been successful. Here's the program:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fp = NULL;
//opening the file
fp = fopen("C:/Users/User1/Desktop/myfile.txt", "w+");
//if there's an error when opening the file, the program shuts down
if(fp == NULL){
printf("error");
exit(EXIT_FAILURE);
}
//print something on the file the program just opened (or created if not already existent)
fprintf(fp, "to C or not to C, that is the question");
//closing the file
fclose(fp);
//end of main function
return 0;
}
My question is: why my program always shuts down? What am I doing wrong? It's just a Windows problem (I saw that, on the User1 folder icon, there's a lock, could be a permission denied thing?) or I'm just putting the path in an incorrect way? I tried to use a string to save the path, I tried to change the opening mode, I even tried to disable all the antiviruses, antimalwares and firewalls I have installed on my computer but nothing, the program still doesn't create the file where I want it.
P.S. Sorry for bad English.
P.P.S. Sorry if a similar question has been already posted, I didn't manage to find it.
fp = fopen("C:\Users\User1\Desktop\myfile.txt", "w+");
The character \ is the escape character in C. You must escape it:
fp = fopen("C:\\Users\\User1\\Desktop\\myfile.txt", "w+");
Even better, windows now supports the / directory separator. So you can write:
fp = fopen("C:/Users/User1/Desktop/myfile.txt", "w+");
With no need to escape the path.
Reference:
MSDN fopen, specifically the Remaks section
Use perror() to have the Operating System help you determine the cause of failure.
#define FILENAME "C:/Users/User1/Desktop/myfile.txt"
fp = fopen(FILENAME, "w+");
// report and shut down on error
if (fp == NULL) {
perror(FILENAME);
exit(EXIT_FAILURE);
}

How to create a file on Ubuntu with C File I/O

I have a simple question if anyone can answer it. I'm currently starting off with Ubuntu and am learning the environment though I'm use to using Windows. On Windows if I wanted to create and write to a file I would do:
FILE *fp;
fp=fopen("c:\\test.txt", "w");
fprintf(fp, "Testing...\n");
fclose(fp);
When using Ubuntu what should I put in the parameters of fopen(), say I want to have the file created on my desktop. My computer's name is "root1". Sorry if this seems like a stupid question.
FILE *fp = NULL;
fp = fopen("c:\\test.txt", "w");
if( fp != NULL ){
fprintf(fp, "Testing...\n");
fclose(fp);
} else{
perror("Could not open the file");
}
Error checking should always be done and pointers should not be left uninitialized.
in simple way of u want to take path of any file in Ubuntu then just right click and select properties ...
then it shows path copy it up and append '/filename.extension' thats it

Trouble testing copy file function in C

Okay so this is probably has an easy solution, but after a bit of searching and testing I remain confused.. :(
Here is a snippet of the code that I have written:
int main(int argc, char *argv[]){
int test;
test = copyTheFile("test.txt", "testdir");
if(test == 1)
printf("something went wrong");
if(test == 0)
printf("copydone");
return 0;
}
int copyTheFile(char *sourcePath, char *destinationPath){
FILE *fin = fopen(sourcePath, "r");
FILE *fout = fopen(destinationPath, "w");
if(fin != NULL && fout != NULL){
char buffer[10000];//change to real size using stat()
size_t read, write;
while((read = fread(buffer, 1, sizeof(buffer), fin)) > 0){
write = fwrite(buffer, 1, read, fout);
if(write != read)
return 1;
}//end of while
}// end of if
else{
printf("Something wrong getting the file\n");
return 0;}
if(fin != NULL)
fclose(fin);
if(fout != NULL)
fclose(fout);
return 0;
}
Some quick notes: I am very new to C, programming, and especially file I/O. I looked up the man pages of fopen, fread, and fwrite. After looking at some example code I came up with this. I was trying to just copy a simple text file, and then place it in the destination folder specified by destinationPath.
The folder I want to place the text file into is called testdir, and the file I want to copy is called test.txt.
The arguments I have attempted to use in the copyFile function are:
"test.txt" "testdir"
".../Desktop/project/test.txt" ".../Desktop/project/testdir"
"/Desktop/project/test.txt" "/Desktop/project/testdir"
I just get the print statement "Something wrong getting the file" with every attempt. I am thinking that it may be because 'testdir' is a folder not a file, but then how would I copy to a folder?
Sorry if this a really basic question, I am just having trouble so any advice would be awesome!
Also, if you wanted to be extra helpful, the "copyTheFile" function is supposed to copy the file regardless of format. So like if its a .jpg or something it should copy it. Let me know if any of you guys see a problem with it.
This is with ISO/POSIX/C89/C99 on Linux.
At the start, you'll want to include stdio.h to provide FILE and the I/O function declarations:
#include <stdio.h>
Aside from this, your program compiles and works properly for me. Unfortunately you can't copy to a directory without using stat() to detect if the destination is a directory, and if so, appending a file name before opening the file.
Some other minor suggestions:
A buffer with a power of two bytes such as 4096 is probably more efficient due to it lining up with filesystem and disk access patterns
Conventionally, C functions that return a status code use 0 for success and other values such as 1 for failure, so swapping your return values may be less confusing
When a standard library function such as fopen, fread or fwrite fails, it is a good idea to use perror(NULL); or perror("error prefix"); to report it, which may look something like:
$ ./a.out
...
error prefix: No such file or directory
if you are trying to write a new file in a directory, you should be giving the full path of the file to be written. in your case
"C:...\Desktop\project\testdir\testfile"

Resources