Job Interview question: How to stream output, open file, and read file in C++ and C

(Last Updated On: May 19, 2010)

Job Interview question: How to stream output, open file, and read file in C++ and C

To stream to clog, set:

void main ( )
using namespace std;

// open the file “file_name.txt”
// for reading
ifstream in(“file_name.txt”);

// output the all file to stdout
if ( in )
cout << in.rdbuf(); else // if the ifstream object is in a bad state // output an error message to stderr clog << "Error while opening the file" << endl; } How to open and read a file: #include

void readone();

int main(int argn, char **argv)
int i;
for (i=0; i<=2; i++){ readone(); } } void readone() { static int ctr = 0; FILE *f = fopen("test.txt", "r"); char str[100]; int i; for (i=0; i<=ctr; i++){ fscanf(f, "%[^\n]\n", str); } puts(str); fclose(f); ctr++; } Beaware of ftell and fseek: /* ftell example : getting size of a file */ #include

int main ()
FILE * pFile;
long size;

pFile = fopen (“myfile.txt”,”rb”);
if (pFile==NULL) perror (“Error opening file”);
fseek (pFile, 0, SEEK_END);
size=ftell (pFile);
fclose (pFile);
printf (“Size of myfile.txt: %ld bytes.\n”,size);
return 0;


NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!
This entry was posted in Quant Development and tagged , , , , , , on by .

About caustic

Hi i there My name is Bryan Downing. I am part of a company called QuantLabs.Net This is specifically a company with a high profile blog about technology, trading, financial, investment, quant, etc. It posts things on how to do job interviews with large companies like Morgan Stanley, Bloomberg, Citibank, and IBM. It also posts different unique tips and tricks on Java, C++, or C programming. It posts about different techniques in learning about Matlab and building models or strategies. There is a lot here if you are into venturing into the financial world like quant or technical analysis. It also discusses the future generation of trading and programming Specialties: C++, Java, C#, Matlab, quant, models, strategies, technical analysis, linux, windows P.S. I have been known to be the worst typist. Do not be offended by it as I like to bang stuff out and put priorty of what I do over typing. Maybe one day I can get a full time copy editor to help out. Do note I prefer videos as they are much easier to produce so check out my many video at