So i'm working on this program. And long story short, I need something that will allow the user to choose the dir/file and have the system set read-only to true.

Here is a piece that I worked on. Only it crashes on the system line.

try
  {
    clear;
    char* dir;
    cout << "Enter folder location > ";
    cin >> dir;
    char l_szCommand[50];
    sprintf(l_szCommand, "attrib +r %s", dir);
    system(l_szCommand);
    Sleep(5000);
  }
  catch (int e)
  {
    clear;
    cout << "Error: " << e << endl;
    system("PAUSE");
  }

Forgive me for anything wrong with this for I am just about a few hours new to C++. And i'm only 13...

Hmm. That seemed to fix it. But now I have a new problem. Whenever I enter a location, it gives me

File not found - Ä

Also, I don't much about memory allocation but after a few tutorials, i wrote this update

try
  {
    clear;
    char * dir;
    cout << "Enter folder location > ";
    cin >> dir;
    dir = new char [50];
    char l_szCommand[50];
    if (dir == 0) {
  clear;
  cout << "Error: memory could not be allocated" << endl;
  pause;
  }; 
    sprintf(l_szCommand, "attrib +r %s", dir);
    system(l_szCommand);
    Sleep(5000);
  }
  catch (int e)
  {
    clear;
    cout << "Error: " << e << endl;
    pause;
  }

Memory should be allocated before you use the pointer for storing data.

In the current code you are taking in data to a pointer which could be pointing anywhere and then pointing that pointer to a fresh memory location.

You lose the input data in this way.

Make the 7th line your 5th line and check out the results.

Edited 6 Years Ago by thomas_naveen: n/a

If after resolving the memory issue you are still getting file not found, I will suggest that you take the full path name of the directory

This question has already been answered. Start a new discussion instead.