forked from bastelfreak/erp-system
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilehandler.cpp
More file actions
104 lines (93 loc) · 2.1 KB
/
Copy pathfilehandler.cpp
File metadata and controls
104 lines (93 loc) · 2.1 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
/*
Created by Eckhardt, Meusel
Responsible: Eckhardt, Meusel
provides all needed functions for handling files
*/
#include "filehandler.h"
#include <string.h>
#include <iostream>
// Check if filepath exists and can be open
bool checkFileExistence(std::string& filepath)
{
std::fstream f(filepath.c_str());
return f.is_open();
}
// Which file you want to open if no file exists?
std::string getFileName()
{
std::string filename;
std::cout << "Please enter in the name of the file you'd like to open: ";
std::cin >> filename;
return filename;
}
// Open specified file. Create if it doesn't exists
void open(std::string filepath, std::fstream& file)
{
bool file_exists = checkFileExistence(filepath);
if (!file_exists)
{
std::cout << "File " << filepath << " not found.\n";
filepath = getFileName(); //Not found
std::ofstream dummy(filepath.c_str());
if (!dummy.is_open()) {
std::cerr << "Could not create file.\n";
return;
}
std::cout << "File created.\n";
}
else
{
file.open(filepath.c_str());
}
}
/* writeLine("123;Kondome;5.00", "C:/Daten/Software/AlleArtikel.txt") */
// Write the given line in the file
void writeLine(std::string line, std::fstream& file)
{
/* Format der hineingeschriebenen Daten Nummer;Bezeichnung;Preis */
if (file.is_open())
{
file << line << std::endl;
}
else
{
std::cout << "File must be open!" << std::endl;
}
}
// Close file
void close(std::fstream& file)
{
file.close();
}
// Read Data into Struct
void getData(std::fstream& file, TArticle AlleArtikel[1000])
{
if (!file.is_open())
{
std::cout << "File must be open!" << std::endl;
}
else
{
char cText[256];
char * pch;
int i = 0;
int n;
while (!file.eof()){
file.getline(cText, sizeof(cText));
pch = strtok(cText, ";");
n = 1;
while (pch != NULL)
{
if (n == 1)
AlleArtikel[i].id = atoi(pch);
else if (n == 2)
strcpy(AlleArtikel[i].name, pch);
else if (n == 3)
AlleArtikel[i].price = atof(pch);
pch = strtok(NULL, ";");
n++;
}
i++;
}
}
}