How to read file from local directory path - c

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.

Related

Unable to access file from share drive using fopen in C (windows)

I am taking share drive path as input and adding extra '\' and assigning to a variable.If I use this variable for opening file with "fopen" error is thrown as "No such file or directory".
But If I give the same path (with extra '\' in the path) in the code itself I am able to access.
command: program \\xyz\abc.txt (program_name )
1) My code which doesn't work is like this:
In the program I am making the passed in input path as "\\\\xyz\\abc.txt" by adding extra "\".
then, fopen(var_name,"r"); /* Not working*/
2) Code which works fine:
char arr[100] = "\\\\xyz\\abc.txt"
fopen(arr,"r"); /* works fine */
It seems if the path is known at compile time itself it is working but not at run time.Please suggest what can I do to access path from input not hard coded in program.
You need to escape the backslash in filepath during compile time.. i.e.
"\xyz\abc.txt" but runtime has only one slash.
Piece of code to read input from user:
char filename[50];
FILE *fp;
printf("Enter the filename \n");
gets(filename);
fp = fopen(filename, "r");

Creating a file using fopen()

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

Where to put files so C program can access them?

I'm trying to open files in a C program, but I am unsure where to place the files I want to open (as in which directory). Here is the code, but I really just need to know where to place the file I want to open with fopen().
FILE *fileptr;
fileptr = fopen("QuizQuestions.txt", "r");
if (fileptr == NULL) {
printf("Unable to open file.");
}
Any help is appreciated!
If you don't use an absolute pathname in your code, paths are interpreted relative to the working directory of the user when they run the program. So for your program, the user should put the file in their current directory.
The location of the program itself is irrelevant. If you want to get the location of the program, you see this question:
How do I find the location of the executable in C?
You can then concatenate the directory with the filename.
You need to keep files where source code file is placed.Otherwise, you need to give absolute path.

Where is a file placed when it is created like this?

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".

How to open a text file that's not in the same folder?

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?

Resources