-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.h
59 lines (45 loc) · 917 Bytes
/
file.h
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
#ifndef FILE_H
#define FILE_H
#include <iostream>
#include <fstream>
using namespace std;
void writeToFile(char const* filename, unsigned lineNo, char const* toWrite)
{
FILE* fileToCreate;
if (fileToCreate = fopen(filename, "r")) {
fclose(fileToCreate);
}
else {
fileToCreate = fopen(filename, "w+b");
fclose(fileToCreate);
}
fileToCreate = NULL;
ifstream input(filename);
string line;
string newFile;
unsigned position = 0;
for (string line; getline(input, line); position++)
{
if (position != lineNo) {
newFile.append(line + "\n");
continue;
}
newFile.append(toWrite);
newFile.append("\n");
}
for (; position <= lineNo; position++)
{
if (position == lineNo) {
newFile.append(toWrite);
}
newFile.append("\n");
}
ofstream myfile(filename);
if (myfile.is_open())
{
myfile << newFile;
myfile.close();
}
else cout << "Unable to open file";
}
#endif