file i/o

I'm working on a profiles type of system and this is my latest build. It works and everything but when i cout the data from the profile its all these weird characters.

the files have data like this:
name level score
john 2 1000

heres the code. I think its the way I'm breaking up the string thats screwing it up. the code to put default data in the file when its empty works.

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

using namespace std;

int main()
{
    ofstream oFile;
    ifstream iFile;

    string Score = "0";
    string Level = "1";
    string Name;
    string FileName;
    string Input;
    string FileData;

    int Counter = 0;

    cout << "Enter your name to load or create a profile." << endl;
    cin >> Input;

    Name = Input;

    FileName = "Profiles/" + Input + ".pfs";

    iFile.open(FileName.c_str());
    oFile.open(FileName.c_str(), ios::app);

    getline(iFile, FileData);

    if(FileData == "")
    {
        oFile << Name << " " << Level << " " << Score;
    }

    else if(FileData != "")
    {
        while(isspace(FileData[Counter]) == false)
        {
            Name[Counter] = Name[Counter] + FileData[Counter];

            Counter++;
        }

        Counter++;

        while(isspace(FileData[Counter]) == false)
        {
            Level[Counter] = Level[Counter] + FileData[Counter];

            Counter++;
        }

        Counter++;

        while(isspace(FileData[Counter]) == false)
        {
            Score[Counter] = Score[Counter] + FileData[Counter];

            Counter++;
        }
    }

    cout << Name << endl;
    cout << Level << endl;
    cout << Score << endl;

    iFile.close();
    oFile.close();

    return 0;
}
Last edited on
These lines are nonsensical:

1
2
3
            Name[Counter] = Name[Counter] + FileData[Counter];
            Level[Counter] = Level[Counter] + FileData[Counter];
            Score[Counter] = Score[Counter] + FileData[Counter];


These lines say to look at the (Counter)th element of the string, and ADD TO THAT CHARACTER the character in the file.
So in other words, 'A' + 'B' == ASCII (65 + 66) == ASCII 131, which is not a letter.

Oh I see. I'm not very familiar ti ASCII, I'll try find a different approach. What if I put each variable on a line like this:

john
2
1000

how would I move to the next line. Isn't there like a pointer or something that can move through the file in the fstream library?(seek?)
Last edited on
Topic archived. No new replies allowed.