Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Whats the C++ equivalent of reading from stdin or a file
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Adrian, post: 3086046"] What is the best was to do this in c++. This is going to be used for unix util that should be able to have input piped to it or file name spec #include <stdio.h> int main(int argc, char *argv[]) { FILE *fp; int c; if(argc==2) { fp=fopen(argv[1], "r"); } else { fp=stdin; } while((c=fgetc(fp))!=EOF) { printf("%c",c); } return 0; } I assume it has something to do with the underlying buffer and have tried this but it wont compile. #include <iostream> #include <fstream> int main(int argc, char *argv[]) { std::ifstream in; if(argc==2) { in.open(argv[0]); } else { in.rdbuf(std::cin.rdbuf()); } int c; while(c==in.get()) { std::cout << c; } return 0; } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Whats the C++ equivalent of reading from stdin or a file
Top