-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeletefile.cpp
More file actions
57 lines (49 loc) · 1.33 KB
/
deletefile.cpp
File metadata and controls
57 lines (49 loc) · 1.33 KB
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
#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include<string.h>
#include <fstream>
#include<sys/shm.h>
#include<sys/ipc.h>
using namespace std;
//Delete a file
int main()
{
int file_size = 200;
int shm_id;
key_t key = ftok(".", 'R');
shm_id = shmget(key,1024, IPC_CREAT | 0666);
if (shm_id == -1)
{
std::cerr << "Failed to create shared memory.\n";
return 1;
}
int *shared_memory = (int *)shmat(shm_id, NULL, 0);
if (shared_memory == ( int*)-1)
{
std::cerr << "Failed to attach shared memory.\n";
return 1;
}
*shared_memory = *shared_memory * 1024;
int status;
std::string fileName;
cout << "Enter the Name of File: " << endl;
getline(std::cin,fileName);
std::string str;
std::ifstream file;
status = remove(fileName.c_str());
if (status == 0)
{
file.open(fileName, ios::binary | ios::ate);
int file_size = file.tellg();
file.seekg(0);
*shared_memory = *shared_memory- file_size;
cout << "\nFile Deleted Successfully!" << endl;
}
else
cout << "\nErorr Occurred!" << endl;
cout << endl;
std::cin.get();
return 0;
}