-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileProcessor.cpp
More file actions
executable file
·112 lines (91 loc) · 2.95 KB
/
Copy pathFileProcessor.cpp
File metadata and controls
executable file
·112 lines (91 loc) · 2.95 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Jaden Heller
// 2328279
// jaheller@chapman.edu
// CPSC-350-01
// Assignment 6
#include "FileProcessor.h"
FileProcessor::FileProcessor(){
fileOpenErr = "Error! Could not open file: ";
}
FileProcessor::FileProcessor(string fileName){
fileOpenErr = "Error! Could not open file: ";
readFile.open(fileName, ios::in); //Try opening an exiting file
//with the fileName
if (readFile.is_open()){
readFile.close();
}
else{ //If it didn't open/it doesnt exist, create a file with the fileName
readWriteFile.open(fileName);
if (readWriteFile.is_open()){
readWriteFile.close();
}
}
}
FileProcessor::~FileProcessor(){
delete linesArray;
}
bool FileProcessor::isTxtFile(string name){
int nameSize = name.size();
string fileExtension;//String that holds the last 4 chars of the entered file name,
//which will be compared against the expected .txt extension
if(nameSize > 4){ //Will always be longer than the 4 characters used in ".txt"
for(int i = nameSize-4; i < nameSize; i++){
fileExtension.push_back(name[i]);
}
}
if (fileExtension == ".txt"){
return true;
}
else
return false;
}
string FileProcessor::getLinesFromFile(string readFileName){
readFile.open(readFileName);
if(readFile.is_open()){
while(getline(readFile, fileAsString)){
return fileAsString;
}
return fileAsString;
readFile.close();
}
else{
cerr << fileOpenErr << readFileName << endl; //If it cant open or can't find the
//specified file, print the error message then exit/don't return any string
exit(1);
}
}
string* FileProcessor::makeArrayFromFile(string readFileName){ //Reurns each line as an
//element in an array
linesArray = new string[getLengthOfFile(readFileName)];
readFile.open(readFileName, ios::in);
if(readFile.is_open()){
while(getline(readFile, fileAsString)){
linesArray[lineCounter] = fileAsString;
lineCounter++; //Increments with each line that's read from the file
}
readFile.close();
}
return linesArray;
}
int FileProcessor::getLengthOfFile(string readFileName){
readFile.open(readFileName, ios::in);
numLines = 0;
if(readFile.is_open()){
while(getline(readFile, fileAsString)){
numLines++; //Increments with each line that's read from the file
}
readFile.close();
}
return numLines;
}
void FileProcessor::writeToFile(string writeFileName, string input1, string input2){
writeFile.open(writeFileName, ios:: out);
if(writeFile.is_open()){
writeFile << input1 << endl;
writeFile << input2 << endl;
writeFile.close();
}
else{
cout << fileOpenErr << writeFileName << endl;
}
}