Read Files From stdin

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 45
Reputation: petzoldt01 is an unknown quantity at this point 
Solved Threads: 0
petzoldt01 petzoldt01 is offline Offline
Light Poster

Read Files From stdin

 
0
  #1
May 22nd, 2008
Im currently trying to work with a program that reads raw DV and dumps each frame in the form of a PPM file. I need to read the PPM's into OpenGL Textures and display them. But that is not the problem.

The program has the ability to dump all of the images to stdout instead of the filesystem. I want to read these images through my program's stdin, and this is where my troubles begin.

Does <iostream> directly support this? All example uses of <fstream> that I have seen regard reading/writing a file on the filesystem. How do I read a file from stdin, and to top that, not just one file, but consecutively streaming files to no predictable end (user terminated).

Thanks for any help.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,571
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Read Files From stdin

 
0
  #2
May 22nd, 2008
It works with cin and cout too.
  1. // test1.cpp
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int _tmain(int argc, _TCHAR* argv[])
  6. {
  7. cout << "Hello World\n";
  8. return 0;
  9. }
  1. // test2.cpp
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string line;
  9. getline(cin,line);
  10. cout << "\n\n" << line << "\n";
  11. return 0;
  12. }
  1. // command line
  2.  
  3. D:\dvlp>test2 | test1
  4.  
  5.  
  6. Hello World
  7.  
  8. D:\dvlp>
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 45
Reputation: petzoldt01 is an unknown quantity at this point 
Solved Threads: 0
petzoldt01 petzoldt01 is offline Offline
Light Poster

Re: Read Files From stdin

 
0
  #3
May 23rd, 2008
I understand how piping works, and I have used cin and cout enough to be comfortable with them, but I have always used them in a context similar to what you provided.

When it comes to streaming these files, I am going to have a thread that continuously scans stdin and will need to be reading it in binary mode rather than as text. I have some confusion when it comes to detecting the End Of File, delegating the completed file back to my main thread, and then begin on the next incoming file.

Do I need to actually scan the incoming stream for an EOF indicator, or do the <iostream> functions handle that?

If you know how to do this, and you can post the source, that would be wonderful, but what I am really looking for is a good read on the topic. I have been having problems finding any directly related content.
Last edited by petzoldt01; May 23rd, 2008 at 5:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Read Files From stdin

 
0
  #4
May 23rd, 2008
Treat them as any other stream. So yes, watch for EOF.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 45
Reputation: petzoldt01 is an unknown quantity at this point 
Solved Threads: 0
petzoldt01 petzoldt01 is offline Offline
Light Poster

Re: Read Files From stdin

 
0
  #5
May 23rd, 2008
Ok, so I found this, http://www.daniweb.com/forums/thread45686.html, and using ...

  1. FILE *stream;
  2.  
  3. if (argc < 2)
  4. {
  5. stream = stdin;
  6. }
  7. else
  8. {
  9. stream = fopen(argv[i],"r");
  10. if (stream==NULL)
  11. ...
  12. }

So, my program will use 'stream' to fill some buffer. Will stream close automatically when it detects an EOF, or do I need to be scanning the buffer for an EOF? So then, I keep two pointers to the buffer, one for the start of a new file, and one after an EOF is detected. I take that whole chunk, and give it to a program like imagick and it will handle it just like every other image?

Does that sound half right? Am I missing anything? Is there an easier way to do it?

Thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC