I can't open a directory

I have to develop two programs to manipulate information on text files. And I have to use pipes. The server has to read a directory (supplied by user from cmd) that selects files with .txt extension. The second process "client" will read the files passed from the server process (via the pipe).

This are mi programs

CLIENT

#include
#include


const int pipesize = 1024;
const char*pipe="\\.\pipe\mypipe";

int main()
{
HANDLE Pipe;
DWORD written, read, lasterror, count;
char buffer[pipesize];
BOOL connected;

for(count =0;;++count)
{
if((Pipe = CreateNamedPipe(pipe,PIPE_ACCESS_DUPLEX,PIPE_TYPE_MESSAGE|
PIPE_READMODE_MESSAGE|PIPE_WAIT,PIPE_UNLIMITED_INSTANCES,pipesize,pipesize,
0,NULL)) = INVALID_HANDLE_VALUE)

{
cout<<"CreateNamedPipe failed!
";
ExitProcess(1);
}
else
{

cout<<"CreateNamedPipe successful Handle ="<<Pipe<<endl;
}

connected= ConnectNamedPipe(Pipe, NULL)?TRUE:((lasterror=GetLastError())
==ERROR_PIPE_CONNECTED);
if(lasterror==ERROR_CALL_NOT_IMPLEMENTED)
{
cout<<"Are you using Windows 9X?
No named pipes here!
";
ExitProcess(1);
}

if(connected)
{
cout<<buffer<<"Hello"<<count<<endl;
cout<<"Pipe connected"<<count<<endl;
WriteFile(Pipe, buffer,lstrlen(buffer),&written, NULL);
cout<<"Iteration:"<<count<<"bytes written"<<count<<written<<endl;
FlushFileBuffers(Pipe);
DisconnectNamedPipe(Pipe);
CloseHandle(Pipe);
}

}
}


SERVER

#include <iostream>
#include
#include
#include
#include
#include
#include
#include
#include
#include


int i;
fstream reportbuffer;
const int pipesize = 2048*5;
const char *pipe = "\\.\pipe\mypipe";
HANDLE Pipe;
DWORD written, read, lasterror, count;
char buffer[pipesize];
BOOL connected;
const int Maxchars = 100;
const char *Path;
HANDLE semhandle, filehandle;



void main(int argc,char *argv[])
{

Path = "c:\bc5\bin\clientprocess.exe";
DIR *directory;
struct dirent *ent;
string filename;
reportbuffer.open("ReportBuffer.txt", ios::app);
for(;;)
{
if((Pipe = CreateNamedPipe(pipe, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE|
PIPE_READMODE_MESSAGE|PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, pipesize, pipesize,
0, NULL))== INVALID_HANDLE_VALUE)
{
cout<<"CreateNamePipe failed!
";
ExitProcess(1);
}
else
{
for(i =1; i < argc; i++)
{
char *dirname = argv[i];
if((directory = opendir(dirname))==NULL)
{
cout<<"Unable to open directory"<<endl;
exit(1);
}
while((ent = readdir(directory))!= NULL)
{
filename = ent->d_name;
if(filename.contains(".txt")||filename.contains(".TXT"))
{
strcat(buffer, dirname);
strcat(buffer, "\");
strcat(buffer, ent->d_name);
strcat(buffer, "
");
}
}
reportbuffer<<buffer;
cout<<buffer<<endl;
closedir(directory);
}
reportbuffer.close();
}
spawnle(P_NOWAIT, Path, Path, NULL, NULL);
connected = ConnectNamedPipe(Pipe, NULL)?TRUE: (lasterror=GetLastError()==ERROR_PIPE_CONNECTED);
if(lasterror==ERROR_CALL_NOT_IMPLEMENTED)
{
cout<<"Which version of windows are you using?
No named pipes here!
";
ExitProcess(1);
}

if(connected)
{


WriteFile(Pipe, buffer, lstrlen(buffer), &written, NULL);


cout<<"Number of bytes written: "<<written<<endl;
FlushFileBuffers(Pipe);
DisconnectNamedPipe(Pipe);
CloseHandle(Pipe);

}
/*ReadFile(Pipe, &buffer, pipesize, &read, NULL);
DisconnectNamedPipe(Pipe);
cout<<buffer;*/
exit(0);
//exit(1);
}

}

Comments

  • [blue]what operating system and compiler are you using?[/blue]


  • : [blue]what operating system and compiler are you using?[/blue]
    :
    : Im using win 2000. And borland 5.0
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories