CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Apr 2022

    Sending Posix message via pipe

    I have following file called helper.h, which I want to call in server and client files later.

    #ifndef HELPER_H
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
     * It will iterate through all the lines in file and
     * put them in given vector
    int getFileContent(std::string infileName,std::vector<std::string>& vecOfStrs)
        // Open the File to read the content into buffer
        std::ifstream in(infileName.c_str());
        // Check if object is valid
        if (!in)
            throw (std::runtime_error("Cannot open the File : " ));
        std::string str;
        // Read the next line from File until it reaches the end.
        while (std::getline(in, str))
            // Line contains string of length > 0 then save it in vector
            if (str.size() > 0)
        /*for (const auto& str : vecOfStrs)
            std::cout << str << '\n';*/
        return str.size();
    int writeFileContent(std::string outfileName, std::vector<std::string>& vecOfStrs){
        std::string str;
        //Open the file to output the read buffer
        std::ofstream out(outfileName.c_str());
        if (!out)
            std::cerr << "Write operation failed.";
            return false;
        //int result = getFileContent("sample.txt", vecOfStrs);//This line should be removed
            for (const auto& row : vecOfStrs)
                out << row << '\n';
        return str.size();//str is created but never used re
    }//Close of the write function
    I want to call it via server named sender.cpp. I want to compare bytes_read and bytes_write and then send it to the receiver. My code is correct till pipe open. How can i proceed after that.
    // Created by pramah on 4/11/22.
    #include "helper.h"
    int main(){
        return 0;
    void send(std::string pipe_name, std::string fname) {
        std::cout << "Starting pipeSend.." << std::endl;
        int pd;
        int total=0;
        std::vector<char> buffer(PIPE_BUF);
        std::vector<std::string> vec_str;
        std::ifstream ifs;
        unsigned bytes_read, bytes_write;
        //Pipe created
            throw(std::runtime_error("Sending failed. mkfifo"));
        //Open the pipe
        pd = open(name.c_str(),O_WRONLY);
            throw(std::runtime_error("Send(): Pipe onpening failed"));
        //Read and write operation
        while(ifs){//infinite loop
            bytes_read=getFileContent(fname, vec_str);
            if(bytes_read<=0) {
                throw(std::runtime_error("Send: readFile"));
        }//reading input stream done
        //Write to pipe
        bytes_write = write(pd,,bytes_read);
            throw(std::runtime_error("send: write to pipe"));
            total +=bytes_read;
            throw(std::runtime_error("send: read write difference"));
        std::cout<<"File deliverd from the pipe."<<std::endl;
    Last edited by perto1; April 24th, 2022 at 03:36 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width