Main arguments

Writing name_file.txt from the shell, the program does not open the file ..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char* argv[]) 
{
 ifstream myfile(argv[1], ios_base::in);
 
 if(myfile)
 {
  cout << "File open!" <<endl;         
 }
 else
 {
  cout << "File do not exist!" <<endl;    
 }
  
 system("pause");
}
Can you show the command that used to run the program? Does the file name_file.txt exists?
can be any of these reasons
You are not passing the name of the file while running this program
The file does not exist, or the name is misspelled
You don't have permission to open the file

also, I am not sure if it is not really opened as if(myfile) should be if (myfile.is_open())
if i try this code, the program works correctly...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char* argv[]) 
{
 fstream myfile("name_file.txt", ios_base::in);
 
 if(myfile)
 {
  cout << "File open!" <<endl;         
 }
 else
 {
  cout << "File do not exist!" <<endl;    
 }
  
 system("pause");
}
Last edited on
Then the problem is probably the way you pass the filename to the program.
First general debugging rule I use is that before I even check for the file being open I output the argv[] to see if I picked the right one. Then I can start figuring out if it is my other parts of the code.

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
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char* argv[]) 
{
 ifstream myfile(argv[0], ios_base::in);

  cout << argv[1] << endl;         
  // showing either way works 
 if(myfile)
 {
	 cout << "MyFile Open!" << endl;
 }
 
 if(myfile.is_open())
 {
  cout << "File open!" <<endl;

 }
 else
 {
  cout << "File do not exist!" <<endl;    
 }
  
 return 0;
}


$ ./fileOpen file.txt
file.txt
MyFile Open!
File open!
codewalker wrote:
also, I am not sure if it is not really opened as if(myfile) should be if (myfile.is_open())

Either way should work.
Last edited on
File do not exist! ....
Last edited on
The file may not be in the working directory of the process. Try giving the full path to the file. eg. "/usr/home/alby/my_dir/myfile.txt"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>

int main( int argc, char* argv[] ) 
{
    if( argc == 2 )
    {
        std::clog << "trying to open the file '" << argv[1] << "'\n" ;
        std::ifstream file( argv[1] ) ;
        
        if(file)
        {
            std::clog << "success!\n" ;
            std::cout << "the file contains\n--------------------\n\n" << file.rdbuf() ;
            
        }
        else std::cerr << "failed to open file for input\n" ;
    }
    else std::cerr << "usage: " << argv[0] << "  <path to the file> \n" ;
}

http://coliru.stacked-crooked.com/a/6de078d2b2040a1d
Yes, you have to have the text file in the same directory as the program otherwise you have to specify the full path to the text file like JLBorges said. I added text to the file and read it in then printed it out to show it does work, but left the code from above just to show it is the same code.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char* argv[]) 
{
 ifstream myfile(argv[1]);
 string data = "\0";
 if(myfile)
 {
	 cout << "MyFile Open!" << endl;
 }
 
 if(myfile.is_open())
 {
  cout << "File open!" <<endl;
  cout << argv[1] << endl;         
 }
 else
 {
  cout << "File do not exist!" <<endl;    
 }
 
 while(getline(myfile, data))
 {
	 cout << data << endl;	 
 }
 
 myfile.close();
  
 return 0;
}

File.txt

Just showing
It does work
As I filled it
With five lines
of text!

Output

$ ./fileOpen file.txt
MyFile Open!
File open!
file.txt
Just showing
It does work
As I filled it 
With five lines 
of text!
The txt file is on the desktop as the executable ...
It may be a problem concerning the translation that follows the shell (ASCII or Unicode)?
> First general debugging rule
is to use a debugger.
(step by step, watch variables)


> the name is misspelled
TAB
It will look for the file in the working directory (the directory that you have navigated to using the cd command). If you run the program from some other directory it will not find the file, unless that directory also contain a file with the same name.
$ cd Desktop
$ ./fileOpen file.txt
File open!
$ cd ..
$ Desktop/fileOpen file.txt
File do not exist!
$ Desktop/fileOpen Desktop/file.txt
File open!
ne555 wrote:
> First general debugging rule
is to use a debugger.
(step by step, watch variables)

It is a debugging technique called diagnostic print statements which a lot of programmers use before they start using the debugger.
Problem solved!!! Thanks to all! the problem has been resolved when I restart the pc ...
Topic archived. No new replies allowed.