All pastes #912650 Raw Edit

Untitled

public text v1 · immutable
·published 2008-02-21 15:22 UTC
rendered paste body
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <unistd.h>

using namespace std;

int main ( void )
{
  int id;
  int filedesc[2];
  int length;
  char buffer[128];
  string b_str;
  int n;

  if(pipe(filedesc))
    cerr << "Error in Opening pipe!\n";

  id = fork();

  if (id == 0)
  {
    ifstream fin("input.txt");
    string str;
    char buf[128];
    while (getline(fin,str))
    {
      strcpy(buf,str.c_str());
      write(filedesc[1],buf,sizeof(buf));

      cout << "Child read/write string: " << str << endl;
    }
    close(filedesc[1]);
    exit(0);
  }
  else
  {
    double sum = 0;

    do 
    {
      n = read(filedesc[0],buffer,sizeof(buffer));

      if (n == 0)
        break;

      b_str = buffer;

      cout << "Parent read string: " << b_str << endl;

      length = (int) b_str.length() - 1;

      for (int i = 0; i <= length; i++)
      {
        char c = b_str[length-i];
        if (c = '1')
          sum += pow(2.0,(double)i);

      }
      cout << "sum=" << sum << endl;
    }while ( b_str != "!" );
    cout << "Final sum: " << sum << endl;

    exit(0);
  }

  return 0;
}