Van
Van

Reputation: 53

using command line to open file

I am new to command line, i have a problem. i have searched to solve the problem but none of them work . I want to use command line to open file but i cant open it and i don't know why. I checked if i created my file yet.I did change the working directory, but it still cant find my file.

My code

#include<iostream>
#include<string>
using namespace std;
void main(int i ,char *a[])
{
    if (i != 1)
    {
        cout << "Wrong!!!";
    }
    fstream fp;
    fp.open(a[1]);
    if (!fp.is_open())
    {
        cout << "Cant open file";
    }

}

What did i do wrong??

Upvotes: 0

Views: 288

Answers (1)

radbrawler
radbrawler

Reputation: 2685

Well I changed the program a bit and it worked. Mainly it was argument length

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(int i ,char *a[])
{
    if (i != 2)
    {
        cout << "Wrong!!! "<<i;
    }
    fstream fp;
    fp.open(a[1]);
    if (!fp.is_open())
    {
        cout << "Cant open file";
    }
    return 0;
}

That worked for me.

edit- I ran it on ubuntu. I didn't had windows.

Upvotes: 1

Related Questions