Wrong File String for fopen? - c

I have a text filed called fun on my desktop, but when I pass:
FILE* fp;
if((fp = fopen("/Users/<username>/Desktop/fun", "r")) == NULL)
{
printf("File didn't open\n");
exit(1);
}
fp is null. I have also tried
/home/<username>/Desktop/fun
and many variations, and I still can't seem to get the right file path.I am new to using files and C. Any help would be appreciated.

fopen() can't expand shell keywords.
Change
FILE* fp = fopen("~/Desktop/fun.txt", "r")
to
FILE* fp = fopen("/home/<yourusername>/Desktop/fun.txt", "r")
Characters like '~', '*' are interpreted by the shell and expanded.

You can't use ~ in pathnames to represent the user's home directory. That notation is recognized by shells and some other applications, but it's not part of the Unix filesystem interface. You need to spell out the user's actual home directory.
fopen("/home/username/Desktop/fun.txt", "r")

The ~ in the path is probably the issue. It's your shell that expands that on the command line. fopen doesn't invoke a shell to do substitutions on the path, you'll need to do that yourself.
So pass a complete (relative or absolute) path to fopen, not something that requires shell expansions (~, globbing patterns or shell variables).

You need to expand ~. Use getenv("HOME").
getenv at opengroup even provides some code:
const char *name = "HOME";
char *value;
value = getenv(name);

/*===exphome===([o]i)==================================================
* if SIn is not NULL then
* if SIn starts with '~'
* then expands $HOME, prepends it to the rest of SIn, and
* stores result in SOut or, if SOut==NULL, in a new
* allocated string and returns it
* else if SOut!=NULL
* then copies SIn into SOut and returns SOut
* else returns duplicated SIn
* else returns NULL
=*===================================================================*/
char *exphome(char *SOut, char *SIn)
{char *Rt= NULL;
char *envhome= NULL;
if(SIn)
if(*SIn=='~' && (envhome=getenv("HOME")))
{Rt= malloc(strlen(SIn)+strlen(envhome)+1);
strcpy(Rt, envhome); strcat(Rt, SIn+1);
if(SOut) {strcpy(SOut, Rt); free(Rt); Rt= SOut;}
}
else if(SOut) {strcpy(SOut, SIn); Rt= SOut;}
else Rt= strdup(SIn);
return Rt;
} /*exphome*/
and then
fopen(exphome(NULL, yourfile), ...)

It looks like the answers have prompted edits of original problem. However, as it is currently written there is NO extension on the file name? Is this really true? or does the file end in "*.txt" etc.?

Double-check that you have the correct full file path. Go to the file, right-click on it and select "properties". Are you entering in the path exactly as it is shown, including any suffixes? I.e. if the file is called "file.txt", make sure you include the ".txt" suffix in your code.

Related

Opening a file with a string

I'm working on a problem that requires me to adjust the name of a file and then create a new file using the adjusted name. I'm storing the adjusted name in an array called nameHolder[]. I'd like to use nameHolder, which contains "file.txt" as the name of the new file. The code I have is the following:
void createNewFile(char nameHolder[])
{
FILE* myNewFile = fopen(nameHolder, "r");
fprintf("****************%s******************", nameHolder);
fclose(myNewFile);
}
I get NULL for myNewFile and I believe this is due to "file.txt" not existing in the directory, but the problem requires that I create an entirely new file that doesn't already exist.
From the man page:
RETURN VALUE
Upon successful completion fopen(), fdopen(), and freopen()
return a FILE pointer. Otherwise, NULL is returned and errno is set to indicate the error.
For a file to be read, it should exist, which doesn't in your case. fopen returns NULL on failure, you should check for it.
Unrelated:
The prototype of fprintf is:
int fprintf(FILE *restrict stream, const char *restrict format, ...);
The first argument should be a FILE *, remedy it.
You create a file, truncate it present like this:
FILE *myNewFile = fopen(nameHolder, "w");
Otherwise you want the mode "a" (or "a+").

C fopen filename parameter as a dynamic variable

