Opening and Creating files in C Issue? - c

So, I have written a simple program in C which opens a file called prog6_input.txt and generates another file called prog_output.txt. However, the code below works perfectly for Windows but fails whenever I compile on a Mac. (I also assume this is having the same issue with create the file as well)
FILE *ptr_file = fopen("prog6_input.txt", "r");
// We Don't have a input file
if(!ptr_file) {
perror("Error Reading Input file.\n");
return 1;
}
For perror:No Such file or directory
The file, as seen in the screenshot below, is in the same directory. This works in windows, but does not in Mac OSX? Why is this and how would i fix it?
UPDATE: In order to vompile i used make main in the directory using terminal.

You should open up a terminal, cd to your Prog6 directory, and run your executable with ./main.

Related

QEMU-sparc can't open to file

I am pretty new about QEMU. I am working on QEMU emulation for SPARC microcontroller in my virtual machine which is Ubuntu 20.04.3. I would like to read data from .raw file but fopen functions doesn't work.
I am using eclipse for compiler.
Code Example :
FILE *fake;
if((fake = fopen("/home/rtems/Desktop/file.raw","r"))== NULL){
printf("File doesn't created !\n");
}
File path is correct. There is no doubt.
I am always getting same error even if I can try to open file for write.
Do I need to change something for file access or do I need to convert my raw binary file to another format to access?

Use fopen independently on the current directory

