A
Adrian
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;
}
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;
}