ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi All,
I've encountered a problem when running a simple bit of code, I'm using c to read write data to a binary file. It works fine when I run as root. However when I run as a user it produces the file, however when I try to open / write after it's produced it caused a seg fault. Is there any reason file functions ( fopen etc ) would work for root and not a user and is there a way to fix this?
Yes, you are not permitted to modify this file unless you are using root. If you want to make a file modifiable by a certain user, use the command chown.
For example,
Code:
chown user:group /tmp/file
If you don't know which group to use, type the username again, because typically there is a group with the same name for every user.
For example, if your file is in the path "/tmp/file", and your username is "knobby67", you use this:
Code:
chroot knobby67:knobby67 /tmp/file
You can prevent segfaults (and instead do error reporting) by checking the return value from the fopen function. If it equals NULL, you can print the error with the following code:
Code:
perror("the error is");
Which will print the last error with a prefix of "the error is". In order to use perror, you must include the stdlib.h header.
Hi All,
I've encountered a problem when running a simple bit of code, I'm using c to read write data to a binary file. It works fine when I run as root. However when I run as a user it produces the file, however when I try to open / write after it's produced it caused a seg fault. Is there any reason file functions ( fopen etc ) would work for root and not a user and is there a way to fix this?
Thanks
How did you "produce" the file? Using fopen()? Does your code check to verify that you successfully created the file? If at a later point you attempt to open the file (presumably in another area of your code), did you check to see if the file was successfully opened before assuming it was ok to write to it?
How did you "produce" the file? Using fopen()? Does your code check to verify that you successfully created the file? If at a later point you attempt to open the file (presumably in another area of your code), did you check to see if the file was successfully opened before assuming it was ok to write to it?
yes the file is produced, I can step through code and see it "appear" in the folder. No I don't check when I reopen as the code works fine when I'm root, so if I start with sudo, it's fine. It's only as a user that it seg faults.
Quote:
Originally Posted by Ivshti
use the command chown
Unfortunately I'm producing the file myself inside my C code , so using chown is not really possible, well mybe I could system( but that seems a bit long winded.
yes the file is produced, I can step through code and see it "appear" in the folder. No I don't check when I reopen as the code works fine when I'm root, so if I start with sudo, it's fine. It's only as a user that it seg faults.
Please show the fopen() call that you use in your code. I am most interested in seeing a) the filename, and b) the mode used to open the file.
Most likely, the file is not accessible to you. Therefore, the fopen() call will return NULL.
If you subsequently use that value, without checking that it is null, a protection exception ("seg fault") will occur, because NULL corresponds to an address of $00000000 ... which is always inaccessible (indeed, for this very reason).
Programs must never "assume that all is well." That's one reason why you should never develop software on high-powered accounts. The software must graciously detect the condition and meaningfully report it to the user.
It is quite handy to use languages that will "throw an exception" when something goes wrong, but "C" is an older language that does not, ordinarily, do that.
Last edited by sundialsvcs; 06-24-2011 at 06:47 PM.
It seems some problems with the program while creating the file in user mode,check if you have the permission to create file in the program directory in user mode,if not,just give the permission or excute program under the user home directory.
Please show the fopen() call that you use in your code. I am most interested in seeing a) the filename, and b) the mode used to open the file.
And *MOST* interested in seeing if you check the error status
1. "Permissions" can cause you to fail to open a file.
2. Failing to open a file *cannot* directly cause a segmentation violation.
... However ...
3. Trying to use a file that you failed to open most definitely CAN and WILL cause an access violation
Thanks for the additional information; however it appears that there is still some gray areas.... how is LASTGAMERECALL defined, and how is LastGameList declared?
As for your function, it seems that initially you are checking to see if the file is readable, and hence implying that it exists. If it does not, then you open the file for writing so that you can initialize it with data.
If the file does happen to exist, then you read in all of its data, perform a shuffling of the data, and then re-write that data to the file.
You could have chosen to use stat() to determine if the file exists, and also devise separate functions that read data from the file, write data to the file, initialize the data, and lastly manipulate the data itself. Instead you opted to perform all of these steps (sans stat() call) in one function.
Anyhow, when you have the luxury of time, please answer the questions I posted above.
P.S. The 'b' option used with fopen() is ignored by most POSIX compliant systems, including Linux. It was deprecated long ago.
It is this variable, LastGameList, that I am interested in seeing how it is declared. I can tell it is a global variable (btw, bad programming practice).
In some loops, you access LastGameList using indexes 0 through 10, in others 0 through 9. How many array slots have you declared for LastGameList.
Also, I've seen the following statement in your code; out of curiosity, why is it present?
I am surprised no one has suggested trying to open the file with a known-good tool. Since the OP has created a binary-formatted data file, I suggest od:
Code:
od -tx1 lastgamescorelist
This should clarify whether there is a file permissions error, or error in the OP's code.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.