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...

Recommended Answers

All 5 Replies

Try allocating memory to your dir variable. Similar to the command variable.

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.

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

Kindly let us know whether your issue is resolved?

If so, mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.