What's going on here?

So I made a program that opens a file, then takes the users input and writes it to the file, then closes the file. No matter what you enter for the file location it doesn't work. If you enter a correct location, it doesn't open the file even though it says it does. If you enter a wrong location it still says it opened the file. What am I doing wrong?
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 <cstring>
#include <fstream>
using namespace std;

int main(){
    char file[MAX_PATH +1];
    string a;

    cout << "Enter the file location to edit: ";
    cin.getline(file, MAX_PATH);    
    ofstream edit(file);
    if(! edit){
         cout << file << " could not be opened.";
         cout << endl;
         system("pause");
         return -1;
         }
         cout << file << " was opend succefully. Enter @X to exit." << endl;;
   while (a != "@X"){
          cout << "Enter line of text to add - ";
          cin >> a;
          edit << a;
          }
          edit.close();
    system("pause");
    return 0;
    }
Hi Zexanima,

just tried your code. The problem, I think, is that you have probably set MAX_PATH to equal 0, even though you have not defined it here atall (I am not sure how you got it to compile!).

The rest of you code looks OK, although you'll want to write an endl to your output file after 'a' and to modify the while loop so that you don't write '@X' as well. Here's my solution:

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

using namespace std;

const int MAX_PATH = 100; //***

int main()
{
  char file[MAX_PATH];
  
  cout << "Enter the file location to edit: ";
  cin.getline(file, MAX_PATH);    
  
  ofstream edit(file);
  
  if(!edit){
    cout << file << " could not be opened." << endl;
    return -1;
  }

  cout << file << " was opend succefully. Enter @X to exit." << endl;;

  for(;;)
    {
      cout << "Enter line of text to add - ";

      string a;
      getline(cin, a);

      if(a != "@X")
	edit << a << endl;
      else
	break;
    }

  edit.close();

  return 0;
}


It then works fine.
Works OK for me - that's on Linux with g++ v4.4.5.

If you enter a correct location, it doesn't open the file even though it says it does.

If your program says it's opened the file, you can be pretty sure it's opened the file.
If you enter a wrong location it still says it opened the file.

What do you mean by 'wrong location'? That doesn't make sense. If you specify a file that doesn't exist it'll just create it for you. You've got the line system("pause"); in your code so I assume you're running this from within an IDE. Do you know what the current working directory is? Because if you just tell it to open 'my_file.txt', that's where it'll go. Try specifying a full path, or better still run the program from the command line.
Regards, keineahunung
Hi,

just realised that MAX_PATH might be a macro that I don't know about. Is it? If so, it is not recognised by g++ 4.2.1 when I do not include additional headers.
i don't know about MAX_PATH macro, but wasn't it not logical to add 1 to MAX_PATH ? the logic is, if MAX_PATH could be added by 1, then MAX_PATH couldn't be said as the maximum value.
i don't know about MAX_PATH macro, but wasn't it not logical to add 1 to MAX_PATH ? the logic is, if MAX_PATH could be added by 1, then MAX_PATH couldn't be said as the maximum value.


Sorry to take so long to reply to this. Yes, MAX_PATH is a macro for the maximum number of characters allowed in a file path including it's name and directory. You have to add one for the "\0" or terminating null that's at the end of a C-String string, because C-String doesn't account for length so you have to take it into account for it. This is unless somethings changed from the time the book I'm reading was written.

If your program says it's opened the file, you can be pretty sure it's opened the file.
If you enter a wrong location it still says it opened the file.


I got it to open the file (I forgot to use the .txt extension, ooops) but still when I open a file that doesn't exist it doesn't exit though. I think it should though with this part ofthe code right? (I'm using DevC++ on WindowsXP by the way.)
1
2
3
4
5
6
7
if(! edit){
         cout << file << " could not be opened.";
         cout << endl;
         system("pause");
         return -1;
         }
         cout << file << " was opend succefully. Enter @X to exit." << endl;;

Part and say "could not open" I thought.
Topic archived. No new replies allowed.