Taking from a file and storing it in two seperate places?

Is there a way to take input from a file, and if each line begins with $ it gets stored in one set of data, and if it begins with @ it gets stored in another?
Here's what I have:
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
#include <iostream>
using namespace std;
#include <fstream>
#include <conio.h>
#include "progclass.cpp"

program_t player1;
program_t player2;

int main(int argc, char *argv[])
{
    string heldcmd;
    int i=0;
    char temp;
    fstream playerdata;
    playerdata.open("player1.txt");
    if( playerdata.is_open() )
    {
        while (! playerdata.eof() )
        {
            temp = playerdata.peek();
            if(temp == '$')
            {
                getline (playerdata,player1.includes[i]);
            }
            else if(temp == '@')
            {
                getline (playerdata,player1.commands[i]);
            }
            else
            {
                getline (playerdata,heldcmd);
                cout << "ERROR: Invalid Command Entered\n";
            }
            i++;
        }
    }
    playerdata.close();
    getch();
    return 0; 
}

Progclass.cpp:
1
2
3
4
5
6
7
8
9
#ifndef PROGCLASS
#define PROGCLASS

typedef struct program_t{
       string commands[10];
       string includes[5];
};

#endif 


It just prints an error message to the console, even though they all have $ or @ in front of them
Last edited on
getline works with strings, player1.includes[0][0] is a char
Yeah, I fixed it and edited my post.
Now the new problem I got is that it won't accept my input file. I just says "ERROR: Invalid Command Entered"
I think your problem is with your "player1.txt" file, try to modify line 33 as this:
1
2
 cout << "Error at line " << i << ", Invalid Command Entered: \"" << heldcmd << "\" first character : "<< temp << " (" << (int)temp << ')' << '\n';

so that you will see what's wrong with that file.
(your program will show that error message each time a new line doesn't start with '@' or '$')
Last edited on
Error at line 3, Invalid Command Entered: "" first character :   (-1)

I found out what was wrong, I was using Dev-C++ to compile and I had opened player1.txt in it. It automatically appends a newline when I save for some reason *sheepishgrin*

Thank you, that DID help me quite a bit.

EDIT:
Now when I try calling my function to open a second file, it doesn't run!
I can only open one file, even if I close the previous file!
I even tried making a second fstream, but nothing happened!

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
void loadplayers()
{
    string heldcmd;
    int i=0,j=0,k=0;
    char temp;
    fstream playerdata;
    playerdata.open("player1.txt");
    if( playerdata.is_open() )
    {
        while (! playerdata.eof() )
        {
            temp = playerdata.peek();
            if(temp == '$')
            {
                getline (playerdata,player[0].includes[i]);
                cout << "Included function list:" << player[0].includes[i] << "\n";
                i++;
            }
            else if(temp == '@')
            {
                getline (playerdata,player[0].commands[j]);
                j++;
            }
            else if(temp == '#')
            {
                getline (playerdata,heldcmd);
            }
            else
            {
                getline (playerdata,heldcmd);
                cout << "Error at line " << k << ", Invalid Command Entered: \"" << heldcmd << "\" first character : "<< temp << " (" << (int)temp << ')' << '\n';
            }
            k++;
        }
    }
    playerdata.close();
    getch();
    system("cls");
    i=0;
    j=0;
    k=0;
    playerdata.open("player2.txt");
    if( playerdata.is_open() )
    {
        while (! playerdata.eof() )
        {
            temp = playerdata.peek();
            if(temp == '$')
            {
                getline (playerdata,player[1].includes[i]);
                cout << "Included function list:" << player[1].includes[i] << "\n";
                i++;
            }
            else if(temp == '@')
            {
                getline (playerdata,player[1].commands[j]);
                j++;
            }
            else if(temp == '#')
            {
                getline (playerdata,heldcmd);
            }
            else
            {
                getline (playerdata,heldcmd);
                cout << "Error at line " << k << ", Invalid Command Entered: \"" << heldcmd << "\" first character : "<< temp << " (" << (int)temp << ')' << '\n';
            }
            k++;
        }
    }
    playerdata.close();
    getch();
    return;
}
Last edited on
Try calling playerdata.clear() in between each close() and open()...if that doesn't work...I don't know what the problem is...make another fstream I guess.
Thanks! That .clear bit fixed it!

EDIT AGAIN:
So, say I have a string like this, taken with the above code:
 
@data 0 0

How can I parse it into the word "data" and the number 0 stored as an int?
EDIT++:
And is there a way to #define a comment? Like, make COMMENTZ represent a comment?
Last edited on
I hate to double-post, but does anyone know how to help with these two problems? The letter-meaning-a-comment and the string splitting into a string and two ints?
Whith comment you mean to skip the file line?
try with:
1
2
3
4
5
if( temp==/*comment char*/)
{
    playerdata.ignore(1024,'\n');
    continue;
}


For getting two numbers, try:
1
2
3
4
5
6
7
8
9
int somenumbers[2], i=0;
while(true)
    temp = playerdata.peek();
    if (temp=='\n') break;
    if (isnumeric(temp))
        playerdata >> somenumbers[i++];
    else
        playerdata.get();
}
Last edited on
For the comments, I mean making the letters BTW represent // and actually work as a comment.

I'll test that code out, but it looks like that will work. Thanks again!

EDIT: Umm...I hate to be a noob, but that's for a file. What if I want it to be taken from the strings thatwere made in that file?
Last edited on
I REALLY hate to necropost, but both of these are important for my project. Also, unless I'm wrong that would end up only containing the last letter of the word...
I mean making the letters BTW represent // and actually work as a comment
#define BTW // Won't work, why do you need that?

What if I want it to be taken from the strings that were made in that file?

You mean that you got a string from the file and use it as a stream?
If so, you should look at stringstreams ( <sstream> ) http://www.cplusplus.com/reference/iostream/stringstream/
Last edited on
*sheepishgrin*
lolcode

Thanks, I think stringstreams are just what I needed.
Well, you can still
1
2
#define KTHNXBAI exit(0);
#define OHNOES throw 
Last edited on
I already have most of the basics implemented, but thanks for the throw idea.
The tough part is the comments: I need BTW to represent //, OBTW to mean /*, and TLDR to mean */
Nice throw...catch idea. I'll list you as offering that idea.

I'm having trouble, as bad as it sounds, just getting the next letter in the file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void EncryptFile(char filename[])
{
    long j;
    char* plain;
	fstream filetoencrypt;
	filetoencrypt.open(filename);
	if( filetoencrypt.is_open() )
	{
        while( !filetoencrypt.eof() )
        {
            filetoencrypt.get(plain[j]);
            j++;
        }
        filetoencrypt.close();
    }
    for(int i=0;i<j;i++)
    {
        cout << plain[i];
    }
}


I tried throwing and catching a lot, and learned that it's stumbling on filetoencrypt.get(plain[j]);
Last edited on
Topic archived. No new replies allowed.