Opening file issues

my file is failing to open i have a file named numbers.txt with 12 numbers in it
like so :
1
2
3
4
5
.
.
.
through to 12

but it will not open can someone tell me what I'm doing wrong?
p.s the file is stored in my documents whit no other files on my computer matching its name.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<iostream>   
#include<fstream>
#include<string>

using namespace std;   

int getLargest(int[]);
int getSmallest(int[]);
int getTotal(int[]);
int getAverage(int[]);

    
 int main()   
 {   
     int value[12]; 
	 string filename;
	 ifstream inFile;

	 //ask user for file name
	 cout<<"Enter the file name: ";
	 cin>>filename;
     
	 // Open the file.  
     inFile.open(filename);  

	 if(inFile.fail())
	 {
		 cout<<"Error!!!";
	 }
	 else
	{	 
	//Write values too array
	 for(int i = 0; i <12; i++)
	 {
		 inFile>>value[i]; 
	 }
	
  
     // Close the file. 
	 inFile.close();
	 
	 //Display information
	cout<<"Largest Value: "
		<<getLargest(value)<<endl;
	
	cout<<"Smallest Value: "
		<<getSmallest(value)<<endl;
	cout<<"Total of the numbers: "
		<<getTotal(value)<<endl;
	cout<<"Average of the numbers: "
		<<getAverage(value)<<endl;
	 }


	cout<<endl;
     system("PAUSE"); 
  }  





 int getLargest(int value[])
{
	int temp;
	
	temp = value[0];

	for(int i = 1; i<10; i++)
	{
		if(value[i]>temp)
		{
			temp=value[i];
		}
	}
	return temp;
}

int getSmallest(int value[])
{
	int temp;
	
	temp = value[0];

	for(int i = 1; i<10; i++)
	{
		if(value[i]<temp)
		{
			temp=value[i];
		}
	}
	return temp;
}

int getTotal(int value[])
{
	int temp= 0; 
		for(int i=0; i < 12; i++)
		{
			temp+=value[i];
		}
	return temp;
}

int getAverage(int value[])
{
	int temp;
		temp = getTotal(value);

	return temp/12;
}
Last edited on
When you enter the filename, give the full path. The whole, complete path. It probably begins "C:\".
didnt work
Does your filename contain spaces ? If yes try one without spaces.
didnt work


Perhaps you typed it in wrongly. I can't see your monitor for some reason, so you're going to have to tell us what you've tried.
I've tried to type in numbers.txt as well as hard code in "numbers.txt"
also tried the full path by typing in:

C:\Users\Franco\Documents\numbers.txt
Last edited on
Try this code and see what it says:
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
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

bool FileExists (string& filename)
{
  WIN32_FIND_DATA fd = {0};
  
  HANDLE hFile = FindFirstFileA (filename.c_str (), &fd);

  return hFile != INVALID_HANDLE_VALUE;
}

int main ()
{
  string filename = "C:\\Users\\Franco\\Documents\\numbers.txt"; 

  if (FileExists (filename))
  {
    cout << "File " << filename << " exists";
  }
  else
  {
    cout << "File " << filename << " doesn't exists";
  }
  cout << "\n\n";

  system ("pause");
  return 0;
}
C:\Users\Franco\Documents\numbers.txt
Try C:/Users/Franco/Documents/numbers.txt instead.
@ OP: To build off of Thomas1965's code, replace his version of Line 26 with:
 
cout << "File " << filename << " doesn't exists\nError Code: " << GetLastError() << "\n";


Escaping the backslashes will probably solve the issue, but just in case, it would be helpful to get an actual error code.
Tried Thomas1965's code and it worked it told me that the file did exist.
I typed it as user input the way Thomas1965 has it hard coded in line 18 as

C:\\Users\\Franco\\Documents\\numbers.txt


and it finally worked my only issue is I have to turn this assignment in online and have no idea as to where my instructor will have the file stored. is there a way around typing the full file path. or do I just specify to the end user to use the full file path?
Last edited on
also in response to Computergeek01 i used your line of code into my own under my if statement

1
2
3
4
5
if(inFile.fail())
{
    cout << "File " << filename << " doesn't exists\nError Code: " << GetLastError() << "\n";
}


then I proceded to once again try to input as the end user numbers.txt and it returned
Error code: 2
OP wrote:
... my only issue is I have to turn this assignment in online and have no idea as to where my instructor will have the file stored.



Charles Babbage wrote:
On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.


Translation: It's up to the end user to feed the correct information into the program. Yes, clear instructions can help sometimes but at the end of the day your program only does exactly what the end user tells it to.
You can specify that he has to enter the full path. Also it might be better to use getline instead of cin in case he has spaces in his filename.
Hi,

It also depends on what IDE you are using. With many IDEs, having the file directly in the same folder as your .exe file will do the trick. This is the standard.

When running a program from VISUAL STUDIO, and not by double-clicking the executable, you'll have to put the text file in your project's directory where all the .cpp and .h files are located. Otherwise, running the program from the IDE will cause Visual Studio to not find the text file.

I ran into this problem with several of my students, already :P

Joe
Thank you for your help everyone my instructor never talked about or showed us how to use files in our programs it was more of figure it out yourself deal. Unless he didn't realize that the assignment asked for that. but thank you anyways. You were all very helpful.
Little Captain wrote:
It also depends on what IDE you are using. With many IDEs, having the file directly in the same folder as your .exe file will do the trick. This is the standard.


Careful there, that's not a "standard" set in stone anywhere, that's just a common default setting. The relative directory that the executable runs in is actually a user setting.
Thanks for that!
Topic archived. No new replies allowed.