Bin file io

so im working on this little file io project of mine and i made this:
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
#include <iostream>
#include <string>
#include <fstream>
#include "Queue.h"
#include "Queue.cpp"
using namespace std;
class fileSystem
{
      private:
             
             Queue holder;
      public:
             fileSystem();
             int add(int ssn, string name);
             Queue getter();
             
      };
      
fileSystem::fileSystem()
{
    return; 
 }
 
int fileSystem::add(int ssn, string name) 
{
    ofstream file("data.bin", ios::in|ios::binary|ios::app);
    char temp[9];
    const char* a;
    a= name.c_str();
    char*e;
    e = itoa(ssn,temp,10); 
    file.write(e, 9);
    file.write(a, 10);
    file.close();
}

Queue fileSystem::getter()
{
      ifstream file("data.bin", ios::out|ios::binary);
      char* temp;
      char * temp1;
      int num = 0;
      Queue::data d;
      while(!file.eof())
      {
                        num++;
      file.read(temp1,9);
      cout << temp1 << endl;
      file.read(temp,10);      //<-- it loops here
      cout << temp <<endl;
      system("pause");
      d.name = temp;
      d.ssn = atoi(temp1);
      d.mem = num;
      holder.push_back(d);
      }
      file.close();
      remove("data.bin");
      return holder;
      }


well it goes in to an infinite loop printing out wrong data
i was wondering if i could barrow some eyes to make sure im doing this right
Line 39: What? ios::out? Do you want to read or write?
Lines 47 and 49: I'm pretty sure read() doesn't advance the file pointer.
ios::out Open for output operations.
just wanna read here
tried taking it out but same error ether way

but if it didn't advance the file than it should just print out my input at least from my add function
I know what std::ios::out is for. Reading is input, not output.
This is the rule:
If you're moving data to memory, then it's input.
If you're moving data out of memory, then it's output.
Data movements between the CPU and memory are neither input nor output.

Using std::ios::out with std::ifstream is somewhat contradictory, but technically correct. It's the same as using std::fstream. You're opening the file both for input and output.

Another thing I just noticed: temp and temp1 are uninitialized, to they are invalid pointers. This should have crashed almost immediately.

Lines 29 and 31: God, this file is a collection of non-crashing segmentation faults.
tested with the temp and temp1 are initialized
and deleted the ios::in/out since there there for no real purpose and now it compiles and just does not work

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 <string>
#include <fstream>
#include "Queue.h"
#include "Queue.cpp"
using namespace std;
class fileSystem
{
      private:
             
             Queue holder;
      public:
             fileSystem();
             int add(int ssn, string name);
             Queue getter();
             
      };
      
fileSystem::fileSystem()
{
    return; 
 }
 
int fileSystem::add(int ssn, string name) 
{
    ofstream file("data.bin", ios::binary|ios::app);
    char temp[9];
     char* a;
    strcpy(a, name.c_str());
    char*e = "";
    e = itoa(ssn,temp,10); 
    file.write(e, 9);
    file.write(a, 10);
    file.close();
}

Queue fileSystem::getter()
{
      ifstream file("data.bin", ios::binary);
      char* temp= "";
      char * temp1= "";
      int num = 0;
      Queue::data d;
      while(!file.eof())
      {
                        num++;
      file.read(temp1,9);
      cout << temp1 << endl;
      file.read(temp,10);
      cout << temp <<endl;
      system("pause");
      d.name = temp;
      d.ssn = atoi(temp1);
      d.mem = num;
      holder.push_back(d);
      }
      file.close();
      remove("data.bin");
      return holder;
      }


Last edited on
Still not right. You can't write to the locations the pointers point to.
Read this: http://www.cplusplus.com/doc/tutorial/dynamic.html
http://www.cplusplus.com/reference/iostream/ostream/write/
they have an example:
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
// Copy a file
#include <fstream>
using namespace std;

int main () {

  char * buffer;   //unish <------------
  long size;

  ifstream infile ("test.txt",ifstream::binary);
  ofstream outfile ("new.txt",ofstream::binary);

  // get size of file
  infile.seekg(0,ifstream::end);
  size=infile.tellg();
  infile.seekg(0);

  // allocate memory for file content
  buffer = new char [size];

  // read content of infile
  infile.read (buffer,size);

  // write to outfile
  outfile.write (buffer,size);  //than writes it to the file <-------------------
  
  // release dynamically-allocated memory
  delete[] buffer;

  outfile.close();
  infile.close();
  return 0;
}


??????????? i think im confused now
buffer is initialized on line 19.
ok kool
the problem was that when i made my char string i didnt put a null terminator at the end of my int after i changed it to a char string

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
#include <iostream>
#include <string>
#include <fstream>
#include "Queue.h"
#include "Queue.cpp"
using namespace std;
class fileSystem
{
      private:
             
             Queue holder;
      public:
             fileSystem();
             int add(int ssn, string name);
             Queue getter();
             
      };
      
fileSystem::fileSystem()
{
    return; 
 }
 
int fileSystem::add(int ssn, string name) 
{
    ofstream file("data.bin", ios::binary|ios::app);
    char temp[9];
     char* a;
     a = new char[10];
    strcpy(a, name.c_str());
    char* e = new char [9];
    e = itoa(ssn,temp,10); 
    file.write(e, 9);
    file.write(a, 10);
    file.close();
}

Queue fileSystem::getter()
{
      ifstream file("data.bin", ios::binary);
      char* temp= new char[10];
      char * temp1= new char[9];
      int num = 0;
      Queue::data d;
      while(!file.eof())
      {
                        num++;
      file.read(temp1,9);
      temp1[9] = '\0';  //this is needed
      file.read(temp,10);
      d.name = temp;
      d.ssn = atoi(temp1);
      d.mem = num;
      holder.push_back(d);
      }
      file.close();
      remove("data.bin");
      return holder;
      }
Last edited on
Line 49: Buffer overflow. Limit exceeded by 1 element.
Last edited on
thats the 10th place in the array .... it goes form 0-9 --- 10
+ it works
Last edited on
An array declared as array[9] has 9 elements: [0..8].
Topic archived. No new replies allowed.