ifstram::read won't work the second time

I'll just copy - paste my code so that is more clear:
here's data.txt:
Hello World!
I'm a file.

the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h> // for system pause ...

using namespace std;

int main(){
    int datalen;
    ifstream ifdata("data.txt");
    ifdata.seekg(0, ios::end);
    datalen = ifdata.tellg();
    ifdata.seekg(0, ios::beg);
    char data[datalen], databuff[datalen];
    // code
    ifdata.read(data, datalen); // first time
    cout << data << endl << endl; // works
    ifdata.read(databuff, datalen); // second time
    cout << databuff << endl; // it does not
    system("pause"); //yes, yes i know all about system pause please don't argue about that...
    return (0);
}


so here's the output:
Hello World!
I'm a file.

sfB
Press any key to continue . . .


and here's the expected result
Hello World!
I'm a file.

Hello World!
I'm a file.
Press any key to continue . . .

can somebody tell me why it doesn't work and possibly how to make it work ?
PS: i know, it'll be easier to use strcpy(databuff, data) but my question is not: solve my problem ! , but, tell my why it doesn't work ?
Well, your code is explicitly designed to read all of the data into the array. Therefore the read pointer will be positioned at the end of the file after the first read.

If you wish to read the data a second time, then the ifdata.seekg(0, ios::beg); will reset the read pointer. But you knew that since you wrote the code?
Last edited on
of course, sorry i forgot to place ifdata.seekg(0, ios::beg); in the code, anyways the code gives the same result:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h> // for system pause ...

using namespace std;

int main(){
    int datalen;
    ifstream ifdata("data.txt");
    ifdata.seekg(0, ios::end);
    datalen = ifdata.tellg();
    ifdata.seekg(0, ios::beg);
    char data[datalen], databuff[datalen];
    // code
    ifdata.read(data, datalen); // first time
    cout << data << endl << endl; // works
    ifdata.seekg(0, ios::beg); // here the missing line, that does not change much
    ifdata.read(databuff, datalen); // second time
    cout << databuff << endl; // it does not
    system("pause"); //yes, yes i know all about system pause please don't argue about that...
    return (0);
}

the new result:
Hello World!
I'm a file.

òfB
Press any key to continue . . .
Last edited on
I overlooked that there might be an "end of file" after the first time. Therefore use clear() to reset the stream status.
Your code wouldn't compile for me. I used new [] and delete [] instead.
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
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h> // for system pause ...

using namespace std;

int main(){
    int datalen;
    ifstream ifdata("input.txt");
    ifdata.seekg(0, ios::end);
    datalen = ifdata.tellg();
    ifdata.seekg(0, ios::beg);
    char * data     = new char[datalen]; // allocate buffer
    char * databuff = new char[datalen]; // and another
    // code
    ifdata.read(data, datalen);          // first time
    cout << data << endl << endl;
    cout << "file is good() : " << ifdata.good() << endl;
    ifdata.clear();                      // Reset flags after end of file condition.
    cout << "file is good() : " << ifdata.good() << endl;
    ifdata.seekg(0, ios::beg);           // reset position
    ifdata.read(databuff, datalen);
    cout << databuff << endl;

    delete [] data;                      // release buffer memory
    delete [] databuff;                  // and the other one

    system("pause");

    return (0);
}

Output:
Hello World!
I'm a file.

file is good() : 0
file is good() : 1
Hello World!
I'm a file.
Press any key to continue . . .
Last edited on
Topic archived. No new replies allowed.