Conversation started Sep 5, 2012 at 23:55.
Sep 5, 2012 23:55
@MooingDuck Hi :)
@MohamedAhmedNabil hi
I wrote a program the stores data in a record format, user can access elemnts and write to it by entering the record number (Direct/Random Access)
fixed size structs? no strings or vectors or anything?
Then i get data from the user and store it in a binary file, as a record (36 bytes per record)
from the things i did in the program that people told me was bad , is that i use stoi
I get number input from the user into a string and then change it to and inteager , because it is better than cin>>
they told me stoi, seemed to cause errors, if for example the user enterd "Abdsf"
So i worked around that problem, added a few more features to the code and would like you too see it
@MooingDuck ideone.com/ykRVK
@MohamedAhmedNabil wow, yes. That's a lot of code to read in a number, and you don't even handle the error case.
Sep 6, 2012 00:02
@MooingDuck Thats the entire program, with the record storage and everything
@MohamedAhmedNabil did you test it?
@MooingDuck The part the makes sure that stoi doesnt make errors is in the functions
@MohamedAhmedNabil true enough
@MooingDuck Yep, it worked like a charm, It doesnt even have try and catch
c[i]<48 what is 48? Why did you type 48 there? Use '0' or whatever it is.
Sep 6, 2012 00:04
@MooingDuck Yea, Im a dum dum. I dont know why i didnt do that, :/
and why didn't you use cin >> i?
Probably the hardest part was figuring out how to get the - sign in there
you also have an infinite loop at the end of standard input, but it's an advanced thing to fix and that's fine for where you are.
If i enter no input, it will keep waiting like a stuborn child,

