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
Related
I want to open file named ex1, ex2, ex3 ...exn etc.
Now when i put the value of n like, n=1, ex1 will be opened
for, n=2, ex2 file will be opened and then I will read or write my c program output array from or into it.
can the name of the file be given as a string?
As I am new with programing please help me to solve this problem.
Normally when you open a file you use the function fopen
fp = fopen ("file.txt", "w+");
if (fp == NULL)
{
exit(1); // Or you can raise some error code and return if this code is in a function.
}
// Process the file
Now in your case, you need to manipulate the filename. So you can take a C string for this.
char filename[10];
// N is set from code above
sprintf(filename,"ex%d",N);
fp = fopen (filename, "w+");
// Further behaviour is same
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c[1000];
FILE *fptr;
if ((fptr = fopen("program.txt", "r")) == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}
// reads text until newline
fscanf(fptr,"%[^\n]", c);
printf("Data from the file:\n%s", c);
fclose(fptr);
return 0;
}
Output is Error! opening file
I have program and txt file in same dir.
How can I direct access to that file?
To diagnose, use the system command to issue a ls or dir depending on your platform. That will tell you where you are running from. Odds are it is a different location than the files you are trying to open.
As suggested in the comment, try replacing printf with perror
if ((fptr = fopen("program.txt", "r")) == NULL)
{
perror("Error");
// Program exits if file pointer returns NULL.
exit(1); // Exiting with a non-zero status.
}
perror prototype is
void perror(const char *str)
where str is the C string containing a custom message to be printed before the error message itself.
However some causes of the of the file not being read are
File is not present in the current working directory. If this is the case, rectifying the path should fix the issue.
The program might not have the permissions to read from the file usually because of a setting related to discretionary access control. Perhaps do a chmod with file?
I made a quick run of your program on TURBOC++ by Borland and it executed without complaining any sort of Warning or Error
As mentioned in the earlier posted answers, you should replace printf by perror
CURRENT REPLACE BY
printf("Error! opening file"); perror("Error! Opening File.");
As in your case of file not found printf("Error! opening file"); will result in :
Error! Opening file.
However in case of perror("Error! Opening File."); if the file program.txt does not exist, something similar to this may be expected as program output
The following error occurred: No such file or directory
The difference is obvious from above explanations.
Regarding your program, I am making an assumption that either your path to the file is wrong or there is some problem with your compiler.
Try to open your file in w+ mode also to ensure that the file exist.
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 "/".
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);
}
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