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?
 
nietodConnect With a Mentor Commented:
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.