Need good sample code to read a file into a string.

I'm a VB person trying to learn some C++, so I'm quite new to this.
This is my current code.  However, it does not do exactly what I want.  I need a good example of how to use the getline command to put the contents of a file into a string.

#include <iostream.h>
#include <fstream.h>
void main(void)
{
  ifstream daFile;
  char daVariable;

  daFile.open("YOURFILE.txt", ios::in);
  if (daFile.bad() !=0)
  {
    cerr << "Failed to open!\n";
    return;
  }

  while (daFile.good())
  {
    daFile >> daVariable;
    if(!daFile.good()) break;
    cout << daVariable;
  }

  daFile.close();
}

TrytanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
That  looks fine, but of course doesn't use a strig.  it just processes one character at a time.   If you want to use the STL string class, then you would do

#include <string>
using namespace std;

   *   *  *

string stringVariable;

while (daFile.good())
{
   getline(daFile,stringVariable);
   if(!daFile.good()) break;
   cout << stringVariable;
}


Let me know if you have any questions.   (or if you meant a "char *" string.)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TrytanAuthor Commented:
Ok, here is what I tried using the code, as I said, i'm new to c++.  Thanks for your quick reply.  If you can correct this code to work, I will give you a total of 100 points. Thanks.

#include <iostream.h>
#include <fstream.h>
#include <string.h>
using namespace std;

void main(void)
{
  ifstream daFile;
  string stringVariable;

  daFile.open("YOURFILE.txt", ios::in);
  if (daFile.bad() !=0)
  {
    cerr << "Failed to open!\n";
    return;
  }
 
  while (daFile.good())
  {
    getline(daFile,stringVariable);
    if(!daFile.good()) break;
    cout << stringVariable;
  }
}

Here are my errors on compile:
(4) : error C2871: 'std' : does not exist or is not a namespace
(9) : error C2065: 'string' : undeclared identifier
(9) : error C2146: syntax error : missing ';' before identifier 'stringVariable'
(9) : error C2065: 'stringVariable' : undeclared identifier
(20) : error C2065: 'getline' : undeclared identifier

 5 error(s), 0 warning(s)
0
TrytanAuthor Commented:
Adjusted points to 100
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

nietodCommented:
It should be

#include <string>

not

#include <string.h>

the .h version is the C-string (char *) functions.  the extension-less one is the C++ string class functions.  Are you familiar with the STL string class?  It is much much more powerful than the old C-strings.
0
nietodCommented:
Also you should use

#include <iostream>
#include <fstream>

Again, no .h.  the .h versions are old, non-standardized versions.  They may not be available in the future and may not behave completely "correctly".
0
TrytanAuthor Commented:
Thanks for replying so quickly.  It works great now.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.