File Will Not Open! Help ASAP

I Need Help Getting This File To Open! Please!. This project is due tonight and I cant get the file to open. I Think its a simple mistake. but I cant find it Please Help!

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
 #include <iostream>
#include <cctype>
#include<stdio.h>
#include<iterator>
#include<fstream>
#include <sstream>

using namespace std;


bool NUM;
int main() 
{

 
  char ch[80]; 
  int i, numbers = 0, other = 0, character = 0, letters = 0, spaces = 0, punct = 0, digits = 0, special = 0, upper = 0, lower = 0;

	ifstream myfile ("C:/Users/marqu_000/Documents/matter.txt");
	
	if(myfile.is_open())
    //getline(myfile);
	while (myfile)
	{
	

  cout << "\nEnter The String:\n " << endl;
  gets (ch);
  bool NUM = false ; //initialises to false
  for (i =0; ch[i] !='\0';i++) 
  {
	
	 
  	if (ch[i] != ' ')
character++;

    if(isalpha(ch[i])) 
       ++letters;
    else if(isspace(ch[i])) 
       ++spaces;
    //else if(ispunct(ch[i])) 
      // ++punct;
    if (ch[i] ==',')
    ++punct;
    else if (ch[i] ==';')
    ++punct;
    else if (ch[i] =='.')
    ++punct;
    else if (ch[i] ==':')
    ++punct;
    else if (ch[i] =='!')
    ++punct;
    else if (ch[i] =='?')
    ++punct;
    else if (ch[i] =='"')
    ++punct;
    else if (ch[i] =='/')
    ++punct;
    else if (ch[i] =='-')
    ++punct;
    else if (ch[i] =='+')
    ++punct;
    else if (ispunct(ch[i]))
    ++special;
    
    else if(isdigit(ch[i])) 
       ++digits;
    ++ch[i];
    
    if (ch[i] >= 'A' && ch[i] <= 'Z')
upper++;
	 if (ch[i] >= 'a' && ch[i] <= 'z')
lower++;

 if (isdigit(ch[i]) == true)
	 NUM++;	
	 if (ch[i] == ' ')
	 ++other;



  }
  cout << "Number of characters: " << character << endl;
  cout << "Number of letters: " << letters << endl;
  cout << "Number of lower cases : " << lower << endl;
  cout << "Number of upper cases : " << upper << endl;
  cout << "Number of digits: " << digits << endl;
  cout << "Number of punctuation characters: " << punct << endl;
  cout << "Number of special characters: " << special <<endl;
  cout << "Number of spaces: " << spaces << endl;
  cout << "Number of words: " << spaces + 1 << endl;
  cout << "Number of numbers " << NUM;
  cout << "\nNumber of others " << other;
 
myfile.close();
}
else
{
	cout << " Unable to open output file. " << endl;
	return 1;
}
  return 0;
}
I forget... should you be using c:\\folder\\folder2\\filename.txt format? windows uses \ unix uses /

It gives me the same results "Unable to open file".
Maybe you don't have access to that path, or the filename is incorrect? You could try to approach it from another angle. This code attempts to first create a file, then read it back in.

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

using namespace std;

int main() 
{
    const char * filename = "C:/Users/marqu_000/Documents/test123xyz.txt";
    
    ofstream outfile(filename);
    
    if (!outfile)
    {
        cout << " Unable to open output file. " << endl;
        return 1;
    }
    
    cout << "Output File is open\n";
    
    outfile << "This is a test 123\nApples and pears\n";
    
    outfile.close();
    
    //----------------------------------------------------
    
    ifstream infile (filename);
    
    if (!infile)
    {
        cout << " Unable to open input file. " << endl;
        return 2;
    }
    
    cout << "Input File is open\n";
    
    char line[100];
    while (infile.getline(line, sizeof(line)))
    {
        cout << line << '\n';
    }
    
    cout << "\nDone\n";
    
}

If I run it as it stands, it gives me the error message,
Unable to open output file. 

However, when I change the username "marqu_000" (or entire path) to something which matches my system, then I get this output:
Output File is open
Input File is open
This is a test 123
Apples and pears

Done

That's weird! I always just use the path to location on my windows computer @Chervil .
Sorry, I don't understand. Does that mean it worked or failed?
I tried both ways. Not a completed project yet! *my heads hurting lol *. Been working on this for like 3 days !!! I got the out file to output but its something with the input file! I'm getting errors.
Last edited on
ITs crazy i have 3 different read string techniques and non of them are making my program valid
I got the out file to output

That's good! It means:
1. you have the correct pathname
2. you have both read and write access to that directory.
So far so good.


but its something with the input file! I'm getting errors.


Well - the original code in the opening post attempts to open an input file. But the rest of the code is a bit of a muddle. It never actually reads anything from the file.

Do a search for "myfile" to see where it is used, no reading of any kind.
Topic archived. No new replies allowed.