|
You may have, at least, two problems:
read return value is -1.read return value is positive, but less than 50.
When read returns -1 , you've to check errno to understand what happened (in any case you must NOT write (garbage) values to the output file.
When read returns a positive value less than 50 , you should copy just the read bytes to the output file.
For instance
while ( ! finished )
{
ssize nread = read(fd[0],buf,50);
if ( nread == -1 )
{
}
else if (nread > 0)
{
printf("Read %d characters\n", nread);
buf[nread]='\0';
printf(buf);
write(fileOpen, buf, nread);
}
}
where finished is a termination condition (timeout?) that you possibly know better than me.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: For instance
while ( ! finished )
{
ssize nread = read(fd[0],buf,50);
if ( nread == -1 )
{
// handle error
}
else if (nread > 0)
{
printf("Read %d characters\n", nread);
buf[nread]='\0';
printf(buf);
write(fileOpen, buf, nread);
}
}
I already tried rewriting that part. When i get nread=-1 i print a message, just for the debug. The result of that change is an endless loop of messages.
For some reason, when it gets to the father, the pipe is empty, or some other pipe error. The father just can't read any info from the pipe.
Man, this is frustrating!
CPallini wrote: where finished is a termination condition (timeout?) that you possibly know better than me.
The thing is - the end condition is an empty pipe...
|
|
|
|
|
 Try:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char* argv[])
{
if (argc != 5)
{
printf("Error!\nUsage: %s <input file 1> <input file 2> <input file 3> <output file>\n", argv[0]);
return -1;
}
int fd[2];
int fileOpen;
int i;
char buf[51];
pipe(fd);
ssize_t nread;
int count = 0;
for (i=1; i < 4; i++)
{
switch (fork())
{
case -1:
printf("Error: fork failure.\n");
return -2;
case 0:
close(fd[0]);
printf("Child %d created. File: %s\n", i ,argv[i]);
if ((fileOpen = open(argv[i], O_RDONLY)) == -1)
{
printf("File %s does not exist.\n", argv[i]);
return -3;
}
printf("Child %d writing to pipe\n",i);
while ((nread = read(fileOpen,buf,50)) > 0)
{
buf[nread] = '\0';
printf("Child %d. Text: %s\n", i, buf);
write(fd[1], buf, nread);
usleep(25000*(rand()%8+3));
}
close(fd[1]);
close(fileOpen);
printf("Child %d done writing to pipe\n",i);
return 0;
default:
break;
}
}
close(fd[1]);
if ((fileOpen = open(argv[4],O_WRONLY|O_CREAT)) == -1)
{
printf("Unable to open file %s.\n", argv[4]);
return -4;
}
printf("Father reading from pipe.\n");
while (count < 1000)
{
if ((nread = read(fd[0],buf, 50)) > 0)
{
int i;
printf("Father read line...\n");
buf[nread] = '\0';
printf("\n%s\n",buf);
write(fileOpen, buf, nread);
}
else
{
printf("Father waiting for data...\n");
usleep(10000);
}
count++;
}
close(fd[0]);
close(fileOpen);
printf("Father Done reading!\n");
return 0;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Just to let you know - the code works perfectly.
Thank you very much!
|
|
|
|
|
You are welcome.
Thanks for the feedback.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
SummerBulb wrote: Here is my Code:
int pids[3];
int i;
for (i=1; i < 4; i++)
{
switch ((pids[i] = fork()))
You forgot that C arrays are zero-based.
|
|
|
|
|
Oh, oops. How did that happen?
Anyway, that array is not used later, and iv'e removed it from the code, so that isn't the problem.
Thanks anyway.
|
|
|
|
|
I tried searching in google, but I couldn't find anything good. I'm sure I've seen such forums before, but now that I need them, I can't find them
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
sashoalm wrote: I tried searching in google, but I couldn't find anything good.
Dr. Joseph Newcomer is an SME on driver development. Check out his book and Web site.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
|
Hi All,
Will you please tell me how to make a Windows Service application that have MFC support in VisualStudio 2005?
Regards,
Spk
|
|
|
|
|
|
|
Hi,
Im using C# in VC++ for WPF.
I create one Delegates in C# as follow
public delegate void NameTab(string msg);
public static event NameTab CallClick;
private void Callbutton_Click(object sender, RoutedEventArgs e)
{
String msg = comboBox1.Text;
CallClick(msg);
}
Im using in my VC++ code as follow
Tabcontrol::SecondTab::CallClick += gcnew Tabcontrol::SecondTab::NameTab(OnCallClick);
And OnCallClick fucntion is
static void OnCallClick(CString msg)
{
}
But it shows error
error C3352: 'OnCallClick' : the specified function does not match the delegate type 'void (System::String ^)'
Im using button click event handler and its woking perfectly.But this only shows error.As i want to get the text in combox,im passing that text as parametes and im uisng that text in vc++ code.
pls help me.
Anu
|
|
|
|
|
Anu_Bala wrote: error C3352: 'OnCallClick' : the specified function does not match the delegate type 'void (System::String ^)'
(System::String ^ ) in the prototype is C# syntax, not C++.
Maxwell Chen
|
|
|
|
|
So,how can i do that.
I want to get the text from C#.
Anyother way?
Anu
|
|
|
|
|
Maxwell Chen wrote: (System::String ^ ) in the prototype is C# syntax, not C++.
It is not C#, it is C++/CLI.
Best wishes,
Navaneeth
|
|
|
|
|
N a v a n e e t h wrote: It is not C#, it is C++/CLI.
Oh, OK. Thanks!
Maxwell Chen
|
|
|
|
|
Anu_Bala wrote: static void OnCallClick(CString msg)
{
}
static void OnCallClick(System::String^ msg)<br />
{<br />
}
Its only a guess... please try ; I think the reason is already said by Maxwell.
|
|
|
|
|
Its working.Thank u so much.
But i cannot enter any text in that.Actually in Combobox,IsEditable is TRUE.
But i cannot enter anything.But this event is working fine.
Anu
|
|
|
|
|
It seems the delegate expects an argument of type String^ and your function is using CString as argument.
|
|
|
|
|
Hi, I am learning C++ right now and I am analysing some VS2008 example of MFC. But I think it's not important in this case.
"modeldlg.h"
class CMainDlg
{
CAdderDialog* m_pModeless;
}
What I want to ask is, if it is possible to omit forward declaration of CAdderDialog, because it is not present in "modeldlg.h" and somehow it compiles OK. When I try to make the same code on my own, it doesn't compile.
|
|
|
|
|
You will definitely need a forward declaration of the CAdderDialog class since you're creating a pointer of that type.
If you where creating an object of this type you would need to include the entire class definition.
|
|
|
|
|
Yes, I think so too. But as I have written it is compiling. Do You have any explanantion why ? Maybe it is some project property ?
Complete file modeldlg.h
class CMainDlg : public CDialog
{
public:
CMainDlg(CWnd* pParent = NULL);
void BoxDone();
enum { IDD = IDD_MAIN_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
<big>CAdderDialog* m_pModeless;</big>
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
virtual void OnOK();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
};
|
|
|
|
|
Check well the header(s), I suppose Visual Studio put in place the forward declaration for you (yes, VS does such things...).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|