If the user adds a white space by accident, it can cause problems, and using getline and cin>> causes errors.
@MohamedAhmedNabil oooh, and nobody told you about stingstream yet. Got it
Sep 6, 2012 00:07
@MooingDuck I also took an exercise from my book into the program, "Keep asking for the record number till the user enters -1"
@MooingDuck so good program?
int getint(string x){
        std::stringstream ss(x);
        int result;
        while (!ss>>result) {
            if (ss.eof()) throw std::runtime_error("reached end of input");
            std::cin.clear();
            std::cout << "Please Enter a number: ";
            std::getline(std::cin, x);
            ss.str(x);
        }
        return result;
}
@MooingDuck I didnt learn stringstream
@MohamedAhmedNabil then the best thing to do is not use getline.
@MooingDuck Why not :O, i thought it was cin>> that was bad
@MohamedAhmedNabil no, nothing wrong with cin >>. It's very good for text.
Sep 6, 2012 00:12
@MooingDuck The whitespace problem
@MohamedAhmedNabil the only whitespace problem I can think of is if you want to read in a string that contains whitespace.
@MooingDuck exactly, as you see, this program takes a name and an age
There is a big chance that the name will contain whitespace
@MohamedAhmedNabil ah. then you should just use cin.ignore(2, '\n') after using getline
@MooingDuck ok, one more thing, My Book tells me that using cin>> and getline together in one program can cause errors, Why?
@MohamedAhmedNabil getline has the side effect that when it reaches the delimiter '\n', it leaves that in the buffer. cin >> i only reads until the next whitespace, which is immedately, so the cin >> i after a getline will fail, unless you use cin.ignore(2, '\n') between.
Sep 6, 2012 00:20
@MooingDuck Then why just not stick to getline only?
@MohamedAhmedNabil getline only works on strings, and defeats the entire purpose of using formatted streams.
@MooingDuck Do i have to use the cin.ignore each time i use cin>> or just the first time i use it afte getline?
@MohamedAhmedNabil just do it right after the getline every time.
@MooingDuck so once after the getline not once before each cin>>
@MohamedAhmedNabil right
Sep 6, 2012 00:25
@MooingDuck There are lots of downsides the cin>>
Give me a sec
@MohamedAhmedNabil there's a lot of upsides as well
@MooingDuck like?
what the, you have goto in your code?
@MooingDuck yea
@MohamedAhmedNabil use functions instead. or loops
Sep 6, 2012 00:27
@MooingDuck I wanted to do a go back feature, it seemed the best way to do so
@MohamedAhmedNabil loops.
@MooingDuck is goto bad?
@MohamedAhmedNabil almost everyone considers goto terrible. There's a tiny number of people who says it's sometimes useful.
waaaiiit
obj.write(reinterpret_cast<char*>(&name), sizeof(name));
should be: obj.write(reinterpret_cast<char*>(&name[0]), name.size());
Sep 6, 2012 00:33
@MooingDuck is there a difference?
@MohamedAhmedNabil huge. massive.
@MohamedAhmedNabil the string represents a series of characters. Internally it keeps them floating in memory in a completely random location, and just keeps track of where they are.
@MohamedAhmedNabil sort of like a person keeping track of where your house is.
@MooingDuck can you explain? If it were a char array i would do this `obj.write(cstr,sizeof(cstr));
@MohamedAhmedNabil that only works for simple types. string is not a simple type.
If I make a clone of a person, I did not make a clone of their house as well. Now there's two people using the same house and that's bad.
you can't just save a string like that
you have to save the characters that the string represents.
this is a homework assignment? And you have to mix strings and binary files? Because this is tricky stuff.
so in a string, the string name doesnt represent the address of the first element, like c-string?
@MohamedAhmedNabil a std::string name represent the series of characters, just like a c-string. However, a c-string can also be treated as if it were a pointer to the first element. a std::string cannot.
Sep 6, 2012 00:37
@MooingDuck This isnt homework, Im learning the language by myself.
@MohamedAhmedNabil oh, that makes sense then.
@MohamedAhmedNabil you know about pointers and dynamic memory?
@MooingDuck actually the book is alot simplier in its excercises, I like to expand and develop, I learned alot that way. The book usees c-strings but i said her STD::STRING, Also the back feature was my idea
@MooingDuck I know about pointers yea, learned about them and used them alot, but not dynamic memory
actually, here's a better explanation: a std::string is actually three variables, and a bunch of cool functions. The first variable is the number of characters. The second variable is unimportant for now. The third variable is a pointer to a null terminated c-string.
so when you did obj.write(reinterpret_cast<char*>(&name), sizeof(name));, you saved the size, the other thing, and a pointer, but no chars.
Sep 6, 2012 00:41
oooh
what about obj.write(reinterpret_cast<char*>(&name[0]), sizeof(name));
@MohamedAhmedNabil name[0] is a reference to the first character, right? So & on that gives you a pointer to the first character.
@MohamedAhmedNabil however, sizeof(name) is still the size of the three variables. You should have name.size(), which is the number of characters.
@MooingDuck But you said that the letters arent arranged after each other in the memroy
@MohamedAhmedNabil what? you missed a word.
@MohamedAhmedNabil I spoke unclearly. The letters are in a random place in memory, but they are all in a row/array/c-string together. somewhere not inside the string.
@MooingDuck The third variable is a pointer to a null terminated c-string.
@MohamedAhmedNabil right
Sep 6, 2012 00:45
What does this c-string represent?
@MohamedAhmedNabil the c-string holds the characters you want.
@MooingDuck Im using random acccess by using fixed size records while storing them in the binary file, knowing where the record is , is a matter of multyplying the recordsize by the record number
@MooingDuck So i thought i could use std::string as a way to keep the input to a very large extent and yet keep the record fashion of the program
Turns out i cant huh :?
@MohamedAhmedNabil yeah, because you have to do special stuff when the string is larger or smaller than you want. But luckily that's easy
  std::string name = whatever;
  name.resize(STRUCT_NAME_SIZE, '\0'); //resize to the correct size
  file.write(&name[0], name.size()); //and then save it
The reason i used ::string was to keep the size at 32 bytes, no matter what
@MohamedAhmedNabil std::string doesn't do that so well.
Sep 6, 2012 00:49
@MooingDuck But this crops out some data
@MohamedAhmedNabil there's really no option. What else would you do? You can't fit a 30 inch object in a 20 inch hole.
Basically, if i read and write in the same program with std::string like this `obj.write(reinterpret_cast<char*>(&name), sizeof(name)) its no problem, since the pointers keep pointing to the same thing, So writing an std::string that way and reading it again, shouldnt be a problem
@MohamedAhmedNabil the way you did before? no, it would definitely be a problem.
namely, if you got amazingly lucky, all of the data would be the last string you saved. Otherwise it would just crash.
@MohamedAhmedNabil as long as you don't change name in any way whatsoever, then yes.
it's time for me to go home now, sorry :(
Ok ill just leave this here, you said, three variables, and a bunch of cool functions. The first variable is the number of characters. The second variable is unimportant for now. The third variable is a pointer to a null terminated c-string.
now these 3 variables will be written
@MohamedAhmedNabil right
Sep 6, 2012 00:54
so when read, These 3 variables, will be read
So bassically if i dont open and close the program between the read and write, The pointer should be at the same address
@MohamedAhmedNabil but if you change name, the string might move the cstring to a completely new place in memory, so when you read in the old three variables, the cstring pointer is invalid and will crash the program.
might???
depending on what?
if you change the word and it doesn't move it, then it will just erase the old string and put the new one where the old used to be. No crash, but the words are wrong.
@MohamedAhmedNabil if you change it to be a longer word/name.
end of story, leave std::strings in binary mode
good luck :(
Sep 6, 2012 00:57
ok :)
Thanks :) You taught me alot, It was really informative and helpful
binary files and complex objects don't mix easy unless you understand how the complex objects work.
I take all my conversations with you and save them for refrence
One more thing,assuming cstr is a c-string
obj.write(cstr,sizeof(cstr));
The null here is written correct?
@MooingDuck Thanks for your time :)
 
Conversation ended Sep 6, 2012 at 1:02.