Writing array of objects in a file

This is a discussion on Writing array of objects in a file within the C++ Programming forums, part of the General Programming Boards category; I am having trouble writing and reading array of objects.
this program is representing the trouble that I am having ...

Writing array of objects in a file usyng write()

I am having trouble writing and reading array of objects.
this program is representing the trouble that I am having in real one.
I want to write class objects in a file "Stu.txt" and then read it so that i can display its contents the program is giving garbage value after 3 objects.
i don't know why? I have search through net but can't find any solution.
I don't have any idea about the problem.

I am using borland 5.0.2 in windows xp sp2
And one more thing why the contents of Stu.txt are displayed in form of symbols
that too only values of those variables which are not char?
I have provided the output just in case.
Please help me...
Thanks in advance.

You're mixing C/C++ I/O, you should avoid doing this. gets in particular should absolutely be avoided, prefer fgets if you really needed to use C input for this purpose here. There is a very nice cin.getline() function that is perfect for what you want which could easily take the place of the gets call.

As for the puts, there is no need again to be using C output when simply stating cout<<"\nName " << name; would do just fine.

The first two of these are old versions of the C++ headers. If you can, you should have:

Code:

#include <fstream>
#include <iostream>
#include <cstdio>

There is no reason to have the windows.h header since there is nothing there that your code needs from it.

#4.

Code:

system("pause");
/*for pause progam as the program
automatically exits which enables
user to see results*/
}

Calls to system in general should be avoided. A call to cin.get (or two) can accomplish the same thing more safely. In addition to the headers you've already got, I believe the system function needs the stdlib.h/cstdlib header - pick one (hopefully the later). You should also consider an explicit return 0; or something to that effect at the end of your program.

The open mode for the first call to open should be ios_base::out since you have no need to do any reading at this point. The second open call should be ios_base::in since all you're going to be doing at this point is reading from the file. The seekg is completely unnecessary. Opening the file will set the "get" pointer to the start of the file, there is no need to explicitly state "go to the beginning before you start reading".

Alternately you could just open the file once in read/write mode and use the seekg in between your write/read attempts without closing the file and having to reopen it.

#6. There is a problem in your read loop. When you've finished reading the first "roll no" there is a newline character left in the stream that needs to be dealt with before you go back to reading the next students name. Otherwise you'd skip the gets call and not be able to enter data for the name. A cin.get call would likely do the job.

#7.

And one more thing why the contents of Stu.txt are displayed in form of symbols
that too only values of those variables which are not char?

The actual contents of the stu.txt file appear as they do because of how you are writting data to the file using the write function. You pass a stream of bytes of a particular length to the write function which ends up writting those bytes exactly as they appear in memory. The funny characters are because of the binary representation of the float and int variables your class has. If you use << to write the values to the file, then you'd see the text converted version of those values but then you'd have problems reading and displaying them back to the user because the length of the data would not necessarily be fixed liked they are when using write.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens

But one more problem has has occurred now (i think this one is due to that #7 point you
stated) -
now when the the program asks for second student it skips the name part.

enter name ay
enter grade A
enter marks 41
enter roll no 1

enter name enter grade

One more thing i need to know can write whole array through one write f(x)
after calling getdata() for all objects(same goes for read())?

#1

I was a frustrated, after hours of programming i could not get the base of my program right but i won't do it again.
I am school boy working for my school project so i don't have much experience and
Patience which got me frustrated.

#2

Can teach me a little more about fgets?
Its header file and parameters and anything that i need to know.

#3

I know about them. its just that i copied the code from my textbook in hurry.
but i think windows is needed for system("pause") although i won't use it again.

#5

I know about these but i used copy-paste here from another code.

#6

I don't have any idea about what you are saying. Can you explain and give solution
for it since i think this is were the problem is?

#7

thanks about this one I didn't had a clue about them.

Thank again you saved me the trouble of asking this to my teacher as i can't beer her
and when i showed her the real program she didn't even had a clue of what was going on.

