-1

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.

user326964
  • 406
  • 1
  • 5
  • 10
  • What the hell do you mean? Can you rephrase? – RvdK May 03 '10 at 15:10
  • It sounds a bit scary to me, writing a program without knowing the basic types of the language. – extraneon May 03 '10 at 15:23
  • it is,I'm no good at c++ at all but I have to learn somehow if the teacher won't help me. I'm good at javascript though. X) – user326964 May 03 '10 at 15:27
  • The accepted answer below is excellent from a learning perspective, but for future reference, here is another question about ways to pull the whole content of a file into a single string: http://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring/2602060#2602060 – Tyler McHenry May 03 '10 at 15:31

3 Answers3

3

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"; 
    }
}
1

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.
  • 139,555
  • 58
  • 313
  • 529
  • 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. – user326964 May 03 '10 at 15:21
0
#include <iostream>
#include <fstream>
#include <string>

int main() {
   // Open stream from file
   std::ifstream ifs("foo.txt");

   // Get file contents
   std::string file_contents(
      (std::istreambuf_iterator<char>(ifs)),
      std::istreambuf_iterator<char>()
   );

   // Output string to terminal to see that it works
   std::cout << file_contents;
}
Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989