Apr 29, 2009 at 11:48pm UTC
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.
Apr 30, 2009 at 12:06am UTC
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
Apr 30, 2009 at 12:17am UTC
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.
Apr 30, 2009 at 12:25am UTC
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 Apr 30, 2009 at 12:33am UTC
Apr 30, 2009 at 12:52am UTC
buffer is initialized on line 19.
Apr 30, 2009 at 1:23am UTC
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 Apr 30, 2009 at 1:24am UTC
Apr 30, 2009 at 1:30am UTC
Line 49: Buffer overflow. Limit exceeded by 1 element.
Last edited on Apr 30, 2009 at 12:04pm UTC
Apr 30, 2009 at 7:18am UTC
thats the 10th place in the array .... it goes form 0-9 --- 10
+ it works
Last edited on Apr 30, 2009 at 7:20am UTC
Apr 30, 2009 at 12:03pm UTC
An array declared as array[9] has 9 elements: [0..8].