GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Nov-2005, 12:54
rff1 rff1 is offline
New Member
 
Join Date: Nov 2005
Posts: 12
rff1 is on a distinguished road

accessing different file location using C++


I am currently working on a project for a research project where I am attempting to view the contents of specific folders in a command line-eqse prompt. My problem is that I cannot figure out how to program in C++ the locations I am hoping to view. Any help at all would be great. Thanks!
  #2  
Old 02-Nov-2005, 16:54
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: accessing different file location using C++


Hi rff1,

You can use the system statement to execute dos commands.
You can find more information about the system statement in this link:
system

Regarding viewing the contents of a specific folder, i thought of a simple idea:
using the system command, you can type
dir d:/samples > contents.txt

Where dir d: will return the contents of the samples directory in d:

Then we are redirecting the output to the file contents.txt.
Now, the contents.txt will contain the output of the dos command dir d:/samples.
So, you can now search the file as required using the file i/o in C/C++.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 02-Nov-2005, 17:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: accessing different file location using C++


Quote:
Originally Posted by rff1
I am currently working on a project for a research project where I am attempting to view the contents of specific folders in a command line-eqse prompt. My problem is that I cannot figure out how to program in C++ the locations I am hoping to view. Any help at all would be great. Thanks!

What operating system? What compiler?

The C language (and by extension C++) are carefully designed to be Operating System agnostic. Any Operating System dependencies are handled by functions that are not part of the language and also are not part of standard library specifications.

Since access to file systems, directories (folders) and files is necessarily operating-system dependent, the functions to perform such tasks as viewing contents of folders are not in the Standard C (or Standard C++) libraries.

Linux and UNIX-like operating systems usually have compilers that do things one way, and many compilers for Microsoft Windows operating systems do them another (that is, with a different set of functions and different ways of specifying paths). Up until recent releases of the Macintosh operating system, those guys were on a completely different path (a different planet, actually).

Even for the same platform, Windows, for example, different compilers may have different functions and different header files to allow programmer access to the functions.

So: the directions to get from "here" (not knowing) to "there" (knowing), depend on where "here" is.

Regards,

Dave
  #4  
Old 02-Nov-2005, 18:52
rff1 rff1 is offline
New Member
 
Join Date: Nov 2005
Posts: 12
rff1 is on a distinguished road

Re: accessing different file location using C++


I'm am using MS Visual Studio and programming for windows XP. Most of my research led me to using the FileCreate command, but I am not sure that will work. See my goal is to enter in my program on a command line like so: myprogram -log which would "ideally" list the logs files for a certain IM client. It is probably easy than I think it is, but I am just starting on learning C++, because this project needs to be written in it.

Thanks for all the help this far!
  #5  
Old 03-Nov-2005, 08:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: accessing different file location using C++


Quote:
Originally Posted by rff1
I'm am using MS Visual Studio and programming for windows XP. Most of my research led me to using the FileCreate command, but I am not sure that will work. See my goal is to enter in my program on a command line like so: myprogram -log which would "ideally" list the logs files for a certain IM client. It is probably easy than I think it is, but I am just starting on learning C++, because this project needs to be written in it.

Thanks for all the help this far!

For Windows-specific information, I always go to the motherlode: Microsoft MSDN


Now, as is true for many trips, getting there is half the fun. The other half is trying to find useful information once you get there.

If you enter "Listing files in a directory" into the MSDN search box there are lots of hits. Wade through the list and when you get to item number 32 you see this link:

http://msdn.microsoft.com/library/de..._directory.asp

Note that if you had entered "Listing the files in a directory" in the search box, that would have been the first hit. Go figure.

Anyhow if you go to that link, you get a command line C program that ---big surprise here--- allows you to enter a command line argument and the program will list the files in that directory.

Now, I said that I consider Microsoft to be the motherlode; I didn't say that Microsoft is perfect.

As a matter of fact, that program has a bug: If you don't give a command line argument, the program crashes.

What is the bug?

Here is what it does with command line argument:

CPP / C++ / C Code:

   printf ("Target directory is %s.\n", argv[1]);
   strncpy (DirSpec, argv[1], strlen(argv[1])+1);
   strncat (DirSpec, "\\*", 3);


It didn't to check to see if there is a command line argument. If the user did not enter a command line argument, then argv[1] is NULL (a pointer with a value of zero). If you dereference a NULL pointer (as strlen does when you give it as an argument) the program crashes.

So, if you use this example as a model for your program, make sure that your version checks to see if there was a command line argument. I would suggest that you make the current directory "." the default. So your startup code could look something like:

CPP / C++ / C Code:
   if (argc < 2) {
     strcpy(DirSpec, ".");
   }
   else {
   strncpy (DirSpec, argv[1], strlen(argv[1])+1);
   }
   printf ("Target directory is %s.\n", DirSpec);

What's the lesson here? Actually two lessons:

1. Don't take code from anyone and just assume that it's OK. Not from davekw7x; not from Microsoft; not from anyone

2. Always always check for valid input, whether it is user input, command line input, or file input. Make sure that your program won't crash. Whether it recovers gracefully or just aborts with a gentle reminder to the user is another question. In this case a reasonable action (in my opinion) is just to use the current directory as the argument.

Now the example for which I gave you the link is a C program, but it's also a C++ program. Give it a name like "printdir.cpp" and compile and execute it. I could compile it as a C program and also as a C++ program with Microsoft, Borland and GNU compilers on my Windows XP system.

If you really want the program to look like C++, then change the headers to

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

And go through the program using std::string instead of char[] for DirSpec.

(And of course, cout << instead of printf.)

Regards,

Dave
 
 

Recent GIDBlogReview: Gel laptop cooling pad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 03:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:28.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.