How can I capture STDOUT to a string?

A handy function for capturing stdout into a string… The following method is a handy general purpose tool to capture stdout and return it as a string. (I use this frequently in unit tests where I want to verify something printed to stdout.) Note especially the use of the ensure clause to restore $stdout (and … Read more

printf not printing to screen

Like @thejh said your stream seems to be buffered. Data is not yet written to the controlled sequence. Instead of fiddling with the buffer setting you could call fflush after each write to profit from the buffer and still enforce the desired behavior/display explicitly. printf( “Enter first integer\n” ); fflush( stdout ); scanf( “%d”, &i1 … Read more

What exactly is the FILE keyword in C?

is this a keyword or special data type for C to handle files with? What you are refering to is a typedef’d structure used by the standard io library to hold the appropriate data for use of fopen, and its family of functions. Why is it defined as a pointer? With a pointer to a … Read more

Find all substring’s occurrences and locations

string str,sub; // str is string to search, sub is the substring to search for vector<size_t> positions; // holds all the positions that sub occurs within str size_t pos = str.find(sub, 0); while(pos != string::npos) { positions.push_back(pos); pos = str.find(sub,pos+1); } Edit I misread your post, you said substring, and I assumed you meant you … Read more

tech