You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Saturday, April 28th 2012, 7:21am

[SOLVED] How to pass binary data to QProcess as its std file input?

Hello everybody,

I am trying to create a simple program, which would listen on a local socket for chunks of binary data. When they are received, I want to pass them somehow to another program, which is capable of converting them into readable text. This text should be the output of my program.

The first part is simple and I will make it using QLocalSocket and QByteArray.

However I do not have an image of how to pass binary data to a program, which accepts input from a file/pipe. I was thinking about using QProcess and its pipe-like functionality to achieve something this:

echo '<my binary data>' | program

Problem is how to put raw binary data into there. Is it possible?

Or are there other ways to solve this issue?

I tried to search for it but without success.



Boris

This post has been edited 1 times, last edit by "juchuchuu" (May 18th 2012, 7:47pm)


2

Saturday, April 28th 2012, 10:29am

QIODevice is part ofQProcess. you can write binary data to one of the channels.
http://qt-project.org/doc/qt-4.8/qiodevice.html#write-3

How you do it depends on how the program works. does it take binary data as command line arg/filename as command line arg/some other command once running.

Look at QProcess and write, above.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Sunday, April 29th 2012, 6:49am

The program accepts filenames as command line arguments. Thank you very much, it works fine.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

int count = 33;
int8_t _bytes [33] =  {
0x12, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1B, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6C,
0x74, 0x00, 0x01, 0x00, 0x08, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6C 
};

QString program = "program name";
QProcess process;

process.start(program);

process.waitForStarted();

process.write((char *)_bytes, count);

process.waitForFinished();

qDebug() << "StdOut: " << process.readAllStandardOutput();
qDebug() << "StdErr:" << process.readAllStandardError();

return 0;

Similar threads

Used tags

binary, QProcess