Let's say I am in 2 differents directory:
On the one hand:
print_text_from_file.c
text_to_be_printed.txt
On the other hand:
src/
print_text_from_file.c
text_to_be_printed.txt
And the source code from the .c:
// can't use argv
int main(void){
FILE *fp = fopen ("text_to_be_printed.txt", "r");
if (fp != NULL) {
// working function which takes fp and prints it as expected
print_from_file(fp);
fclose (fp);
}
else {
printf("The file doesn't exist\n");
}
return 0;
}
And let's say, the file only contains Hello World !
If I compile it
After compiling it from being in the first directory with gcc print_text_from_file.c
and after executing it with ./a.out, the output is:
Hello world ! (exactly what I want)
However if I'm in the second case, I'll compile being on src : gcc print_text_from_file.c
and then execute it with being out of src: ./src/a.out, the output is:
The file doesn't exist.
So basically the challenge is that I can't use argv AND I can't know from where the user will execute the file (so I don't know if using absolute paths would be correct). From anywhere, the program will have to print Hello World !.
You really should read something like Advanced Linux Programming and something about operating systems.
And you might also look into syscalls(2) and credentials(7)
The important notions are working directory, process, glob, unix shell, and PATH
See also chdir(2), path_resolution(7), glob(7), getcwd(3), exec(3), proc(5)
There are several issues here:
gcc generates the a.out file at the working directory. It does not matter whether your build print_text_from_file.c or src/print_text_from_file.c, the a.out file is created at the same place, at the working directory (not at src directory). When you run src/a.out, I don't know what did you actually run, maybe an old artifact.
The file opened by the program calling fopen is relative to the working directory. If you want to open the file src/text_to_be_printed.txt you should give path which is relative to the working directory (i.e. src/text_to_be_printed.txt).
Alternative way is to give the full path of the file. e.g. something like /home/myuser/src/text_to_be_printed.txt; this way, the program will find the input file, not as relative to the working directory but as absolute path.
In order to enable the user to give you the path, you shall change the main function signature to get arguments from the user. It should be: int main(int ac, char **av). The user shall run the command like this ./a.out INPUT_FILE_PATH, where INPUT_FILE_PATH is either relative to the working directory or absolute path.
You can access INPUT_FILE_PATH within your code as av[1]. Code will look like this:
int main(int ac, char **av){
const char *input_path = av[1];
FILE *fp = fopen (input_path, "r");
...

Daemon process unable to find file (specified via relative path)

I have a daemon process that spawns several threads one of which handles HTTP requests. The handler is intended to return a file located in
resources/html/index.html
I have the following code:
void * read_file_ex(char *file_name, int32_t *data_len) {
FILE *fp;
fp = fopen(file_name, "r");
... // more code to fetch file contents
fclose(fp);
}
void * read_file(char *file_name){
return read_file_ex(file_name, NULL);
}
And in the thread, I call:
read_file("resources/html/index.html");
The code crashes with a "Segmentation Fault" error when a request is made for that file.
When I use GDB to break at fopen, I notice that a NULL is returned and errno is set to 2 (File not found).
Also, when I change the code to use the absolute path of the file:
/usr/sbin/app/resources/html/index.html
then `fopen()' is able to find the index file and everything works fine.
Another thing to mention is that this error happens when run on Debian Linux but not on Ubuntu 12.04 which makes my question look even dumber.
I forgot to add that I am running the program from the same folder that contains the `resources' folder.
If the current directory of the process is not /usr/sbin/app (and it seems a bit unlikely that the current directory would be /usr/bin/app), then the relative pathname won't work. You should always check the return result from fopen() before attempting to use it. There are endless reasons why an open operation can fail even if you're in the correct directory, let alone when there's a chance that you aren't.
Note that if your process uses functions like daemon(), or is run via a daemonize program, the current directory can be changed to / even if you expected it to be somewhere else.
If you need to check the current directory of the process (a process has a single current directory common to all threads), you can use the getcwd() to get the current working directory.
If you need to change directory (again) after daemonizing your process, you can use chdir() to do so. There's also fchdir() which can be used to change back to a directory if you have an open file descriptor for the directory.

Error opening file

RESOLVED. Problem -
The lecturer uploaded a text file called file.txt and this resulted in a file "file.txt.txt"... I am feeling a mix of frustration and stupidity right now.
Original problem
I'm having trouble with C using Visual Studio 2012 on Windows 7 trying to open a text file using fopen. I'm not too sure which directory this file.txt should be in so I tried placing it with the .vcxproj file AND the .exe file which is in the Debug directory created by VS.
With no success, I tried including the full path to the file in the fopen function.
This code compiles fine but when I run it, I get an error saying "No such file or directory"
What am I doing wrong and how can I fix it? I'm really confused here and any help would be most welcome! Thanks in advance.
Code below:
int main(void)
{
FILE *fp;
fp = fopen("C:\\Directory\\file.txt", "r");
if (fp == NULL)
{
perror("Error opening file\n");
}
return 0;
}
Do you really have any file at this place "C:\Directory\file.txt" I guess you do not have one.
I tried the code and it runs perfectly fine for me. Initially I was getting the same error and that was because the file was not there. Once I put the file there, it all worked perfectly as expected.
Please check again that the file is in place.
You should include the proper header for fopen(), which is
#include <stdio.h>
Make sure all the backslashes are really escaped (doubled) in your filename, too.

Reference a file in C static library

I created a static library in C using Visual Studio. This library contains a function which accesses a text file stored in that current directory. The library was built properly. But the problem is that when I call the function from outside other project it is not loading that text file( I linked the .lib file properly everything else is working except for loading of that file).
Any ideas how to load a text file from .lib file just by relative path??
Thanks in advance..
The following is the library test function definition
int test()
{
FILE *fp = fopen("hello.txt", "r");
if(!fp) printf("File Error");
return 0;
}
The test.lib file is built and created for this.
Just accessing the current folder hello.txt file but when this function is called from other Project. it is saying File Error.
Modify your code to look at the errno:
#include <errno.h>
#include <string.h>
...
if(!fp) printf("File error: %s\n", strerror(errno));
And then look up the meaning of the errno on your operating system to see what's going on.
I'm pretty sure the fact that you're calling this function from a library is a red herring.
What's most likely happening is that your hello.txt file is not in the working directory of the executing process. Go ahead and #include <windows.h> in your project, and use the GetCurrentDirectory function to see what the working directory is when you run your program. Most likely, it's not the same path as your text file.
To remedy this, you can do one of two things: you can change the startup settings of the program (whether that's from Visual Studio or a Windows shortcut) to specify the working directory (called "Start in:" for a Windows shortcut) to be the path to the text file you want to open, or you can figure out what working directory your program has been using and move your text file there instead.
Edit: Also, if you want the application to use its own directory (where the executable file actually resides) you can use the GetModuleFileName function to get the full path of the executable. Of course, you'll have to trim the filename of the program off the end of the string it produces, but that should be a piece of cake.
Check your file path and print an errno, I think you have a static file path

Resources