Find the answer to your Linux question:
Results 1 to 2 of 2
Hi all, I'm currently making some very cool archiving library. If you want to see the details about it , you'll find usefull info here , but that's irrelevant for ...
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    2

    Problem with pipes in C++

    Hi all,

    I'm currently making some very cool archiving library. If you want to see the details about it , you'll find usefull info here , but that's irrelevant for this thread.

    I'm currenlty having problems with RAR format.
    Since I could not find some decent low-level RAR library, I've decided to use unrar command in background ,and to pipe it trough . I did not simply called fork/exec without pipes, because I want to place progress monitors and appropriate events.

    Code:
    /* This code is bit simplified, of course, I've omitted logging and stuff */
    
    
    // This command prints given archive-file  to stdout  ( option p   )  
    // without printing any extra info like Copyright form RARLabs  ( -inul ) 
    // The objective is to have same effect as writing the following in console: 
    //  rar p -inul 'myarchive.rar'  'myfile.pdf' >   '/home/dule/myfolder/myfile.pdf' 
    
    FILE *fpipe;
    string command= "rar p -inul  \'"  + "myarchive.rar" + "\' \'" + "myfile.pdf" + "\'";
    
    if ( !(fpipe = (FILE*)popen(command.c_str(),"r")) )
    {  // If fpipe is NULL
    	
    	pclose(fpipe);
    	exit(1);
    
    }
    char c;
    ofstream fout( "/home/dule/myfolder/myfile.pdf" );
    
    while( (c = fgetc(fpipe)) != EOF)
    {
    	fout << c;
    }
    
    
    pclose(fpipe);
    return true;

    For small text files it works . When I try to do this with , for example , PDF file, program exits cleanly, but it unpacks small portion of file.

    When I issue the the following command from shell :
    rar p -inul 'myarchive.rar' 'myfile.pdf' > '/home/dule/myfolder/myfile.pdf'

    , it works just as it should be.


    Thanks in advance.
    If anyone needs full source, I'll drop a link

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You're dealing with binary data here, and there's no guarantee you won't come across weird characters such as hex FF. In fact, hex FF will appear as the EOF indication.

    Use fread() instead of fgetc().

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

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