0

I'm just starting to learn C++ coming from C and I am hitting a bit of a roadblock right off the bat. Writing this simple program to print input from a text file line by line gives no console output whatsoever, not even the lines that should be printed regardless of the files content. What gives? Using endl to flush the buffer seems to have no effect.

using namespace std;

#include <iostream>
#include <fstream>
#include <string>

void readFile(ifstream& file) {
    string word;
    int c = 0;
    while( file >> word) {
        cout << word << endl;
        c++;
    }
    cout << "Read " << c << " words." << endl;
}

int main() {
    cout << "Will open file." << endl;
    ifstream file;
    file.open("testscene.txt");
    if(!file.is_open()) {
        cout << "File not found!" << endl;
        return 0;
    }
    readFile(file);
    return 0;
}
SolidSnackDrive
  • 213
  • 2
  • 10
  • https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Mat Dec 16 '20 at 23:18
  • 2
    It looks like there is a problem with your environment rather than with your code. – n. m. could be an AI Dec 16 '20 at 23:23
  • The environment is Git Bash under Windows 10. I am unsure of where the ambiguity would be coming from based off the post Mat linked. – SolidSnackDrive Dec 16 '20 at 23:26
  • Your code produces output just fine. Though you should not hardcode filenames. Suggest `int main (int argc, char **argv)` and then validate `if (argc < 2)` and then `file.open(argv[1]);`. You shouldn't have to recompile your program just to read from a different filename. – David C. Rankin Dec 16 '20 at 23:31
  • 1
    I am well aware of command line arguments. This is just a little test program. Compiling on my machine with g++ and running the program produces no output in both git bash and powershell, so I suppose this is some problem unrelated to my code... But I still have no clue what that issue is. – SolidSnackDrive Dec 16 '20 at 23:33
  • The only thing I can think of is that you have redirected `stdout` to some other file descriptor. Such as redirecting to `/dev/null`, or a file, etc..You would have had to do that in your current shell. Similar to `exec 3<> "$TMP"` and `exec >&3`. Otherwise I would suggest closing your current terminal and opening a new one with a fresh environment -- just to make sure. Since you are on windows -- there may be an option?? Something like `[] Write program output to a log file` ?? That's really the only thing I can think of that would make output disappear. Both in Git-bash & PowerShell is odd? – David C. Rankin Dec 16 '20 at 23:37
  • you have to accept the 'string word' using cin.getline(word); – Anurag Hale Dec 17 '20 at 02:24
  • "The environment is Git Bash under Windows 10." Sadly this is not enough information to diagnose your problem. As far as I know, git bash doesn't include a C++ compiler. You may want to install msys2 and install gcc using the msys2 package manager. This is a tried and proven environment. – n. m. could be an AI Dec 19 '20 at 18:01

0 Answers0