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

Dani AI

Generated

Brief expert note: the original crash and the strange "File not found - Ä" come from reading into an uninitialised or wrong-sized C buffer and from not handling spaces in paths. As pointed out, allocate (or better: use a safe string type) before storing input; and as suggested, use the full path. The fastest, safest fix is to stop using raw char* and sprintf for command construction.

A minimal, safe example that keeps the attrib approach (Windows) — reads the whole line, quotes the path (so spaces are handled), and shows the command for debugging:

#include <iostream>
#include <string>
#include <cstdlib>

int main() {
    std::string path;
    std::cout << "Enter folder location > ";
    std::getline(std::cin, path);
    if (path.empty()) return 1;
    std::string cmd = "attrib +r \"" + path + "\"";
    std::cout << "Running: " << cmd << '\n';
    int rc = std::system(cmd.c_str());
    std::cout << "system returned " << rc << '\n';
    return 0;
}

Better approach (recommended for production) is to call the platform API directly instead of invoking a shell. On Windows, use GetFileAttributesW / SetFileAttributesW to add FILE_ATTRIBUTE_READONLY (and use wide strings for Unicode paths):

#include <windows.h>
#include <string>

bool setReadOnly(const std::wstring &p) {
    DWORD a = GetFileAttributesW(p.c_str());
    if (a == INVALID_FILE_ATTRIBUTES) return false;
    a |= FILE_ATTRIBUTE_READONLY;
    return SetFileAttributesW(p.c_str(), a) != 0;
}

Notes and troubleshooting: wrap paths in quotes when using system(); check return codes; print the command for debugging; prefer std::getline to capture spaces; avoid manual new[]/delete[] to prevent leaks and UB; remember the read-only attribute on a folder is treated specially by Explorer and does not enforce NTFS ACLs — to prevent write access change file/ACL permissions or use std::filesystem::permissions (C++17) / proper ACL APIs for robust permission control.

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.