How to send data to file

i have a quaetion regarding file in c++. if i want to send a data to file , i will do

1
2
 cout << " Year : " << year << endl;
 toFile << " Year : " << year << endl;


but this didn't work with cin. itry to do exactly the same but it said no match for operator . i did it like this

1
2
 cin >> name;
 toFile >> name;


i have one more question. if i have many functions and i want to send the cout data in functions to file. but my functions are like this

1
2
3
void Highest (ifstream & theFile);
void Average (ifstream & theFile);
void Lowest  (ifstream & theFile); 


what should i fix ?
output files are ofstream, input are ifstream ... have you mixed them up somewhere??
Last edited on
how to mix them up ? i only do this at main function :

1
2
3
      ifstream nameFile("data.txt");         
      ofstream toFile;
      toFile.open("output.txt");
http://sscce.org/
Random one-liners which are otherwise correct based on a whole load of assumptions are not going to solve this problem.

You're screwing up in places you're not showing us.
toFile >> name;
Trying to read from an output stream. Ain't gonna work.
this is my example coding but i have more function

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
61
62
63
64
65
#include <iostream> 
#include <fstream>  
#include <string>
#include <cmath>    
#include <iomanip> 
#include <limits>

using namespace std;

void Highest (ifstream & theFile);


int main()
{
        string data;
        ifstream theFile("data.txt");        
	ofstream toFile;
	toFile.open("output.txt");                                

	if (!theFile) // if file didn't open
		return (cout << " ERROR : cannot open file.\n"), 1;

	while (getline(theFile, data))
	{
	     toFile << "   " << data << endl << endl;
         cout << "   " << data << endl << endl;
	}

       Highest(theFile);
    

    toFile.close();
    nameFile.close();
    return 0;
}

void Highest (ifstream & theFile)
{
    
        theFile.clear();
        theFile.seekg(0);

        int highest1 {} ;
        int highest2 {} ;

        for (int year {} , class1 {}, class2 {}, class3 {};
           nameFile >> year >> class1 >> class2 >> class3; )
    {

		if (calss1 > highest1)
			highest1 = class1;

		if (class2 > highest2)
			highest2 = class2;

		
    }

        cout << endl << "   The highest number for class 1 is " << highest1 << endl;
        cout << "   The highest number for class 2  is " << highest2 << endl;
     
        toFile << endl << "   The highest number for class 1 is " << highest1 << endl;
        toFile << "   The highest number for class 2  is " << highest2 << endl;
        
}


for cout in main it work but not in the function. i have more than 4 function and i need all of them to be stored in the output file. What should i do ?
So just add another parameter.
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
61
62
63
64
65
#include <iostream> 
#include <fstream>  
#include <string>
#include <cmath>    
#include <iomanip> 
#include <limits>

using namespace std;

void Highest (ifstream & theFile, ofstream &toFile);


int main()
{
        string data;
        ifstream theFile("data.txt");        
	ofstream toFile;
	toFile.open("output.txt");                                

	if (!theFile) // if file didn't open
		return (cout << " ERROR : cannot open file.\n"), 1;

	while (getline(theFile, data))
	{
	     toFile << "   " << data << endl << endl;
         cout << "   " << data << endl << endl;
	}

       Highest(theFile, toFile);
    

    toFile.close();
    nameFile.close();
    return 0;
}

void Highest (ifstream & theFile, ofstream &toFile)
{
    
        theFile.clear();
        theFile.seekg(0);

        int highest1 {} ;
        int highest2 {} ;

        for (int year {} , class1 {}, class2 {}, class3 {};
           nameFile >> year >> class1 >> class2 >> class3; )
    {

		if (calss1 > highest1)
			highest1 = class1;

		if (class2 > highest2)
			highest2 = class2;

		
    }

        cout << endl << "   The highest number for class 1 is " << highest1 << endl;
        cout << "   The highest number for class 2  is " << highest2 << endl;
     
        toFile << endl << "   The highest number for class 1 is " << highest1 << endl;
        toFile << "   The highest number for class 2  is " << highest2 << endl;
        
}

Your other typo's you'll have to fix yourself.

it work , thankyou but can you help me with cin

1
2
cin >> choice;
toFile >> choice;


it didn't work and have error
Provide a complete test program that shows the problem.
Hello kseokjk,

To go along with what seeplus said a complete program would include the input file that you are using. Posting the file allows everyone to use the same information and also to check if your reads are correct and also the writes.

In the following code I changed some variable names. I think you will find that the new names make it easier to follow in the code.

You have a really good start, but I believe a misunderstanding of how some things work.

Look over this and see if it makes more sense.
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
61
62
63
64
65
#include <iostream> 
#include <iomanip> 
#include <limits>
#include <string>
//#include <cmath>    

#include <fstream>  

using namespace std;  // <--- Best not to use.

void Highest(ifstream& inFile, ofstream& outfile);

int main()
{
    ifstream inFile("data.txt");
    ofstream outFile("output.txt", std::ios::app);
    //outFile.open("output.txt");

    if (!inFile) // if file didn't open
        return (cerr << "\n     ERROR : cannot open file \"data.txt\".\n"), 1;

    if (!outFile) // if file didn't open
        return (cerr << "\n     ERROR : cannot open file \"output.txt\".\n"), 2;

    string data;

    while (getline(inFile, data))
    {
        outFile << "   " << data << endl << endl;
        cout << "   " << data << endl << endl;
    }

    Highest(inFile, outFile);

    outFile.close();  // <--- Not required as the dtor will close the file when the function looses scope.
    inFile.close();

    return 0;
}

void Highest(ifstream& inFile, ofstream& outFile)
{
    inFile.clear();
    inFile.seekg(0);

    int highest1{};
    int highest2{};

    for (int year{}, class1{}, class2{}, class3{};
         inFile >> year >> class1 >> class2 >> class3; )
    {
        if (class1 > highest1)
            highest1 = class1;

        if (class2 > highest2)
            highest2 = class2;
    }

    cout << endl << "   The highest number for class 1 is " << highest1 << endl;
    cout << "   The highest number for class 2  is " << highest2 << endl;

    outFile << endl << "   The highest number for class 1 is " << highest1 << endl;
    outFile << "   The highest number for class 2  is " << highest2 << endl;

}

At the beginning I find the,mostly alphabetical order, helps especially when you forget a header file.

"<cmath>" is not necessary here if all you are doing is (+, -, *, / and %). "<cmath>" provides functions like "pow()", "cos()", "sin()" and others. Have a look at http://www.cplusplus.com/reference/cmath/

As mentioned line 11 is how the forward declaration or prototype should be written.

Line 15 is considered the more proper way to create a file stream and open the file at the same time.

Lines 16 and 17 work, but you should save the ".open()" for when you close a file steam and need to reopen it. Also when you setup the "ofstream" you might want to set it up to append the file and not clear its contents when it is open. Sometimes this can help when testing the program or you may want to start with an empty file.

The if statement, line 19, is good, but you also need to check the output file stream as there is a chance that it may not have opened. Notice that lines 20 and 23 end with a (1) and (2). The idea here is that the numbers can help track down where it went wrong. In this short program this is not a big deal, but in a larger program where you may open a file stream it can help.

The rest of the code and the function should be easy enough to understand.

Without the input file I can not test the function having no idea what is being read from the file.

Andy
Topic archived. No new replies allowed.