I want to have the function fopen's filename parameter as a dynamic variable that takes in a ftp client input for my ftp server. I have tried numerous different ways both on this forum and on google but "filename" will still not be recognized by fopen.
else if (strncmp(client->input, "retr", 4) == 0) {
char fname[1024];
// COMMAND LINE: retr filePATHNAME thats why +5
strcpy(fname, client->input+5);
if(fopen(fname, "r") != NULL) {
...
If I put fopen("/pub/test.txt" , "r"), it works so it has to do with something with spaces or quotations or type.
But if i try to do it on the client command line with retr /pub/test.txt or even retr "/pub/test.txt" fopen does not work and will not open the file.
Been stuck on this for the longest time, any help is appreciated.
Thanks
I found the answer.
Thanks to #user3386109 for the hint.
I basically had to clear all the spaces, new lines for the input
strtok(client->input+5,"\r\n\t");

C language parse file's detail

I'm a beginner with C.
I want to parse all the source code (e.g., *.c, *.h) under a directory.
I want to know the file name, size, how many lines in the source.
After searching, I can parse one file's detail (to get how many lines in the source). I can also use system() to know the size and file name, or file list in the directory (without size).
But I have no idea about how to combine all these into one program, so I'm looking for guidance on that.
Thanks all!
below is my code for now
have no idea about next step..
int main (void){
DIR *dp;
FILE *fp;
struct dirent *ep;
dp = opendir ("./");
fp = fopen ("output.txt", "w");
if (dp != NULL)
{
while (ep = readdir (dp))
fprintf(fp,"%s\n", ep->d_name);
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
return 0;
}
Here's the things you need to look in to:
how to iterate over file names, such as with opendir(), readdir() and so on, including while statement for the actual iteration of course.
how to get file details, such as with fstat().
how to open and read files, such as with fopen(), fgetchar() and fclose(), including how to recognise line end characters with if.
That should be the tools you need to start the job, I'd suggest looking in to them then trying to construct your program. Specific problems with the program can then be bought to our attention in other questions.
Note that the examples given above (specifically those in the opendir bullet point) may be platform-specific. If they're not available, you'll need to find equivalents for your platform since standard C does not provide that functionality.

How to discover which files exist in a folder

I have written a program, where it takes an input file, does some operations on it and gives its corresponding output file. i.e., for inp1.txt output is out1.txt, for inp2.txt output is out2.txt and so on, both in different folders.
Right now I have used a file_count variable and have used switch case method, to open the particular file.
The problem is, if I add one more file to the folder, then I have to re-edit the program with another case statement.
Please suggest me the usage of directory pointer in , I browsed all over the net but didnt get an exact solution.
Thanks a lot in anticipation.
There's no way to read the contents of a directory using only standard C APIs, so you'll have to use platform-specific APIs instead:
On *nix systems, you use opendir(3)/readdir(3)/closedir(3) to read the contents of a directory.
On Windows systems, you use FindFirstFile/FindNextFile/FindClose
If you know the file names in advance ie. they follow a pattern like this: f1.txt f2.txt fn.txt then you can loop over the files:
for (int i = 0 -> num_files)
char * filename;
filename = create_your_filename(i)
open(filename)
dostuff
close(filename)
you can follow the same pattern or even edit the filename for the output files.
Otherwise you can also call the program with all the input files in the command:
your_program *.txt
Then all of the file names will appear in argv[], which you can iterate over.
you have to find the files in input folder in runtime before processing. you can sort the files by extension,file name,created date etc...
Here is a simple function that show you if a file (in parameters) is in the path you put in argument.
Return 0 if file exit, 1 if not.
int is_file_enabled(char *path, char *filename)
{
char exec[255], line[255];
sprintf(exec, "ls %s | grep \"%s\"", path, filename);
FILE* cmd_res = popen(exec, "r");
if (cmd_res != NULL)
while (fgets(line, sizeof(line), cmd_res) != NULL)
if (line != NULL) { pclose(cmd_res); return 0; }
pclose(cmd_res);
return 1;
}

C - fopen invalid file

I am wondering, how to check if I am opening file which exists with fopen? I want to diplay some message, when user selects file with bad name. Is must be some simple checking, but I am not able to solve it.
Thanks
in your param list:
FILE pFile ;
then:
pFile = fopen ("myfile.txt","r");
if (pFile == NULL)
printf("No Such File !! ");
When fopen fails, it returns NULL and sets errno to indicate the type of error.
Check the return value, and if it's NULL check errno. You can use functions like perror or strerror to display simple messages about those errors.
To make it even clearer:
f = fopen("some-file-name.ext", "r");
if (f == NULL) reporterror();
But, probably you don't want to use fopen for checking existence and access right. You should look at stat and access. Both available in C libraries and using man
See the possible errors for open:
However, I think you'll have a hard time finding a way to determine that a filename was invalid. On most systems (except Windows) any string that's not overly long is potentially valid (modulo / being interpreted as a path separator).
fopen() function opens the file whose name is specified in the parameter filename and associates it with a stream that can be identified in future operations by the FILE pointer returned.
FILE *try_to_open = fopen("Document.txt", "r"); //READ ONLY
If the file is successfully opened, function returns a pointer to a FILE object that can be used to identify the stream on future operations; otherwise, a null pointer is returned. So, if you want to check that file has been opened correctly, just check that pointer is not null, in this way:
if (try_to_open == NULL){
puts("Opening file failed.");
} else {
puts("File opened successfully.");
It's simple: the returned FILE* pointer will be null if file doesn't exists.
Of course this assumes you are opening it in r, read mode.

Resources