core guidelines - C++ Rewrite a file but leaving out everything before a word

5 Answers

First of all, your while loop is wrong. In fact, such while loop is almost always wrong.

You should be writing the loop as:

while (myReadFile >> output) 
     if (line > 20) {
         cout << output;

Your while(!myReadFile.eof()) loop is wrong, because the eof flag (or any other failure flag) is set after an attempt to read from the stream fails; that means, if the attempt to read fails, you're still outputting, because you're still inside the loop, and the rest of the code in the loop still executes when it in fact should not.

In my version, however, if an attempt to read (i.e myReadFile >> output) fails, then returned std::istream& implicitly converts into false, and the loop exits immediately. And if it doesn't fail, the returned stream implicitly converts to true.

By the way, it seems to me that you want to read line-by-line, instead of word-by-word. If so, then you should write this as:

std::string sline; //this should be std::string
while (std::getline(myReadFile, sline))
     if (line > 20) {
         cout << sline;

Again std::getline returns std::istream. If the read was successful, the returned stream implicitly converts to true and the loop will continue, or if it was unsuccessful, then it would implicitly convert to false and the loop will exit.

pdf program to

I'm using Visual C++ Express 2010... and I'm very new to C++.

I want to read a file then remove everything before the word "<--START-->" and rewrite the file with the rest.

Here's the code I've got for reading the file so far:

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int main() {
  ifstream myReadFile;"text.txt");
  char output[500];
  int found;
  int line;
  if (myReadFile.is_open()) {
    line = 0;
 while (!myReadFile.eof()) {
     myReadFile >> output;
     if(line > 20) {
         cout << output;
return 0;

Many thanks in advance.

delete cout; delete cin; do not give compilation error - a flaw in the Standard library?

It has apparently been fixed.

At least, in N3290 you have std::basic_ios::operator bool instead of that void* conversion, and this operator bool is declared explicit.

Note that C++98/C++03 did not support explicit type conversion operators, but C++11 does.

An explicit type conversion operator

N3290 §12.3.2/2;
is only considered as a user-defined conversion for direct-initialization (8.5)

And that might seem to be impractical for the condition in e.g. a while or for statement.


N3290 §4/3;
An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t (8.5). Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (8.5). The effect of either implicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion.

where bool t(e); is a direct-initialization.

E.g. you don’t have to explicit convert a stream object used as condition in a while, because there is implicitly an explicit conversion (he he).

Unfortunately, searching N3290 I can’t find any list of the “certain language constructs” where this happens, but in comments to this answer JohannesD wrote:

Searched through the FDIS for “contextually”, and the whole list seems to be: if, while, do, for, noexcept, and static_assert conditions; the first operand of ?:; both operands of && and ||; and the operand of !.

Cheers & hth.,

How to end a whole program

 if(input(matrix,number) != 0) output(matrix,number); 
 // You don't {} for a single instruction

This should fix your problem.

The usual way to indicate errors is to throw an exception.

int main() {
    try {
        input(matrix, number);
        output(matrix, number);
    } catch(std::exception& e) {
        std::cout << "Error: " << e.what();

In input:

if ((a[i][j] < 6) || (a[i][j] > 10))
    throw std::runtime_error("Invalid input!");

You can read from fstream to int using operator>>:

int n;
infile >> n;