Fstream not opening file

When I try to open a file, it says it opened when it didn't. Please check the code to see if its my codes problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <winsock2.h>
#include <windows.h>

using namespace std;

int main()
{
    cout<<"Hello"<<endl;
    while(true)
    {
    int counter=0;

    string enter;
    getline(cin, enter);
    istringstream iss(enter);
    string request[2];
        do
    {
        counter++;
        iss >> request[counter];
    } while (iss);
    if(request[1]=="run")
    {
        if(request[2]=="chrome")
        {
            ifstream file("hi.txt");
            file.open("hi.txt", ios :: in | ios :: out | ios :: trunc);
            if(!file.is_open())
            {
                cout<<"Could not open file"<<endl;
            }

        }
    }
    }
}

Thank You
How do you know it didn't open when you don't seem to be using the file variable?
i have a file name hi.txt and it doesnt open when it says it does
file.open() doesn't open the text file, it opens a stream to the file so the program can read from and/or write to the file depending on the type of stream.
oh but do u know a way to open the file?
When a programmer says "open a file" that simply means the file is made available for reading or writing to it. It doesn't mean the content of the file is displayed or anything like that. So what exactly do you want to happen? If you want to display the text content of the file you will have to read the content and print it.
write the complete address in the open command.
1
2
ifstream in;
in.open("c:\\users\\hi.txt");
You usually don't have to do that if the file is in the same directory as either the project or the executable depending on whether or not you are running it from within an IDE or from the exe.
Topic archived. No new replies allowed.