Reading/Writing data to a file

Hi,

I'm trying to make a basic program that writes a number to a file as a series of letters, then reads them out again as letters and converts them to an integer again. I've done it through 2 functions. But it doesn't work.. :(

It just prints a load of random numbers...

I've got the code from the file and the output from the console, as well as the contents of the file it writes to.

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
//Code from main.cpp

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>

int getIntFromFile(std::string path) {
    int result;
    std::string storage, converted;
    std::fstream file;
    file.open(path.c_str());

    getline(file, storage);
    transform(storage.begin(), storage.end(), storage.begin(), ::toupper);

    for (unsigned int x=0; x<storage.size(); x++) {
        switch(storage[x]) {
            case 'A': converted.push_back('1');
            case 'B': converted.push_back('2');
            case 'C': converted.push_back('3');
            case 'D': converted.push_back('4');
            case 'E': converted.push_back('5');
            case 'F': converted.push_back('6');
            case 'G': converted.push_back('7');
            case 'H': converted.push_back('8');
            case 'I': converted.push_back('9');
            case 'J': converted.push_back('0');
        }
    }
    result=atoi(converted.c_str());

    file.close();

    return result;
}

bool writeIntToFile(std::string path, int integer) {
    std::string result;
    std::fstream file;
    file.open(path.c_str());
    std::string storage;
    std::stringstream stream;
    stream<<integer;
    stream>>storage;
    for (unsigned int x=0; x<storage.size(); x++) {
        switch (storage.c_str()[x]) {
            case '1': result.push_back('A');
            case '2': result.push_back('B');
            case '3': result.push_back('C');
            case '4': result.push_back('D');
            case '5': result.push_back('E');
            case '6': result.push_back('F');
            case '7': result.push_back('G');
            case '8': result.push_back('H');
            case '9': result.push_back('I');
            case '0': result.push_back('J');
        }
    }

    file<<result;

    file.close();

    return true;
}

int main()
{
    writeIntToFile("test.txt", 203);

    std::cout<<getIntFromFile("test.txt");

    return 0;
}


From output file:
 
BCDEFGHIJJCDEFGHIJ


I don't know why this happened, so I am hoping I can rely on other people's far superior experience!

Hoping you can help me,
hnefatl
It looks like you forgot to use break; in the switch statements.
Hi, again.

This is my appended code:
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

#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>

int getIntFromFile(std::string path) {
    int result;
    std::string storage, converted;
    std::fstream file;
    file.open(path.c_str());

    getline(file, storage);
    transform(storage.begin(), storage.end(), storage.begin(), ::toupper);

    for (unsigned int x=0; x<storage.size(); x++) {
        switch(storage[x]) {
            case 'A': converted.push_back('1'); break;
            case 'B': converted.push_back('2'); break;
            case 'C': converted.push_back('3'); break;
            case 'D': converted.push_back('4'); break;
            case 'E': converted.push_back('5'); break;
            case 'F': converted.push_back('6'); break;
            case 'G': converted.push_back('7'); break;
            case 'H': converted.push_back('8'); break;
            case 'I': converted.push_back('9'); break;
            case 'J': converted.push_back('0'); break;
            default: break;
        }
    }
    result=atoi(converted.c_str());

    file.close();

    return result;
}

bool writeIntToFile(std::string path, int integer) {
    std::string result;
    std::fstream file;
    file.open(path.c_str());
    std::string storage;
    std::stringstream stream;
    stream<<integer;
    stream>>storage;
    for (unsigned int x=0; x<storage.size(); x++) {
        switch (storage.c_str()[x]) {
            case '1': result.push_back('A'); break;
            case '2': result.push_back('B'); break;
            case '3': result.push_back('C'); break;
            case '4': result.push_back('D'); break;
            case '5': result.push_back('E'); break;
            case '6': result.push_back('F'); break;
            case '7': result.push_back('G'); break;
            case '8': result.push_back('H'); break;
            case '9': result.push_back('I'); break;
            case '0': result.push_back('J'); break;
            default: break;
        }
    }

    file<<result;

    file.close();

    return true;
}


However, now it prints
-584078638

Any ideas?

hnefatl
Sorry, I forgot to wipe the file's data; it works perfectly now.

Thanks! :)

hnefatl
Topic archived. No new replies allowed.