iostream, fstream which do I need for handling files?

C++ is really confusing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>
//#include <fstream>
#include <cstring>
using namespace std;

int crypt(char* file, float seed)
{    
    //Open the files for encryption
    ofstream input(file, ios::in|ios::binary|ios::ate);
    long size = input.tellg();
    input.seekg(0, ios::beg);
    
    char* outputname; tmpnam(outputname);
    ifstream output(outputname, ios::out|ios::binary);
    
    //Now do the encryption itself
    char* byte;
    for(int i = 0; i <= size; i++)
    {
        output.read(byte, 1);
        input.write(byte, 1);
    }
}


In case I include <fstream>:
C:\Documents and Settings\Laptop\Mijn documenten\letters wisselen.cpp:29: error: 'struct std::ofstream' has no member named 'tellg'
C:\Documents and Settings\Laptop\Mijn documenten\letters wisselen.cpp:30: error: 'struct std::ofstream' has no member named 'seekg'

In case I exclude it:
C:\Documents and Settings\Laptop\Mijn documenten\letters wisselen.cpp:28: error: variable `std::ofstream input' has initializer but incomplete type
C:\Documents and Settings\Laptop\Mijn documenten\letters wisselen.cpp:33: error: variable `std::ifstream output' has initializer but incomplete type

So what am I doing wrong?

Also I would like some advice on the way I open files and coding habits.
You need to include fstream to use it functions.

I think you need to replace tellg and seekg with tellp and seekp? Otherwise, read this:
http://www.cplusplus.com/reference/iostream/fstream/
Thanks you are right:
tellp and seekp are for input
tellg and seekg are for ouput

fstream needs to be included although it gets partly included when including iostream
Topic archived. No new replies allowed.