I am trying to drag a program originally written in Borland C version 3 into the light of the present using Visual C++ 2010.
I have a curious, apparently simple, problem.
The program read data from a file.
I can create a noddy program that opens and reads the contents of a file.
However, when I transfer that code into the program to be converted it complains about the getline function.
The noddy program is

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <conio.h>
#include <ctype.h>
#include <direct.h>
#include <stdio.h>

#include <fstream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

 char* path = "c:\\DrugData\\DrugData.txt";
 string line;
  ifstream myfile (path);
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }
}

but when used in the converted program

strcpy_s(filename, "C;:\\DrugData\\DrugData.txt");
   ifstream fp(filename);
  	getline(fp, buffer);

I get the error message:
c:\users\norman\documents\visual studio 2010\projects\drug1\drug1\drug1.cpp(114): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'
1> c:\program files\microsoft visual studio 10.0\vc\include\string(479) : see declaration of 'std::getline'

In case you're worried about the drug reference, I am diabetic and take a lot of pills to control it, and the program helps me maintain control over the quantities of pills I have left.

Recommended Answers

All 11 Replies

How have you defined "filename"? Is it a C-Style string (null-terminated array of char) or a std::string object?

Have you tested that your ifstream is in fact open?

You should probably check the literal that you are using to define your path. You appear to have an extraneous semi-colon in there...

"C;:\\DrugData\\DrugData.txt"

Whoops, sorry about that. I have modified the code to the following

strcpy_s(filename, 80, "C:\\DrugData\\DrugData.txt");
   ifstream fp(filename);
   if (fp.is_open() == 0)
   {
       cout <<"Error in opening file " << filename << "error code = " << errno;
       exit(1);
   }

  	getline(fp, buffer);

and still get the same error on compilation.
Filename is a char array.
buffer is defined as char buffer; where SIZE is a #define as 60. I assume that this is still permitted?

Just noticed something. The error on the line associated with getline' actually occurs SIX times with error codes C2784/C2784/C2780/C2784/C2784/C2780.
Isn't that overkill?

the std::getline function puts its output into a std::string, the std::ifstream::getline function puts its output into a char*. You should call getline like fp.getline(buffer, SIZE);

I was getting to that. That's why I asked about the definition of filename. I didn't want to tell the OP to change it if it was correct.

...
buffer is defined as char buffer; where SIZE is a #define as 60. I assume that this is still permitted?

In newer code, it's permitted, yes; it's not really recommended though. Most will say that you're better off making SIZE a const int rather than a #define.

That helped a lot. Thanks.
I've been able to move on quite a long way, so the sun is definitely starting to rise. Still have about six more compilation errors to deal with, including a very messy structural (not structure) problem (caused by commenting out a lot of code to get the basic functionality working). That's for me to work through.
The next big step will be to convert it from a console application to a proper 'window' application. I think I have some code examples to work through before that can take place. Any suggestions on where to start with that?

Google "The Forger". That should get you to a WindowsAPI Tutorial. Just be ready to absorb a lot of information.

Now that is what you call serious reading! Thank you for that.
Now all I have to wish for is a Free C++ compiler that does include MFC (or its equivalent).
As I said at the beginning I'm using Visual C++ 2010 Express (That at least is free)!

If you're a college student in Comp. Sci., check if your school provides Microsoft Developer Network Academic Alliance (MSDNAA) accounts. You usually need to request access through your instructor, then they'll facilitate creation of an account for you. You can get fully-licensed versions of MS software very cheap, and in some cases, for free. Obviously, you can't use it for commercial purposes, but for personal and/or educational uses, it's a nice cheap possibility.

Unfortunately I'm a bit old for college, so that isn't really an option!

That completes this part of the exercise. The next set of problems will be a new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.