I have a command line app and have the code
chdir("/var");
FILE *scriptFile = fopen("wiki.txt", "w");
fputs("tell application \"Firefox\"\n activate\n",scriptFile);
fclose(scriptFile);
and when I run it in Xcode I get an EXC_BAD_ACCESS when it gets to the first fputs(); call
Probably the call to fopen() failed because you don't have write permissions in /var. In this case fopen() returns NULL and passing NULL to fputs() will cause an access violation.
Are you checking to make sure the file is properly being opened?
Normally you will need superuser privileges to write into /var, so this is likely your problem.
I already answered this in the comment and a couple people have told you what you've done wrong as answers but I decided to add a little sample code with error checking:
chdir("/var");
FILE *scriptFile = fopen("wiki.txt", "w");
if( !scriptFile ) {
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
exit(-1);
} else {
fputs("tell application \"Firefox\"\n activate\n",scriptFile);
fclose(scriptFile);
}
Now you will see an error if your file is not opened and it will describe why (in your case, access denied). You can make this work for testing by either 1) replacing your filename with something world writeable, like "/tmp/wiki.txt"; or 2) running your utility with privileges sudo ./your_command_name.
Related
I'm working on an assignment that involves creating files and manipulating characters within them using what the lecturer describes as "File and System I/O calls" in C.
Specifically, I am using open, creat, read, write, lseek, close, and unlink.
Without revealing too much and violating academic integrity, essentially the assignment just entails creating files, copying characters between them, changing the characters, etc.
All of this was going perfectly until a certain point of the program after which read just... Wouldn't work. It doesn't matter what file I am accessing, or what other calls I put before it- for instance, I have tried closing and re-opening a file before trying to read, writing before reading (worked perfectly, put the characters right at the file position the lseek intended), etc.
I have checked the returns of all previous commands, none are giving errors other than the read, which is giving me errorno 9. Having looked this up, it appears to refer to having an incorrect file descriptor, but this doesn't make sense to me as I can use the same fid for any other command. Using ls -l, I confirmed that I have read and write permission (groups and public do not, if that helps).
I am at a total loss of where to go troubleshooting from here, any help would be immensely appreciated. Here is the code snippet in question:
readStatus = lseek(WWWfid,500,0);
if (readStatus<0) {
printf("error with lseek");
return 0;
}
/*printf("Read status: %i\n", readStatus);/*DEBUG*/
writeStatus = write(WWWfid, "wtf", 3);
if (writeStatus<0) {
printf("error with write");
return 0;
}
readStatus = read(WWWfid, buffer, 26);
int errorNum = errno;
/*buffer[27] = '\0';*/
if (readStatus<0) {
printf("error with read before loop, error %i\n", errorNum);
return 0;
}
I can likely include more without invoking the wroth of my uni but I'd prefer not to- also seems likely to be irrelevant given that all proceeding code appears to be functioning correctly.
Thanks for reading, please let me know if you have any insight at all
So, I tried to find an answer to this question unsuccessfully.
I know what to do and how to manage such a case - by using fluss/NULL etc. afterward. But checking it is tricky to me.
So, basically:
open some file(successfully) - let's call the pointer: file.
after some code running...
fclose(file);
Now, how can I check after(before it's also an option) closing the file - that it really happened?
What is the condition? By demand, I need to handle this case by printing some specific errors.
You can use the following snippet:
#include <errno.h>
if(fclose(file) != 0)
{
fprintf(stderr, "Error closing file: %s", strerror(errno));
}
From the man pages, we see that an error in closing a file using fclose() sets the global variable errno to a value indicating what error occurred. The function strerror() takes this value of errno and outputs a string to help indicate what the error actually was.
My code uses fftw_export_wisdom_to_filename successfully to create a wisdom file which looks like some text wisdom data.
However, when I try to open it, like so:
if (access("p2", R_OK) == 0)
printf("File can be read!\n");
else
printf("File can't be read!\n");
int error = fftw_import_wisdom_from_filename("p2");
printf("Import status: %d\n", error);
it fails:
File can be read!
Import status: 0
What am I doing wrong?
It is clarified in the last paragraph here that the value returned is 1 if the import routine was successful.
I don't know if this is your problem, but make sure the version of FFTW that reads the wisdom file is the same as the one that created it.
I had this problem, and I'm not saying this is the right thing to do, but I just changed the version info in the beginning of the wisdom file to the version of FFTW I'm using. 3.3.6-p12 is what I'm running, but 3.3.8 created the wisdom file. It successfully reads the file and shows a noticeable performance increase versus when it failed to read the file.
I am supposed to write a program that creates new files using the open() command, which, everything I read says that it's supposed to do if a file doesn't already exist.
My code is like this:
char startroom[] = "laruee.rooms/startroom.txt";
//...
int file_descriptor;
//...
file_descriptor = open(startroom, O_WRONLY | O_CREAT );
{
fprintf(stderr, "Could not open %s\n", startroom);
perror("in main");
exit(1);
}
However, despite everything I've googled about this command indicating that the file should get created if it doesn't already exist, the file is not getting created. (And also from everything I googled, it appears that I am the only programmer in the universe having this problem.)
What gives?
Your question could be operating-system (and even file-system) specific. I guess you are running on Linux on some usual file-system like Ext4 or BTRFS.
Read open(2) & path_resolution(7). There are several reasons why an open could fail (and we cannot guess which is relevant for your computer).
It might happen that your code is not running in the conditions you want it to (working directory, user id...)
Then, improve your code as:
char startroom[] = "laruee.rooms/startroom.txt";
//...
int file_descriptor = open(startroom, O_WRONLY | O_CREAT );
if (file_descriptor < 0) {
fprintf(stderr, "Could not open %s : %s\n",
startroom, strerror(errno));
char wdbuf[256];
if (getcwd(wdbuf, sizeof(wdbuf))
fprintf(stderr, "in %s\n", wdbuf);
exit(EXIT_FAILURE);
}
When using perror or strerror on errno you don't want any function which might change errno to be called after the failing syscall. On Linux with glibc the fprintf(3) function knows about %m ....
BTW, you could also strace(1) your program
Perhaps look also in your /var/log/syslog or /var/log/messages. Be sure your disk is not full (use df -h and df -i). If you have disk quotas, be sure to not overflow them. Be sure that your program is running in the directory you want it to and that current directory contains a laruee.rooms/ sub-directory; you might get it with getcwd(2) like I did above.
Particularly for server-like programs, you might want to use syslog(3).
You should read Advanced Linux Programming
BTW your open is not a command (that would be xdg-open) but a system call or at least a POSIX function
As it stands the code looks like this:
fd = open(filename, openflag, perm);
I then call perror on open and receive
open: No error
open: No error
open: No error
open: No error
The final error call is:
die(1, "open_fds: File open error");
Yielding
SIO_ERROR: open_fds: File open error
What can I do to get more meaningful error messages?
Notes: This is for a Windows environment and I'm relatively new to C.
You can use perror or you can use strerror(errno) on Windows and both should work, here's an example of the latter:
int fd = open("some_made_up_file_name.txt", O_RDONLY);
if(fd < 0)
printf("%s\n", strerror(errno));
In this case I'd get:
No such file or directory
Because that file I'm trying to open doesn't exist and I didn't tell it to create in this case. You need to make sure you check the return from open(), < 0 and you have a problem.
As far as I have seen seen, perror works fine in windows also.
Do you know if open did fail (what did it return) & if it did fail, why did it fail in your case? Was the file not there or was it a permissions issue? And why did perror print the "No error" message 4 times?
What is the die function - as far as I know it's not a standard function? What does it do to print an error message?
On windows, you can also use GetLastError to get the error. (other than errno and perror).
You can use FormatMessage to format the error message corresponding to the error code returned by GetLastError.