tags:

views:

68

answers:

2

If I made a program that stores strings on a text file using the "list"-function(#include ), and then I want to copy all of the text from that file and call it something(so I can tell the program to type in all of the text I copied somewhere by using that one variable to refer to the text), do I use a string,double,int or what do I declare that chunk of text as?

I'm making the program using c++ in a simple console application.

Easier explanation for PoweRoy:

I have a text in a .txt file,I want to copy everything in it and then all this that I just copied, I want to call it "int text" or "string text" or whatever.But I don't know which one of those "int","string","double" etc. to use.

+1  A: 

Broadly speaking, you are talking about the concept of Serialization - storing variable values in permanent storage like a file so you can reload them later. Have a look at that link to broaden your understanding.

Specifically, it sounds like you have arbitrary text in a file and want to refer to it in your program. In that case, string sounds appropriate. Unless the text in the file is intended to represent one single number, that seems most appropriate.

Note that if you have structured data (like a CSV file or XML file), a more complex data structure (e.g. a class, array of classes, etc) might be a better choice.

Eric J.
I didn't quite intend to have a single number,since this text will store several strings and on command it will use a function which will save all of the strings on a .txt file as one single text.And it is bigger than just one character/number.
+3  A: 

To take some pity on you, this is about the simplest C++ program that reads a file into memory and then does something with it:

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

int main() {

    ifstream  input( "foo.txt" );
    if ( ! input.is_open() ) {
        cerr << "could not open input file" << endl;
        return 1;
    }

    vector <string> lines;
    string line;
    while( getline( input, line ) ) {
        lines.push_back( line );
    }

    for ( unsigned int i = 0; i < lines.size(); i++ ) {
        cout << (i+1) << ": " << lines[i] << "\n"; 
    }
}
anon
If you remove error handling it will get even simpler!
extraneon
oooooooooooooh now I get it :P thanks for helping me
@extraneon Never, ever remove the error handling - doing so will make life more complicated, not simpler.
anon
But going through a core dump is so much more fun...
extraneon