You are here:

C++/problem in c++ programming

Advertisement


Question
QUESTION: Hi,
and thanks for your help about my last prob. U was on vacation and i was waiting for u because only u can give right answers to me.

Now, i have another problem on same program. problem is that how can i open a file by giving its path.
how can i give path show its format.what is its protype.

another one problem is how can i call another program at the end of this program.
plese response as quickly as possible.
Thanks

ANSWER: Firstly, I very much doubt I am the only one to be able to answer such questions as you ask here.

So on to your problems.

If you can open a file then you can open a file on a path. However such a path name is _platform_ _specific_ - thus a MS Windows file path will be different to a UN*X file path which will be different to a Mac OS file path, etc...

If fact that is not quite true. Microsoft operating systems tend to use the backslash ('\') as a path element separator. However they also accept the UN*X style forward slash ('/'). The problem is that the forward slash is the command line command switch symbol on Microsoft systems, so on the command line you have to use the back slash in path names. On newer Microsoft systems you can use the forward slash on path names on the command line if you place the path name in quotes.

The other difference of course is that UN*X like systems have a single directory tree, with volumes mounted in directory paths within this tree and Microsoft systems use different volumes such as C:, D: etc. Microsoft systems can also accept UNC server share paths (please refer to the Microsoft documentation for further information on the exact nature of Microsoft path name grammar).

So no matter what grammar a system uses for its path names, you use that grammar when opening a file - or possibly certain devices. However the backslash used on Microsoft systems can cause problems when quoted using C or C++ character or string literals because in C and C++ this character is the escape character used to insert special character values such as end of line ('\n') etc. So to obtain a backslash character in C or C++ string literal you have to repeat each one - that is '\\' represents a single backslash character in C and C++. Thus a path such as:

   C:\MyDataFiles\DataFile.dat

In C and C++ would need to be quoted thus:

   "C:\\MyDataFiles\\DataFile.dat"

Alternatively, use forward slashes instead:

   "C:/MyDataFiles/DataFile.dat"

Thus you might open such a file like so:

   std::ifstream  in( "C:\\MyDataFiles\\DataFile.dat" );

or

   std::ifstream  in( "C:/MyDataFiles/DataFile.dat" );

On a UN*X like system you might use a path such as:

   /home/myusername/mydata/datafile.dat

And it could be opened like so:

   std::ifstream  in( "/home/myusername/mydata/datafile.dat" );

Of course you can also use other options to open a file on the path, such as using the open operation or the forms of file stream constructor and open that take the additional open mode flags.

Trying to manipulate and validate paths for code intended for multiple platforms can be tricky (at best). Trying to determine which platform format a path is in can also be tricky. I found for example that trying to determine if I had a Windows or Mac OS X path not to be possible in all cases. This is because Mac OS used a colon (':') as a path element separator so a path such as:

   C:name

Could be a path relative to the current C: drive working directory on a MS Windows system or a Mac OS path...

There is some hope however. For example, the Boost libraries (http://www.boost.org/) have a file system library (http://www.boost.org/libs/filesystem/doc/index.htm) which contains a generic grammar for pathnames, used with a path class. There is a proposal to include such support based on this library in the second technical report (TR2) for the C++ standard.

OK so that is the take on path names and file streams.

Now for 'calling' one program from another. The only standard support for this in both C and C++ is the system function (include <cstdlib> for C++ or <stdlib.h> for C). This takes a command line string, starts up a command processor in such a way that is executes the single command represented by the command string it was passed, and returns when the program or command processor command terminates. A common use for students using Visual Studio or similar MS Windows based IDE is to pause the program before it returns so that the user can get a chance to see the results when the program is executed from within the IDE:

   int main()
   {

   // do stuff...

       system("pause"); // wait for user to press a key
   }

Of course this only works on a MS-Windows (or MS-DOS or similar) system, unless other system's command processors (or shells) also have a pause command, or they have a pause program accessible.

Note also that calls to the system function _wait_ for the executed program or command to finish before returning and use a command processor of some sort.

If you wish to start a program then continue processing within your program you have to use implementation or operating system specific features such as the spawn family of functions, a MS Visual C++ runtime library extension, or a UN*X style call to fork (or vfork) followed by a call to one of the exec family of functions (the exec functions are also supported by the MS Visual C++ runtime library by the way), or a call to the Win32 CreateProcess or ShellExecute (or related) functions.

As you give no information of the operating system or compiler you are using I cannot be of more help - consult the documentation for your compiler's runtime library and you operating system programming environment. E.g. check out the MSDN library for Microsoft systems, or use the man pages often installed on UN*X and Linux systems, although not all system call man pages may be installed, if so then try entering the function name into an internet search engine - many standard UN*X/Linux/POSIX functions are documented online.



---------- FOLLOW-UP ----------

QUESTION: I'm using windows XP , compiler C++.

Answer
Thank you for the information on the operating system. I had in fact realised you were using a C++ compiler of some description you fail to mention which one though (maybe you posted the question before reading it thorough <g>).

Hence for more information on Microsoft development topics, I suggest you refer to the MSDN library (online at http://msdn2.microsoft.com/en-us/library/default.aspx) and the MSDN website (at http://msdn2.microsoft.com/),

Specifically for information on Microsoft Windows path grammar see the article at http://msdn2.microsoft.com/en-us/library/aa365247.aspx.

If you are using Visual C++ then you can find information on the Visual C++ runtime library (CRT) at http://msdn2.microsoft.com/en-us/library/59ey50w6(VS.80).aspx (for Visual C++ 2005). Those functions concerned with processes can be found at http://msdn2.microsoft.com/en-us/library/d6dtz42k(VS.80).aspx. Of particular interest should be the _execXXX (or _wexecXXX) functions and the _spawnXXX (or _wspawnXXX) functions (where XXX is one of a number of suffixes).

For raw Win32 API support for starting a program in another process, check out the Win32 Dlls, Processes, and Threads section (see http://msdn2.microsoft.com/en-us/library/ms682584(VS.85).aspx), specifically http://msdn2.microsoft.com/en-us/library/ms684841(VS.85).aspx. Information on ShellExecute in fact counts as part of the Windows Shell as part of the User Interface and can be found in that section (start at http://msdn2.microsoft.com/en-us/library/bb773177(VS.85).aspx, and see http://msdn2.microsoft.com/en-us/library/bb762153(VS.85).aspx for the ShellExecute reference information).

The above links are good at the time of writing (as far as I know) but may well change over time. They should be enough to answer your immediate questions. Remember, as I note in my instructions to questioners, for all Microsoft specific development topics, start with the MSDN website and MSDN library.

I would also obtain a good C++ standard library reference, and possibly a good C standard library reference as well, in the case the C++ library reference does not go into too much detail on the part of the C++ library inherited from C.


Hope this is of use.  

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.