You are here:

C++/file permissions and C++

Advertisement


Question
hello Mr. Ralph McArdell
I have been using allexperts for quite a while now
and by far you are the greatest expert that I've met.
Thanks to your inspiration and advice I was able to code an online interpreter, though I am not done yet and the coding is extremely poor, I will go on and complete my project. (I've been working on this for the past 8 months!)

Well, here's a question..

#include <stdio.h>
#include <iostream.h>

int main(){
fopen("haha.txt","w");
cout << "Content-type: text/html\n\n";
cout << "<html> cout << " return 0;
}


when i execute the following code online,
the new file that is produced haha.txt's file permission is set as nobody.

is there a way that i can set that permission to the owner of the execution file?

I have looked through stat() and fstat(), but I don't think they can change the permission of a file, I know they can express uid.

thank you..  

Answer
This is not a C++ question. It is an operating system programming question. I assume you are using a UN*X or Linux type OS using the owner, group, world; read, write, execute permission mask style of permissions. Note that different operating systems - and even file systems - have different object access models - the Win32 one for example is totally different based on access control lists (ACLs) for various system objects as well as NTFS file system objects. I am not an expert in these areas and have to look up the details myself.

Also I do not see that your file ever has anything written to - you open a file then write to stdout. You seem to be mixing C and C++ IO library calls - probably not a good idea. Remember fopen returns a pointer to C library FILE type for use with subsequent C library file functions such as fputs, fprintf etc. As you do not save this FILE pointer you cannot write to the file you just opened - certainly _not_ using C++ file IO at least not without some work. Try including <fstream>, creating a std::ofstream and using that object instead of std::cout to insert data into. Refer to your C++ standard library (specifically the IOStream part of the library) reference - you might try an online reference such as that at http://www.cplusplus.com/ref/iostream/.

In fact I think you never bothered to ever compile this code let alone run it:

       cout << "<html> cout << " return 0;

Eh? What is the return doing as part of the output expression to std::cout ?
Methinks you are missing a semi colon and a newline + indentation.

Not only that the _standard_ C++ header file is iostream _not_ iostream.h iostream.h is for old, so called traditional, pre-standard C++ IOStreams. The correct name for cout is std::cout as in standard C++ such entities are part of the std namespace. Sorry if you are lumbered with an ancient compiler and C++ library in which case the code would be correct.

I fixed the code and ran it; it produced the following output on the console:

Content-type: text/html

<html> cout <<

and a 0 length (i.e. empty) file in the filesystem.

Back to the UN*X file permissions. You need to look at the UN*X system API functions relating to file permissions such as setting and checking them. For starters see the man pages for access and chmod (man section 2, as, surprise, surprise, the default is the man page for the chmod shell command in section 1). You might like to visit http://users.actcom.co.il/~choo/lupg/tutorials/handling-files/handling-files.htm (warning! Long URL - parts may have wrapped over more than 1 line) for UN*X related file handling.

I point out that you are probably as able as I am to enter queries into a search engine such as Google.

Hope this moves you forward.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.