-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile.cpp
More file actions
99 lines (87 loc) · 2.16 KB
/
makefile.cpp
File metadata and controls
99 lines (87 loc) · 2.16 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <exception>
#include <fstream>
#include <string>
#include <thread>
#include <vector>
#include <ctime>
#include<sys/shm.h>
#include <sys/ipc.h>
using namespace std;
int main()
{
//take input from the user
string filename;
cout << "Enter the name of the file to be created: ";
cin >> filename;
fstream file;
file.open(filename.c_str(), ios::out);
if (!file)
{
cout << "Error in creating file!!!";
}
else
{
std::string str;
int choice;
std::cin.ignore();
std::cout << "\nBegin from here.....\n";
getline(std::cin,str);
file << str;
file.close();
std::cout << "\nDo you want to save file?\n";
std::cout <<"1.YES 2.NO\n";
std::cin >> choice;
if (choice == 1)
{
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;// converting gb to kb
if (file_size < *shared_memory)
{
*shared_memory = *shared_memory- file_size;
cin.get();
std::cout << "File created successfully!\n";
shmdt(shared_memory);
shmctl(shm_id, IPC_RMID, NULL);
}
else
{
file.open(filename.c_str()); //to format the file
file.close();
std::cerr << " NOT ENOUGH SPACE \n";
return 1;
}
shmdt(shared_memory);
}
else if (choice == 2)
{
std::cerr<<"closed without saving!\n";
}
}
//wait for the user to press key to terminate program
std::cin.get();
return 0;
}