How can I see where the file is created and open it?
#include <stdio.h>
int main ()
{
FILE * pFile;
char sentence [256];
printf ("Enter sentence to append: ");
fgets (sentence,255,stdin);
pFile = fopen ("mylog.txt","w");
fputs (sentence,pFile);
fclose (pFile);
return 0;
}
The file will be created in your current working directory.
Should find it in the directory you run the app from
The file will be create in the same directory the executable program is in.
you can open the same way you create it, changing the options to the fopen,,
FILE *inp;
inp = fopen("FileName","r");
// Do what you want
fclose(inp);
File is created in the same directory as the source code/C File because here you're giving relative path. If you want to put the file somewhere else, you can try giving Full path instead of relative one.
The file should be automatically created in your working directory (where you run the program from). You can open it in any text editor to make sure it was written properly.
If you meant how do you open it in the code, change "w" to "r".
Related
I'm trying to create a file inside a directory, then append some random text inside of the file.
My Code
char dirname[30];
sprintf(dirname, "myroom.%d", (int)getpid());
mkdir(dirname,0777);
char path[path_max+1];
snprintf(path1, PATH_MAX+1, "%s/file1.txt,dirname);
FILE *filedir1 = fopen(path1, "a+");
fclose(filedir1);
char *random_name = { "burger", "toast", "burrito", "noodles" };
int number = rand();
fputs(random_name[number], filedir1];
What I want
(Inside directory "dirname")
When I open file1.txt, I expect there will be either the word burrito, burger, toast, or noodles in the first line.
What I get
file1.txt still empty.
Questions
Anybody know what happen with my code? I saw from youtube video, to append some text into a file, all I need is the fputs command but it doesn't seem to work in my code. Is it because I'm using "a+" in fopen?
Any help will be highly appreciated. Thanks
I think that rand() is creating problem.use rand()%4 instead.
And you are closing the file using fclose() before putting random word. Use it at last.
After opening a file, you are close that file. After closing the file, you are not able to enter the text to that file.
FILE *filedir1 = fopen(path1, "a+");
fclose(filedir1);
Your problem is in this fclose line. You have to do this in after finishing the write operation in that file.
So, remove the fclose(filedir1) and put this line after writing the file.
I am just creating a basic file handling program.
the code is this:
#include <stdio.h>
int main()
{
FILE *p;
p=fopen("D:\\TENLINES.TXT","r");
if(p==0)
{
printf("Error",);
}
fclose(p);
}
This is giving Error, I cannot create files tried reinstalling the compiler and using different locations and names for files but no success.
I am using Windows 7 and compiler is Dev C++ version 5
Change the mode argument in fopen(const char *filename, const char *mode) from:
p=fopen("D:\\TENLINES.TXT","r");//this will not _create_ a file
if(p==0) // ^
To this:
p=fopen("D:\\TENLINES.TXT","w");//this will create a file for writing.
if(p==NULL) // ^ //If the file already exists, it will write over
//existing data.
If you want to add content to an existing file, you can use "a+" for the open mode.
See fopen() (for more open modes, and additional information about the fopen family of functions)
According to tutorial, fopen returns NULL when error occurs. Therefore, you should check if p equals NULL.
Also, in printf("Error",);, omit the comma after string.
Yes you should open the file in write mode.
Which creates the file . Read mode is only to read content
or else you can use "r+" for both read and write.
You should be able to open the file, but you need to make it first. Make a txt document with the name res.txt. It should be able to write your result into the text document.
<?php
$result = $variable1 . $variable2 "=" .$res ."";
echo $result;
$myfile = fopen("res.txt", "a+") or die("nope");
fwrite($myfile, $result);
fclose($myfile)
?>
fopen()
Syntax:
FILE *fp;
fp=fopen(“data.txt”,”r”);
if(fp!=NULL){
//file operations
}
It is necessary to write FILE in the uppercase. The function fopen() will open a file “data.txt”
in read mode.
The fopen() performs the following important task.
It searches the disk for opening the file.
In case the file exists, it loads the file from the disk into memory. If the file is found with huge contents then it loads the file part by part.
If the file does not exist this function returns a NULL. NULL is a macro defined character in the header file “stdio.h”. This indicates that it is unable to open file. There may be following reasons for failure of fopen() functions.
a.When the file is in protected or hidden mode.
b.The file may be used by another program.
It locates a character pointer, which points the first cha
racter of the file. Whenever a file is
opened the character pointer points to the first character of the file
I have written a simple C code in visual C++
I am planning to open a text file for reading , but whenever i enter path it shows "unable to open the file".
then i hardcoded the path in the program itself. still same error , can anyone tell me what I am doing worng ? or where I wil have to copy paste that file so that i can open it in visual c++ through my code ?
here is my code :
#include<stdio.h>
#include<conio.h>
int main(){
FILE *p;
char file1[20];
char ch,i;
printf("\nEnter the source file name to be copied:");
gets(file1);
p=fopen(file1,"r"); // I have tried changing it with actual path to the file
if(p==NULL)
{
printf("cannot open %s",file1);getch();
exit(0);
}
while((i=getc(q))!=EOF)
printf("%c",i);
fclose(p);
return 0;
}
Save one file in your directory where you have saved the program.
And try it typing the perfect full name of the file including format of the file.
Your code seems error free.
When you're telling directory during execution use // only dont use / otherwise it will show you error and file won't open.
it worked after Entering double back slash "\\" instead of single back slash after the Drive name (example "c:\\") for the path to the file.
I have one function which reads file and does the conversion part.
fp=fopen("newfile.txt","r");
Here i have copied this newfile.txt in project file and compiling in VC++ 2008 IDE.It works fine
I would like to read the file from a local drive directory path.is it possible to read the files from local drive.how to mention the path.If so please mention any example.
one more thing If i want to read all the files in that particular folder with out changing the name of text files in the above code. Suggest me any thing to do.
I dont want to change the file name manully in the code
You could use an absolute path to your file:
FILE* fp = fopen("c:\\your_dir\\your_file.txt", "r");
if(fp) {
// do something
fclose(fp);
}
or a relative path, assuming your file is located in c:/etc and your executable is located in c:/etc/executables:
FILE* fp = fopen("..\\your_file.txt", "r");
if(fp) {
// do something
fclose(fp);
}
I think you can use first program argument. It is a string containing path of executable. You can access it by usingint main(int argc, char *args[]) instead of int main(). The args[0] contains what you need. Just take a substring of it, to get the path and concatenate it with your filename.
Since C it's not a language I am used to program with, I don't know how to do this.
I have a project folder where I have all the .c and .h files and a conf folder under which there is a config.txt file to read. How can I open that?
FILE* fp = fopen("/conf/config.txt", "r");
if (fp != NULL)
{
//do stuff
}
else
printf("couldn't open file\n");
I keep getting the error message. Why?
Btw, this only have to work on windows, not linux.
Thanks.
The easy way is to use an absolute path...
my_file = fopen("/path/to/my/file.txt", "r");
Or you can use a relative path. If your executable is in /home/me/bin and your txt file is in /home/me/doc, then your relative path might be something like
my_file = fopen("../doc/my_file.txt", "r");
The important thing to remember in relative paths is that it is relative to the current working directory when the executable is run. So if you used the above relative path, but you were in your /tmp directory and you ran /home/me/bin/myprog, it would try to open /tmp/../doc/my_file.txt (or /doc/my_file.txt) which would probably not exist.
The more robust option would be to take the path to the file as an argument to the program, and pass that as the first argument to fopen. The simplest example would be to just use argv[1] from main's parameters, i.e.
int main(int argc, char **argv)
{
FILE *my_file = fopen(argv[1], "r");
/* ... */
return 0;
}
Of course, you'll want to put in error checking to verify that argc > 2, etc.
You're probably going to want to look into the dirent family of routines for directory traversal.
The location of your .c and .h files is not really the issue; the issue is the current working directory when you run your executable.
Can you not pass in the full path to the fopen() function?