![]() |
| | #1 |
| Registered User Join Date: Jun 2009 Location: Melbourne, Australia
Posts: 49
| It seems to be a simple task but one that has taxed my obsolete grey matter to the hilt. I would like call the dos command string dir /b | findstr /c:".RPT" from a C program and return the value as a variable. This variable is a file name of form *.RPT. I would then like to use this variable to open that file for manipulation. (I am using the dos command as the name of the .RPT file can vary depending upon day and date of creation.) Code: system("dir /b | findstr /c:\".RPT\" ");
inputFile = fopen("<file name found by system call>", "r"); //fopen("file name","mode i.e r-read");
I originally used system() as noted above but am aware that there are other alternatives viz. fork(), pipe() etc: What would be the best alternative to use to get the information back to my C program so that I may use it? [But how do I prevent it from being echoed to the screen and also have it so that I may use it as I wish?] |
| Tigers! is offline | |
| | #2 |
| Hat seller extraordinaire Join Date: Apr 2008
Posts: 159
| If you're searching for files, it's usually easier to use functions that search for files rather than having to mess around with string parsing ![]() Code: // you'll need to include <io.h>
struct _finddata_t fileData = {0};
// start a search for all files with an extension of "rpt"
intptr_t searchHandle = _findfirst("*.rpt", &fileData);
// if the search started
if(searchHandle != -1)
{
// print a banner
puts("Found the following files:");
do
{
// print the file name
puts(fileData.name);
}
// look for any more files
while(_findnext(searchHandle, &fileData) == 0);
// stop the search after all have been enumerated
_findclose(searchHandle);
}
else
{
// if we couldn't start a search, print out the reason
perror("Couldn't enumerate files: ");
}
__________________ Desktop Heap Monitor for Vista/7 A solution to Visual Studio 2005/8 runtime problems Getting Kernel Thread Call Stacks Last edited by adeyblue; 06-24-2009 at 08:05 PM. Reason: Abachler style spelling |
| adeyblue is offline | |
| | #3 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> If you're searching for files, it's usually easier to use functions that search for files rather than having to mess around with string parsing That won't recurse a directory, though. There are a few cross-platform libraries that can do this, including boost::filesystem. If you want to parse the output of the command, you'll need to use 'popen' instead of 'system', ie: Code: #include <stdio.h>
int main( void )
{
int
success = 0;
size_t
read = 0;
const size_t
size = 1024;
char
buffer[ size + 1 ];
const char*
command = "dir /b | findstr /c:\".RPT\" ";
FILE*
pipe = popen( command, "r" );
if( pipe )
{
for( ;; )
{
read = fread( buffer, 1, size, pipe );
if( read == 0 )
break;
success = 1;
buffer[ read ] = 0;
printf("Buffer:\n***\n%s\n***\n", buffer );
if( read < size )
break;
}
pclose( pipe );
}
if( !success )
{
fprintf( stderr, "Error: could not not execute command: %s\n", command );
return 1;
}
return 0;
}
|
| Sebastiani is offline | |
| | #4 | |
| Registered User Join Date: Jun 2009 Location: Melbourne, Australia
Posts: 49
| Quote:
Last edited by Tigers!; 06-24-2009 at 10:50 PM. Reason: Left out a ? | |
| Tigers! is offline | |
| | #5 | |
| Registered User Join Date: Jun 2009 Location: Melbourne, Australia
Posts: 49
| Quote:
| |
| Tigers! is offline | |
| | #6 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> Is it buffer that contains the names of the RPT files? Yes, but unless you read everything in one fell swoop, you'll have to assemble it into a secondary buffer before parsing. |
| Sebastiani is offline | |
| | #7 | |
| Hat seller extraordinaire Join Date: Apr 2008
Posts: 159
| Oops good spot, for anybody intested I've fixed it so it does the same recursing as XP's dir does. Quote:
| |
| adeyblue is offline | |
| | #8 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> for anybody intested I've fixed it so it does the same recursing as XP's dir does. Looks like the same code to me?? |
| Sebastiani is offline | |
| | #9 |
| Registered User Join Date: Aug 2006
Posts: 159
| Just to kind of answer the original question, here's what I've done for this in C++ before. I'm not sure if it can be easily switch to C, though: Code: FILE* stream = _popen( "dir", "r" );
std::ostringstream output;
while( !feof( stream ) && !ferror( stream ))
{
char buf[128];
int bytesRead = fread( buf, 1, 128, stream );
output.write( buf, bytesRead );
}
std::string result = output.str();
|
| System_159 is offline | |
| | #10 |
| Registered User Join Date: Jul 2007
Posts: 38
| system_159 what include files do I need to make that work? Doesnt compile for me |
| elmutt is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Question.. | pode | Windows Programming | 12 | 12-19-2004 07:05 PM |
| XP style in DOS ! | skywing | C Programming | 7 | 05-03-2004 07:34 PM |
| Direct Connect between XP and DOS | lightatdawn | Tech Board | 0 | 01-22-2004 11:22 PM |
| Dos differences in Windows NT and Windows XP | Ruchikar | A Brief History of Cprogramming.com | 5 | 09-17-2003 11:36 PM |
| loading DOS app before XP startup: is it even possible? | toaster | A Brief History of Cprogramming.com | 6 | 05-26-2002 04:59 PM |