-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.cpp
168 lines (141 loc) · 2.87 KB
/
file.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Author: Justin Muskopf
* Instructor: Mark Hoffman
* Course: CSCE 4550, Fall 2018
* Assignment: Project 2
*/
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
#include "file.h"
//
// brief: Default constructor
//
File::File()
{
}
//
// brief: Initializing constructor
//
File::File(std::string fileType)
{
type = fileType;
}
//
// brief: Open the file of filename with openMode readWrite
//
// param: filename - The filename of the file
// param: readWrite - The open mode of the file
//
int File::open(std::string filename, FileOpenMode readWrite)
{
// The return code
int ret;
// Open file in proper mode
switch(readWrite)
{
case READ:
file = fopen(filename.c_str(), "r");
break;
case WRITE:
file = fopen(filename.c_str(), "w+");
break;
default:
return FILE_FAILED_TO_OPEN;
}
// File was opened
if (file)
{
ret = FILE_SUCCESS;
}
// Error
else
{
ret = FILE_FAILED_TO_OPEN;
}
return ret;
}
//
// brief: Opens a file from stdin
//
// param: readWrite - The mode to open the file in
//
int File::openFromInput(FileOpenMode readWrite)
{
// String to store filename
std::string filename;
// Prompt for filename
std::cout << "Please enter the name of the " << type << " file: ";
std::cin >> filename;
open(filename, readWrite);
// Loop until a file is opened
while (!file)
{
std::cout << "Unable to open '" << filename << "'. Please try again: ";
std::cin >> filename;
open(filename, readWrite);
}
}
//
// brief: Sets the file's type
//
// param: fileType - the file's type
//
void File::setFileType(std::string fileType)
{
type = fileType;
}
//
// brief: Close the file
//
int File::close()
{
int ret = FILE_FAILED_TO_CLOSE;
if (!file)
{
return FILE_SUCCESS;
}
fclose(file);
if (!file)
{
ret = FILE_SUCCESS;
}
return ret;
}
//
// brief: Return a string of the file's contents
//
std::string File::getContents()
{
std::string contents; // The string to hold contents
char line[MAX_LINE_LEN + 1]; // The c-string temp to hold interim
// While the file has more contents, continue to read
while (fgets(line, MAX_LINE_LEN, file))
{
// Terminate line if it came through with a newline
int len = strlen(line);
if (len > 0 && line[len - 1] == '\n')
{
line[len - 1] = '\0';
}
contents.append(line);
}
return contents;
}
//
// brief: Returns whether or not a file is open
//
bool File::isOpen()
{
return file == NULL;
}
//
// brief: Writes buffer to the file
//
// param: buffer - the buffer to write to the file
//
int File::write(std::string buffer)
{
return fputs(buffer.c_str(), file);
}