0

I am setting up my environment to run a C++ program on VSCode in Windows 10. I have MINGW installed and the path (C:\MinGW\bin) added to my environment variables. I have added the appropriate extensions in VSCode.

In my hello world program. It works just fine. However, when I open a file, nothing prints from anywhere in the entire program.

I am compiling like this: g++ main.cpp -o main

And running the program like this: ./main test.txt

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

using namespace std;

int main(int argc, char *argv[]) {
    cout <<"Here\n";
    ifstream file; //This is the line that causes no output
    file.open(argv[1]);
    return 0;
}

The file is not the problem. I have tried multiple file types like .txt and .csv.

Dane B
  • 169
  • 12
  • The marked line doesn't cause any output. It simply defined an `ofstream`. To get output you have to open a file (done on the next line) read data from the file (not done yet), and write the data read from the file to the console (not done yet). [Here's a quick way to do the latter two operations](https://stackoverflow.com/a/10195497/4581301), but instead of opening and writing to `dst`, write to `std::cout` – user4581301 Sep 27 '19 at 03:57
  • You are just opening a file, no read or write operation, you want to read or write? Btw. you can get Visual Studio Community for better comfort. – idem Sep 27 '19 at 03:59
  • 1
    @user4581301: OP meant that `cout <<"Here\n";` does not produce any output when the marked line is included. – Eugene Sep 27 '19 at 04:00
  • Ah. I see you @Eugene . That would make more sense. In that case the stream is not flushed, so there are no guarantees "Here\n" makes it out before the program exits. It SHOULD be emitted when the program exits and `cout ` closes, though. I got nuthin' for you here, Dane. – user4581301 Sep 27 '19 at 04:09
  • Does it help if you flush the output? That should be general practice anyway. – OrdoFlammae Sep 27 '19 at 14:53

0 Answers0