problem with ifstream

Here is a snippet of my program:
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
54
55
56
57
58
59
60
#include <iostream>
#include <cstring>
#include <string>
#include <sstream>

using namespace std;

//The purpose of this program is to take a line of binary ASCII code and convert it to plain-text that the user can read.
//This program will accept the code in two different formats: a format asking for direct input from the user, and one
//asking for the user to specify a file with the ASCII code. The user will have to specify which one to use.

//If the user specifies the direct input method, things will be easier for the program but harder for the user. This is
//primarily because the user has to input the code verbatim, and there is probably no copy and paste feature in ANY C++
//program. The code will be then translated on-screen. However, if the user tells this program a filename, the program is
//then to open it, parse the data,

class ASCIItext
{
    private:
        string response;
        string filename;
        string rawdata;
        int numberdata;
        char youdone;
    public:
        ASCIItext();
        string translatefile();
        string translatetext();
};   //end class

ASCIItext::ASCIItext()
{
    numberdata = 0;
    rawdata = "";
    response = "";
    filename = "";
    youdone = ' ';
}   //end constructor

string ASCIItext::translatefile()
{
    cout<<"Please enter the name of the file to be translated (must be in the same directory as this program): "<<endl;
    getline(cin, filename);
    cout<<"Searching for file..."<<endl;
    ifstream codefile;
    filename = filename + ".txt";   //appending a .txt to the filename
    codefile.open(filename.c_str());    //Opening the file
    //For test purposes, we're going to just display the contents on the screen, for now...
    if (codefile.is_open())
    {
        while (codefile.good())
        {
            getline(codefile, rawdata);
            stringstream(rawdata)>>numberdata;
            cout<<"This file contains the following data: "<<rawdata<<endl;
        }   //end while
        codefile.close();
    }   //end if
}   //end translatefile

The main() function was working until I created the translatefile() function. All I was trying to do was allow the user to input the filename to be opened. Yet, it complains that ifstream codefile has incomplete type and cannot be defined. Why?
Last edited on
Just from glancing at this, I see you forgot to include fstream. That should fix you up
It does! Thanks! I might be having other problems. Problems such as the freeze I get when running this program. Also, how do you get the program to handle the data in binary? I tried ios::binary, but isn't that just for .bin files?
Last edited on
If the calling code is depending on translatefile() to return a string like it says it's going to, that's probably the problem, since you don't actually return one.

The while loop in translatefile() would be less error-prone if it were:

1
2
3
4
5
        while (getline(codefile,rawdata))
        {
            stringstream(rawdata)>>numberdata;
            cout<<"This file contains the following data: "<<rawdata<<endl;
        }   //end while 


The other way, if getline should fail you're still trying to extract data from rawdata as if it hadn't.

ios::binary is for processing files in a binary fashion. The extension of the file isn't important.

What exactly are you trying to accomplish here? Assuming the file has more than one line you assign multiple values to numberdata, only the last of which is saved.
Last edited on
I am trying to take a sequence of 0s and 1s from a file and convert it, using ASCII, to text. For example, 0100100001001001 would be HI.

-Update- I have decided to go pseudo-binary on this one. By the way, stringstream is not working on this...

Oh, and in a 2-dimensional array, can you define an ENTIRE ROW at once? For example,
<code>
char asciichar[8][16];
asciichar [3] = {//insert character string here};
Last edited on
Topic archived. No new replies allowed.