cannot save file...


 
Thread Tools Search this Thread
Top Forums Programming cannot save file...
# 1  
Old 10-10-2003
cannot save file...

Hello! I have a problem:

I compile some function, which must save file:
=====================
// prog.cpp
void save_file(){
FILE *fs = fopen("file.txt", "w");
if(!fs) cerr<< "cannot save";
fprintf(fs, "This is a text file.");
fclose(fs)
}
=====================
If i compile it from command command line
...root]# g++ prog.cpp
...root]# ./a.out
It starts and saves file normally.
But if i run this function in a programm from as CGI
programm(in Apache) - it doesn't save file! Says "cannot save"...

Maybe it s a problem with permissions? I have neard, that
Apache executes everything in "cgi-bin" as a user "nobody" -
maybe this is a reason? If it is, how can I cnange the permissions
and to what? Is there a function in C++ for permissions changing?
# 2  
Old 10-10-2003
In what directory are you trying to create the file? You can use the tmp directory (it should work with no changes to permissions).
# 3  
Old 10-10-2003
For programs running as CGI scripts, the best bet is to create a directory, say 'data', which is only used to store dynamically-created files, and set the directory permission to 777. Make your program write into a file in that directory, then it should work.
# 4  
Old 10-10-2003
What do you need this file for ? You can use certain facilities such as "cgiwrap" that can change to another user, but it would have to be setuid root. If they are just some temp files, use the /usr/tmp directory. You can also try running apache as another user besides 'nobody'. Create a generic login and store the apache files in that login's home directory. Then, just change the httpd.conf file and change the 'user=' line to specify the new login.
# 5  
Old 10-12-2003
The saving file - is generated
"cpp" file, which must be compiled by this CGI-programm after it is saved.
And user can do it in any folder he wants (no possibility for using 'usr/temp/').
Maybe i have to change the directory's permissions? How to do it in C++?
Is there a function like "chmod()" in C++?
# 6  
Old 10-12-2003
C++ is almost a superset of C. You can call chmod in C++ pretty much like you call it C. Main difference: C++ will demand a function prototype and you can get that from the include file.
# 7  
Old 10-13-2003
Quote:
Originally posted by szzz
The saving file - is generated
"cpp" file, which must be compiled by this CGI-programm after it is saved.
And user can do it in any folder he wants (no possibility for using 'usr/temp/').
Maybe i have to change the directory's permissions? How to do it in C++?
Is there a function like "chmod()" in C++?
You want to allow the user to save the file ANYWHERE they want ? This is not a good idea. Does you web server authenticate the user at all ? If you authenticate the user's identity, you can use a 'cgiwrap' program that can switch to their user id before saving the file. However, this would only give them the save permissions that they would have when logged into that UNIX machine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save value from output of Corestat and save in a list for each core

I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below: Core Utilization CoreId %Usr %Sys %Total ------ ----- ----- ------ 5 4.91 0.01 4.92 6 0.06 ... (0 Replies)
Discussion started by: Zam_1234
0 Replies

2. Shell Programming and Scripting

Opening a file in vi and automatically save and quit this file using shell script

Hi friends, In my shell script, I want to open a file using vi editor. After opening the file in vi, I want to save and quit this file automatically.... all through shell script. the code segment is: ------------------------------------------------------------ cd ~/netfpga/projects/scone/sw/... (2 Replies)
Discussion started by: sachinteotia
2 Replies

3. Shell Programming and Scripting

FILE_ID extraction from file name and save it in CSV file after looping through each folders

FILE_ID extraction from file name and save it in CSV file after looping through each folders My files are located in UNIX Server, i want to extract file_id and file_name from each file .and save it in a CSV file. How do I do that? I have folders in unix environment, directory structure is... (15 Replies)
Discussion started by: princetd001
15 Replies

4. UNIX for Dummies Questions & Answers

CSV file:Find duplicates, save original and duplicate records in a new file

Hi Unix gurus, Maybe it is too much to ask for but please take a moment and help me out. A very humble request to you gurus. I'm new to Unix and I have started learning Unix. I have this project which is way to advanced for me. File format: CSV file File has four columns with no header... (8 Replies)
Discussion started by: arvindosu
8 Replies

5. Shell Programming and Scripting

cut lines from log file and save it another file

Dears, i want cut the lines from a log file. Example of the log file as follows.. May 27, 2011 5:54:51 PM com.huawei.ivas.utilities.sm.client.SMDeliverContrUtil isDeliverSM FINE: May 27, 2011 5:54:51 PM com.huawei.ivas.utilities.sm.client.SMUtil addSysUpMsgLog INFO: . The message content... (1 Reply)
Discussion started by: tonypalokkaran
1 Replies

6. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

7. UNIX for Dummies Questions & Answers

how to save changes to a file

is i want to sort or delete lines etc etc to a file, how do i save the changes. example i do sort file > file when i do cat file, all data is erased. thanks. (2 Replies)
Discussion started by: hobiwhenuknowme
2 Replies

8. Shell Programming and Scripting

Save cURL verbose output to file or do it like browser "save as.."

hi there ! i have exactly the same problem like this guy here https://www.unix.com/shell-programming-scripting/127668-getting-curl-output-verbose-file.html i am not able to save the curl verbose output.. the sollution in this thread (redirecting stderr to a file) does not work for me.... (0 Replies)
Discussion started by: crabmeat
0 Replies

9. Shell Programming and Scripting

Data fetched from text file and save in a csv file

Hi i have wriiten a script which fetches the data from text file, and saves in the output in a text file itself, but i want that the output should save in different columns. I have the output like: For Channel:response_time__24.txt 1547 data points 0.339 0.299 0.448 0.581 7.380 ... (1 Reply)
Discussion started by: rohitkalia
1 Replies

10. UNIX for Advanced & Expert Users

Can't save a file

There is a file A.lcf. I am updating the file and saving it . But as soon as I am logging off and then logging in I am finding that the updated changes are not there in the file. (5 Replies)
Discussion started by: suchismitasuchi
5 Replies
Login or Register to Ask a Question