Need help with how to use ifstream &operator >>(ifstream &in,DATA &d);

dsf
Last edited on
Can you provide me with the contents of your text file?

if (argc = 2)

You should use equality comparison operator (==) instead of assignment operator (=), like this.

if (argc == 2)

Why do you have this line in your function main?

ostream &operator >>(ostream&,DATA &);
Last edited on
the infile text only a few lines

1 john smith 2.0
2 king kong 3.0
3 kevin sim 2.5

Why do you have this line in your function main?

ostream &operator >>(ostream&,DATA &);


i forgot to remove this. Its just a mistake and shouldnt be there.
1
2
3
4
5
6
7
8
if (argc == 2)
    {
        ifstream in;
        in.open(argv[0]);

        ofstream out;
        out.open(argv[1]);
    }

Where do you use the insertion operators >> and <<? You need to use them to test whether they actually work or not.

Also, since you may not necessarily know the file, it should be better to check if the files are open before doing other things.
Your default ctor definition (lines 22-25) would cause problems because moment you declare a DATA object it'd need to be read in from a file into a Data object that, in turn would call it's own ctor and so on ad infinitum. In this program you can just use the default compiler supplied ctor and remove the above lines
For printing to console you'd need std::ostream, std::ofstream (line 17) would be for insertion to files
For class methods like DATA::setAll(...) (line 15) const qualify the arguments, so it should be DATA::setAll(const int& k, const std::string& fname, const std::string& lname, const double& G)
In main() one way to read the file directly into DATA objects would be along following lines:
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

class DATA
{
    private:
    int m_key;
    std::string m_firstName;
    std::string m_lastName;
    float m_GPA;

    public:
    friend std::ifstream& operator >> (std::ifstream& is, DATA& d);
    friend std::ostream & operator << (std::ostream& os, const DATA& d);
};

std::ifstream& operator >> (std::ifstream& is, DATA& d)
{
    is >> d.m_key >> d.m_firstName >> d.m_lastName >> d.m_GPA;
    return is;
}
std::ostream & operator << (std::ostream& os, const DATA& d)
{
    os << d.m_key << " " << d.m_firstName << " " << d.m_lastName << " " << d.m_GPA;
    return os;
}

int main()
{
    std::ifstream inFile ("D:\\input1.txt");
    DATA d{};
    std::vector<DATA> datum{};
    while (inFile >> d)
    {
        if(inFile)
        {
            datum.push_back(d);
        }
    }
    for (auto& elem : datum)
    {
        std::cout << elem << '\n';
    }
}
Last edited on
Note that argv[0] is the name that was used to invoke your program.

If you run your program as ./prog file1 file2 the the values of argc and argv will be as follows.

1
2
3
4
5
argc = 3
argv[0] = "./prog"
argv[1] = "file1"
argv[2] = "file2"
argv[3] = nullptr
Last edited on
Topic archived. No new replies allowed.