Okay I have a small problem. I have to load a file which contains (for example):

a bleh
b blah
c bloh

So basically it contains a character(1,2,3,..etc) followed by a string (bleh,blah,bloh etc..)
I have to create a struct which should contain a char and a string, they will represent the character and string from the file respectivly(sp?).

What I am required to do after is basically read a new file (with random text in it) which will inturn use the struct I have created to "encode" the contents of my new file with the string of the sctruct file.

My code so far is:

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;


struct letterTocode
{
	char letter;
	string to_code;
};

int main (int argc, char *argv[])
{

	ifstream codefile("blahblehbloh.txt");
	vector<letterTocode> toCode;
	string line;
	ifstream myfile,encoded_file;
               
	
	if (blahblehbloh.is_open())
	{
		while(!blahblehbloh.eof())
		{
		getline (blahblehbloh,line);
		cout << line << endl;
		}
			blahblehbloh.close();
	} 
	encode(myfile);
                print(encoded_file);

	return 0;
}

I am going to have a new function called encode() which will:
- Read the struct
- "encode" the new file with the codefile(blahblehbloh.txt)


Right now I am stuck at the creating of my Struct itself, I've read a few Struct examples, they had similar layout for the Struct but I do not think that the way I layed out my Struct is correct. As of this moment I don't see how I will be able to read the single character, store it in the struct char and then read the string (on the same line) and store that in the string in Struct and then use that Struct to encode the new file opened.


>_>

Your struct looks fine.
It's the opening and read of the file I'm worried about.
"blahblehbloh" is not a variable. And why do you need 3 ifstreams?
And I'm not a big fan of infile.eof() either

Here's an example to get you on your way:

string line;
ifstream file("blahblehbloh.txt");
if (file)
{
    while ( getline(file, line) )
    {
         // do stuff (like pushing vector etc)
    }
    // end of the file is here
}

the if streams will be:

1. blahblebloh , the code file
2. A file with some text
3. An empty file which will write the contents of ifstream 2 which will be encoded with the struct of ifstream1

then the third has to be an ofstream not an ifstream.

blahblebloh , the code file

But you called that ifstream "codefile" not blahblebloh, so your code won't compile

then the third has to be an ofstream not an ifstream.


But you called that ifstream "codefile" not blahblebloh, so your code won't compile

pardon, my mistake, the 1. blahblebloh, the code file was worded badly.

Basically the ifstream will contain the code file (which is the .txt file), another file with some text which will be read in, encoded with the code file using the struct and spewed out into a new file (which as you pointed out will be an ofstream).

Right now I got the code to load and display just the code file (blahblehbloh.txt) (the reason for me to display the code file is so that i know it loaded :)) so now going to try to figure out how to assign all the stuff i need from the code file into my structs :).

Thanks for the help so far

Look into the 'substr' function of the string class, you can easily pull out sections of the string (since you know the structure) to populate your structure.

This article has been dead for over six months. Start a new discussion instead.