Simple file IO

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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
void save();
void load();
int y;


int main(int argc, char *argv[])
{
int choice;
y = 0;
         

cout << "Load previous results? 1)yes 2)no\n";
               cin >> choice;
               switch (choice){
               case 1:
                    load();
                    break;
               default:
                    break;
             }
if (y == 0)
{
cout << "Enter y";
cout << endl;
cin >> y;
}

cout << "Y " << "= " << y << endl;
cout << "Do you want to save results? 1)Yes 2)No\n";
cin >> choice;
switch (choice){
       case 1:
           save();
           break;
       default:
             break;
             }


    system("PAUSE");
    return EXIT_SUCCESS;
}

void load(){
     ifstream fin ("save.txt");
     fin >> y;
     fin.close();
     }

void save(){
     ofstream fout ("save.txt");
     fout << y;
     fout << flush;
     fout.close();
}


I wrote this ages ago, when I new what I was doing. I understand most of it still (I really need to comment my code). I have another version were you could save strings, but I have over 3400 emails archived and the one with it I can't seem to find. Anyways, hope someone finds this useful.
I'm sorry to say this, but it's crap.
I know its crap, it was.. nvm, I was hoping somebody would get some use out of it. I wrote it when I was learning it. No offense though. Can you tell me how you would write it? I yearn to learn.
Can you tell me how you would write it? I yearn to learn.
Alright. This was a test, and you passed.

1. No globals. I/O functions should get all their information from passed parameters.
2. Always check whether the file was opened.
3. It's never a bad idea to pass the file name as a parameter.
4. When writing text files without format (example of a formatted text file: INI), write data separated by newlines, even if it's just one datum.
5. Use templates.

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
template <typename T>
void write(const std::ostream &stream,const T &datum){
	stream <<datum1<<std::endl;
}

//Note: Don't extrapolate this to use many parameters. More than 3, and it's probably time to pass a structure.
template <typename T1,typename T2>
void write(const std::string &name,const T1 &datum1,const T2 &datum2){
	std::ofstream file(name.c_str());
	if (!file.is_open())
		return;
	write(file,datum1);
	write(file,datum2);
}

template <typename T>
void read(const std::istream &stream,const T &datum){
	stream >>datum;
	//I'm not entirely sure if the above line leaves a newline in the input
	//buffer. If it does, use the code below, instead:
	std::string line;
	std::getline(stream,line);
	std::stringstream s(line);
	s >>datum;
}

//I've used template specialization one twice before, so the syntax may be a
//little off. I think it's right, though.
template <>
void read <std::string>(const std::istream &stream,const T &datum){
	std::string line;
	std::getline(stream,line);
	datum=line;
}

template <typename T1,typename T2>
void read(const std::string &name,const T1 &datum1,const T2 &datum2){
	std::ifstream file(name.c_str());
	if (!file.is_open())
		return;
	read(file,datum1);
	read(file,datum2);
}


This applies to text files. Writing generic functions for binary I/O is much more straightforward.
Ok, i'll look at it. I have not gotten to templates yet. I'm still pretty new. Glad I passed the test.
Topic archived. No new replies allowed.