fstream in devc++

Feb 22, 2008 at 5:04pm
Hello all. I've read all the fstream tutorials, and none of the code works in devc++ 4.9.9.2.

For example here is a function I'm working on. The code I have to convert a string to char works. It's the fstream stuff that throws off a "no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::get(char*&)" error.

I'm a bit rusty on c++, so it's probably something simple.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <fstream>
#include "files.h"
/* files.h has getbinpath(), and it works */

using namespace std;

char *playlist[6]; /* I make this dynamic in my code */


int loadplaylist()
{
    std::string plocat = _strdup(getbinpath());
    plocat = plocat + "media\\sound\\";
    std::string pfile = plocat + "playlist.txt";
    char* sofile;
    sofile = new char[pfile.length()+1];
    std::strcpy(sofile, pfile.c_str());
    int lcounter = 0;
    std::ifstream plister(sofile,std::ios::in);
    	char *ch;
	while(!plister.eof())
	{
		plister.get(ch); /* this is where the first error is */
		playlist[lcounter] = ch;
		lcounter++;
	}
	plister.close();
    /* I delete sofile later */
   /* this is code I originally tried: */
	/*
    if(plister.open(pfile))
    {
        if(plister.is_open())
        {
            while(!plister.eof())
            {
                 plister.getline(playlist[lcounter],lcounter);
                 lcouter++;
            }
            plister.close();
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
     }
     */
     return 0;
}
Last edited on Feb 22, 2008 at 5:50pm
Feb 26, 2008 at 7:15pm
fstreams can be used exactly like C++ streams like cin and cout. cin.get() works on a single char argument, not a char*. So the error is giving you the exact problem - there is no method in ifstream (or cin) called get() that accepts a char*. Maybe gets would do what you want, but that's C-style. Try plister >> ch, or getline(plister, ch). And why are you using character arrays? This is C++; that's what std::string is for.
Topic archived. No new replies allowed.