I am using windows 7 64bit OS and DOS box turbo C++. I want to write a simple program to read a text file containing a single integer from E drive of my machine. The file name is t.txt. I have written the following code:
#include <stdio.h>
#include <conio.h>
#include <dir.h>
#include <stdlib.h>
int main(void)
{
FILE *input;
int data;
if ( (input = fopen("E:\\t.txt","r")) == NULL)
printf("Error: Unable to open");
else
{
fscanf(input,"%d",&data);
printf("successfully read in %d",data);
}
fclose(input);
input=NULL;
getch();
}
But this program is unable to access the file and every time it gives an output like:
Error: Unable to open
What is the problem with this code?
Please help.
Yes!!! got it.. Thank you Michael. I tried to mount E: in dosbox and it has run fine.
Problem must be with the usage of file path
Solution given below worked with me in Ubuntu
just try it
char *file = "E:\\t.txt";
FILE *fp = fopen(file, "r");
And verify whether you are using the correct path
Have a good day
Related
First of all, this is my fist stack overflow question, so forgive me if I format this wrong.
I am a beginner at C, and I am up to a point in my book on File i/o. The following code, which is supposed to print the lines to test.txt, doesn't create a new txt file or... do anything.
I am running Code Blocks 16.01 on Windows. Is this code designed for another OS?
#include <stdio.h>
#include <stdlib.h>
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);
}
Ok, so removing the slash makes it work. In the original code, it is 'fopen("/tmp/test.txt", "W+");'
Shouldn't this create the file in folder tmp?
Try removing the front-slash from the file name. You seem to be doing everything properly, the slash might be the problem. If not, let us know.
Edit: When I wrote my comment your fopen used "/test.txt" and not "/temp/test.txt", do you have the "temp" folder created in the directory the application is running from? If not, try creating it. Or remove it altogether and try creating the text file within the directory the application is running from.
Use double // in windows for navigate through directory.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
/*
file path in windows should be like this= C:\\users\\r.maurya\\Desktop\\Downloads\\file.txt
*/
fp = fopen("C:\\users\\r.maurya\\Desktop\\Downloads\\file.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
return 0;//Optional, On success of program
}
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.
I have suppose two text file abc.txt and def.txt in folder "my". I have a programme which directly goes to that folder and search particular file and if that particular file find out then how to access that file's information.
I know how to read write file in C through file handling but I have no idea how to search particular file and after that read that particular file to match particular string in file.
**All these things access through file handling in C.**
So please if any one have any solution I will be thankful for that
Example will be best way to understand .
Thanks in advance
To get a listing of the files in a directory in Linux, you can use the 'opendir', 'readdir' and 'closedir' functions from 'dirent.h'. For example:
#include <dirent.h>
#include <stdio.h>
int ListDir(const char *pDirName)
{
DIR *pDir;
struct dirent *pEntry;
pDir = opendir(pDirName);
if (!pDir)
{
perror("opendir");
return -1;
}
while ((pEntry = readdir(pDir)) != NULL)
{
printf("%s\n", pEntry->d_name);
}
closedir(pDir);
return 0;
}
My first post :), am starting out with C language as basic learning step into programming arena. I am using following code which reads string from text file, makes directory with that string name and opens a file for writing in that created directory. But am not able to create a file inside directory made, here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>
#include <string.h>
int main()
{
char file_name[25], cwd[100];
FILE *fp, *op;
fp = fopen("myfile.txt", "r");
if (fp == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
fgets(file_name, 25, fp);
_mkdir(file_name);
if (_getcwd(cwd,sizeof(cwd)) != 0)
{
fprintf(stdout, "Your dir name: %s\\%s\n", cwd,file_name);
op = fopen("cwd\\file_name\\mynewfile.txt","w");
fclose(op);
}
fclose(fp);
return 0;
}
What you need is to store the file name (with the path) in a c-string before opening. What you are opening is cwd\file_name\mynewfile.txt. I doubt that your directory is named cwd.
A sample could could be:
char file_path[150];
sprintf(file_path, "%s\\%s\\mynewfile.txt", cwd, file_name);
op = fopen(file_path,"w");
use
#include <sys/stat.h>
#include <sys/types.h>
instead of
#include <direct.h>
and modify
op = fopen("cwd\\file_name\\mynewfile.txt","w”);
I see you are using the return values. That is a good start for a beginner. You can refine your error messages by including "errno.h". Instead of printing your own error messages call
printf("%s", strerror(errno));
You get more precise error messages that way.
op = fopen("cwd\\file_name\\mynewfile.txt","w”);
You’re actually passing the string literals “cwd” and “file_name” as part of the path of the file, when I think you actually mean to put the contents of the variables with those names in there. You will probably have to piece together a string for the path. Try looking into strcat()
http://www.cplusplus.com/reference/cstring/strcat/
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;
}