-3

this is my first time here. I'm learning C++ by myself from the book "Starting Out With C++" by Gaddis. Therefore I don't know who to ask, and then I found this website. Hope you don't mind to help me and I will learn a lot from you.

Here is one problem in the book:

Write a function named arrayToFile. The function should accept three arguments: the name of a le, a pointer to an int array, and the size of the array. The function should open the speci ed le in binary mode, write the contents of the array to the le, and then close the le. Write another function named fileToArray. This function should accept three arguments: the name of a le, a pointer to an int array, and the size of the array. The function should open the speci ed le in binary mode, read its contents into the array, and then close the le. Write a complete program that demonstrates these functions by using the arrayToFile function to write an array to a le, and then using the fileToArray function to read the data from the same le. After the data are read from the le into the array, display the array s contents on the screen.

And here is my code:

# include <iostream>
# include <string>
# include <fstream>


using namespace std;

const int SIZE = 10;
bool arrayToFile(fstream&, int*, int);
bool fileToArray(fstream&, int*, int);

int main()
{
    int arr[SIZE] = { 10, 8, 9, 7, 6, 4, 5, 3, 2, 1 },
        arrTest[SIZE];

    fstream file;

    if (arrayToFile(file, arr, SIZE))
    {
        if (fileToArray(file, arr, SIZE))
        {
            for (int n = 0; n < SIZE; n++)
            {
                cout << arrTest[n] << " ";
            }

            cout << endl;
           return 0;
        }
    }


    return 1;


}

bool fileToArray(fstream &file, int* a, int size)
{

    file.open("t.dat", ios::in | ios::binary);
    if (!file)
    {
        cout << "Can't open file t.dat\n";
        return false;
    }


    file.read((char*)a, size);
    file.close();

    return true;

}

bool arrayToFile(fstream &file, int* a, int size)
{
    file.open("t.dat", ios::out | ios::binary);
    if (!file)
    {
        cout << "Can't open file t.dat\n";
        return false;
    }

    file.write((char*)a, size);
    file.close();

    return true;
}

AND HERE IS THE OUTPUT: -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -85 8993460 -858993460 -858993460

I don't know what's wrong here, because the output is supposed to be like this: 10 8 9 7 6 4 5 3 2 1

Thanks for your help.

Minh Le
  • 47
  • 4
  • -858993460 = 0xCCCCCCCCCC which is a special microsoft debug code. It means uninitialized stack memory. http://stackoverflow.com/a/127404/487892. Looking at the code you did not initialize arrTest. – drescherjm Dec 15 '14 at 03:55
  • Start by reading the documentation for `read` and `write` paying close attention to what each argument passed to them does. Then look at how you're actually using them. – Captain Obvlious Dec 15 '14 at 03:57
  • Did you compare with what is in arr? – Benjamin Bannier Dec 15 '14 at 03:58
  • You print the contents of `arrTest` - but you've never initialized `arrTest`. In particular, you are passing `arr` to `fileToArray`. Further, `ostream::read` and `write` take the size in bytes. It is unlikely that `sizeof(int)==1` on your system. – Igor Tandetnik Dec 15 '14 at 04:00

2 Answers2

0

Before I begin, you must understand that a char is of size sizeof(char) bytes and an int is of size sizeof(int) bytes, which in general is 1 byte and 4 bytes respectively. This means that 4 chars can make up 1 int.

Now if you look at file.write((char*)a, size);, you are converting the int* to char*, which means to look at the data in a as chars and not ints.

Performing some simple math, your array of 10 ints is of size 10 * 4 = 40 bytes. However, the amount of data you're writing to the file is of 10 chars which is only 10 bytes.

Next, you attempt to write arr to the file, and you read from the file back into arr. I assume you want to read into arrTest but as you've failed to do so, attempting to access the contents of arrTest will give you the output that you see, which as explained by the rest of the community is uninitialized memory. This is because arrTest[SIZE] defines an array of SIZE but the memory within it is uninitialized.

To sum it up, the cause of your current output is due to a problem in your program logic, and your incorrect usage of read and write poses a potential problem.

Thank you for reading.

Nard
  • 1,006
  • 7
  • 8
0
file.write((char*)a, size);

writes size bytes to the file.

file.read((char*)a, size);

reads size bytes to the file.

You are using 10 for size. You are writing and reading just 10 bytes of data. You need to change those lines to:

file.write((char*)a, size*sizeof(int));

and

file.read((char*)a, size*sizeof(int));

And call fileToArray using the right argument. Instead of

    if (fileToArray(file, arr, SIZE))

use

    if (fileToArray(file, arrTest, SIZE))

without that, arrTest remains an uninitialized array.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thank you for your help. It's very detailed. I don't know why I made the last mistake, though. It's really help. – Minh Le Dec 15 '14 at 04:20
  • @MinhLe, most of us have dealt with such simple errors. I'm glad I was able to help. – R Sahu Dec 15 '14 at 04:22