This code makes no sense: just look what is written in the output file. You have written some floating-point numbers in human-readable string representation, without any delimiters between the values; due to casualty of the format, some "records" take 9 bytes, some other take 8. You should not have written it in first place — what's the use?
Then you open the file for reading and try to read it as an array of double-precision floating-point numbers, 8 bytes each. You write in one format, which makes no practical sense, because it does not allow for unambiguous interpretation (due to lack of fixed boundaries between records), and read in totally unrelated format. This is nothing but a mess.
There is no a fix, because apparently there is no functional purpose in this code, so I assume the purpose is learning how data is presented, read and written. It can be done in different ways, and you will need to learn them all. First, you need to write data in many different ways and see how it looks. As you are a beginner, I would advise you use both text and binary editors and examine how the data is laid out. When you get a grip on what's going on, think about reading it.
Some side advice: don't use obsolete
getch
, use
_getch
. Never use hard-coded absolute file path, even in the research/study code, use relative; in your case, it could be just the file name without directory, so the actual location will be defined by the current working directory. Remember, in the final product, there are no situations when a hard-coded path can be used. Never repeat any data; you have written "C:\\Tests\\test.dat" twice, which is totally unsupportable. Define it as an explicit constant, only once. Please see:
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[
^]. Failing to follow this very basic principle is the invitation for all kinds of trouble.
By the way, how about writing at least a bit in C++, not C, once your file is *.cpp? Avoid
#define
, use
const int length = 10;
instead. But if you want to lean C, as opposed to C++, create a *.C file.
—SA