Processing input works differently depending on what you are trying to read. When you reach your cin>>rn code, you enter a numeric value and press the return key. The value is converted and stored into the rn variable but the newline is kept in the input buffer and can be read/processed in subsequent input operations. This is where the rules can be confusing... if you were to read another numeric value at this point, the existing newline in the input buffer would be skipped and the input operation would block (wait for input) until you entered another number and then pressed the enter key once more. But, in your loop you go back to the gets call which read character input instead of numeric input. The processing of character data here is handled differently than if you were trying to read a number for instance. Here, the existing newline that is still in the buffer from the previous cin>>rn statement is interpreted as you - the user - having pressed the enter key without really entering anything. Nothing gets stored in the name field and the effect is that your code appears to skip that statement altogether. This fault is therefore due to the newline that is left over from a previous input. What needs to happen - probably after the cin>>rn statement is you must throw away the trailing newline in the input buffer so that when you next attempt to read the name it does not skip over that line. This can be done in a couple ways one of which is:

One more thing i need to know can write whole array through one write f(x)
after calling getdata() for all objects(same goes for read())?

Well, you currently call write once in each iteration of the loop. If you want a single write call, you could move it to after the loop and instead of writing one chunk of data simply write 5 chunks worth (one for each student in the array) all at once... you control how many bytes are written in that call, it is after all the second parameter to that particular function. Something similar can be done when reading from the file.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens

but the problem still exists... why? i can't figure it out.
if it is due to cstdio or cstdlib which i didn't include because when included it the program gave error -

noname00.cpp(3,2) : Unable to open include file 'CSTDIO.h'
noname00.cpp(4,2) : Unable to open include file 'CSTDLIB.h'

which means my complier doesn't have these header file. if this is were the problem is then please tell me from where to download it.
If the header file is not the problem then whay to due?
please help me the the submission deadline is next week.

which means my complier doesn't have these header file. if this is were the problem is then please tell me from where to download it.

There are no standard headers named <stdio> and <stdlib>. However, there are standard headers named <cstdio> and <cstdlib>, or in the C version, <stdio.h> and <stdlib.h>. That said, you don't need them here.

You also need to format your code properly and use appropriate using declarations and using directives, or fully qualify names from the std namespace. For example:

Change your compiler. It looks fine on inspection, and compiles without even a warning on the Comeau online compiler, the MinGW port of g++ 3.4.5 and MSVC10. Therefore, in all likelihood it is your compiler that is at fault.

Refer to this incomplete list of C++ compilers. It is written by Professor Bjarne Stroustrup, who is a Distinguished Professor and holder of the College of Engineering Chair in Computer Science at Texas A&M University, as well as the designer and original implementer of C++. Try compiling the test program that is shown. I suspect that your compiler will fail to compile it, thus you can use it as proof when you approach your instructors.

Originally Posted by Dynamis

isn't there anything else one can do to compile the code without garbage value that i latest posted.

What you posted is not some "garbage value". It is a list of compile errors; notice the stuff in parentheses, e.g., (15, 3). The first number is the line number where the error was detected (not necessarily where the error actually is), though I am not sure about the second number (which does not seem to be a column number, as I thought it might be).

You could try removing the using declarations and using directive. It would make the code incorrect, but possibly allow it to compile on your compiler.

thanks laserlight the program works fine on dev C++ 4.9.9.2.
Can you explain me what was wrong in the code which i posted in post #5 and what you did to make it work on your code as i don't know what is going in it(using namespace, using std::cout, using std::cin)?
is their any way to make using namespace work on borland 5.0.2 as dev C++ is declaring error in clrscr(), textcolor(), random(), randomize(), gotoxy() and cputs() or is there any alternative to these f(x) ?

cin.getline() is also bad since it works with C-style strings.
I recommend you change the declaration of name to
std::string name;
And use
std::getline(name, std::cin);
instead.
This is the obvious C++ solution.