I have this very simple code:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
FILE * file_ptr = NULL;
file_ptr = fopen ("file.txt", "r");
if (file_ptr == NULL)
{
puts ("Error!");
return EXIT_FAILURE;
}
else
{
puts ("O.k.!");
}
return EXIT_SUCCESS;
}
Output:
Error!
Why fopen doesn't work? The file is not protected, not opened elsewhere and is stored in the same folder as the *.exe of this program. I also tried it with giving the complete path to the file and with an array, in which the filename is stored. Everytime it puts out "Error!".
What's going on??
I'm using Eclipse Neon.2 Release (4.6.2) with newest cygwin gcc compiler on Windows 10 64bit.
Thank you for your help!
The problem was solved by changing the fopen to
file_ptr = fopen("xxyyzzqq.txt", "w");
and then searching the hard drive to see where the file was created.
Turns out that the file was created in the project source directory, and not the debug directory (where the .exe file is), unlike the old installation which used the debug directory as the working directory.
perror might help.
FILE *file_ptr = fopen("file.txt", "r");
if (!file_ptr) {
perror("fopen");
} else {
printf("It's working!");
}
Similar question: fopen() not working in C
Related
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 "/".
This is my code
#include <stdio.h>
int main()
{
FILE *file;
file = fopen("file.txt","a+");
fprintf(file,"%s","test :)");
fclose(file);
return 0;
}
Don't understand why it won't create a txt file
help
Please try perror to check if you have permission to write to the file or not. That is the problem most of the time. Add this after fopen
if (!file)
perror("fopen");
You need to check for errors in your program. fopen() can fail for a variety of reasons. We can either inspect errno, or use perror / strerror to print a useful message.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *file = fopen("file.txt","a+");
if (file == NULL) {
perror("Failed to open the file");
exit(-1);
}
fprintf(file,"%s","test :)");
fclose(file);
return 0;
}
For example, if a file exists in the current directory, but is owned by a different user:
[8:40am][wlynch#watermelon /tmp] ./foo
Failed to open the file: Permission denied
Create a file if one doesn't exist - C
here are answers...The one that's under the marked one worked for me on my s.o. The way you are trying to do doesn't work on windows, but works on linux. Sorry for saying what I said before...Both operating systems have their bright and not so bright side.
FILE * in;
in = fopen("file1.bmp", "rb");
if (in == NULL) {
perror("file1.bmp");
return ;
}
Why is it that I am not able to open the *.bmp file. fopen() is returning NULL.
Can anyone kindly help me how to successfully open a BMP file. Should I use some other C++ function. If yes, plz let me know. An example would be very helpful. I am using VC++ in VS2008.
Thanks in advance.
The file does not exist, or you can't read from it (maybe file permissions)?
You might have a working directory issue. Try opening the file with a fully qualified path.
Also this is a plain C question
#include<stdio.h>
#include <errno.h>
int main()
{
FILE * in;
in = fopen("file1.bmp", "rb");
if (in == NULL) {
perror("file1.bmp");
printf("Error %d \n", errno);
return ;
}
}
using this way see errno and find its meaning from here http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html
I'm trying to make a program to open a file, called "write.txt".
#include <stdio.h>
main() {
FILE *fp;
fp = fopen("write.txt", "w");
return 0;
}
Should this work? Because it returns nothing.
Other than an old variant of main, there's not really much wrong with that code. It should, barring errors, create the file.
However, since you're not checking the return value from fopen, you may get an error of some sort and not know about it.
I'd start with:
#include <stdio.h>
#include <errno.h>
int main (void) {
FILE *fp;
fp = fopen ("write.txt","w");
if (fp == NULL) {
printf ("File not created okay, errno = %d\n", errno);
return 1;
}
//fprintf (fp, "Hello, there.\n"); // if you want something in the file.
fclose (fp);
printf ("File created okay\n");
return 0;
}
If you're adamant that the file isn't being created but the above code says it is, then you may be a victim of the dreaded "IDE is working in a different directory from what you think" syndrome :-)
Some IDEs (such as Visual Studio) will actually run your code while they're in a directory like <solution-name>\bin or <solution-name>\debug. You can find out by putting:
system ("cd"); // for Windows
system ("pwd") // for UNIXy systems
in to your code to see where it's running. That's where a file will be created if you specify a relative path line "write.txt". Otherwise, you can specify an absolute path to ensure it tries to create it at a specific point in the file system.
What did you expect it to 'return' - it opens a file, on most platforms creating one if it doesn't exist.
You should probably fclose(fp) the file at the end.
I think you want to print the contents of file write.txt. (Assume it contains characters).
#include <stdio.h>
int main()
{
FILE *fp,char ch;
fp=fopen("write.txt","r");
if(fp==NULL)
{
printf("Some problem in opening the file");
exit(0);
}
else
{
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
}
fclose(fp);
return 0;
}
I think you should study some more fundamentals in C before you start attempting to work with files. A return means some data is passed back to the calling code from the called function.In this case you return 0 at the end of your program. You did not do anything with your FILE pointer except cause a new file to be created...
I am having some trouble with a basic file opening. I can't successfully initiate the filestream. It keeps coming back NULL... Can anyone tell me what I'm missing? I created a "test.dat" file in the same directory that the source code lives.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
if((cfPtr = fopen("test.dat", "rb+")) == NULL) {
printf("File could not be opened.\n");
}
return 0;
}
Try moving the test.dat file to the directory where the compiled .exe lives, or if the current directory for the application is somewhere else, place the file in that directory.
When you try to open a file, your OS will look in whatever the process current directory is. This may or may not be the same directory as where your source file lives, depending on your OS and/or IDE.
Are you using Visual Studio? You have to put test.dat into the Debug directory. There are two debug directory in newer version, you have to check that yourself.
You could use the errno to get a hint as to what is going wrong:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h> /* new */
int main (void)
{
if((cfPtr = fopen("test.dat", "rb+")) == NULL) {
printf("File could not be opened.\n");
printf("Errno = %d\n, errno) ; /* new */
}
return 0;
}