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

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

Related

Unable to Read file in C

I am a beginner at C and am using DevC++. I have written the code below and wish to read in the data written in input.txt. However when I try to run the code, I always receive the "Cant open file" message. It seems unable to find input.txt, and I am entirely unsure how to change that.
int T;
char command;
FILE *inputfile;
inputfile = fopen("input.txt", "r");
if(inputfile == NULL)
{
printf("Cant open file");
}
fscanf(inputfile, "%d", &T);
It can be because of the following reasons
File you are reading and your source code are not in same directory
You have misspelled the file name
Otherwise your code is correct there should be no problem

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);
}

Create files in fuse using 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 */

Resources