Pointer conversion problem

Discussion in 'C++' started by Kat, Apr 26, 2006.

  1. Kat

    Kat Guest

    Ok so I finally fixed most of the problems in this program but I've
    come up with even more. My program(if you saw earlier) is a program
    for secure file deletion as part of a project. I'm using Visual Studio
    6.0(my school refuses to upgrade, I don't know why nor can I persuade
    them otherwise) Visual C++.

    Here's a snippet of the code I'm having problems with.

    //Begin Code\\

    long size;

    infile.seekg(0,ifstream::end);
    size = infile.tellg();
    infile.seekg(0);
    infile.close();

    ofstream outfile (fileloc,ofstream::binary);

    outfile.write (zero,size);

    //End Code\\

    Bellow is the error given by the compiler.

    //Begin Error\\

    Z:\SecureIO.cpp(172) : error C2664: 'write' : cannot convert parameter
    1 from 'const int *' to 'const char *'
    Types pointed to are unrelated; conversion requires
    reinterpret_cast, C-style cast or function-style cast

    //End Error\\

    How do I fix this problem?
    Kat, Apr 26, 2006
    #1
    1. Advertising

  2. Kat wrote:
    > Ok so I finally fixed most of the problems in this program but I've
    > come up with even more. My program(if you saw earlier) is a program
    > for secure file deletion as part of a project. I'm using Visual Studio
    > 6.0(my school refuses to upgrade, I don't know why nor can I persuade
    > them otherwise) Visual C++.
    >
    > Here's a snippet of the code I'm having problems with.
    >
    > //Begin Code\\
    >
    > long size;
    >
    > infile.seekg(0,ifstream::end);
    > size = infile.tellg();
    > infile.seekg(0);
    > infile.close();
    >
    > ofstream outfile (fileloc,ofstream::binary);
    >
    > outfile.write (zero,size);
    >
    > //End Code\\
    >
    > Bellow is the error given by the compiler.
    >
    > //Begin Error\\
    >
    > Z:\SecureIO.cpp(172) : error C2664: 'write' : cannot convert parameter
    > 1 from 'const int *' to 'const char *'
    > Types pointed to are unrelated; conversion requires
    > reinterpret_cast, C-style cast or function-style cast
    >
    > //End Error\\
    >
    > How do I fix this problem?
    >


    Hi,


    How is zero defined? What requirements do you have on "secure file
    deletion" - to fill it with \0's or many '0' characters, or perhaps
    random bits?

    Look at what the compiler tells you, there is the answer to your question.


    Regards,

    Peter Jansson
    http://www.p-jansson.com/
    http://www.jansson.net/
    Peter Jansson, Apr 26, 2006
    #2
    1. Advertising

  3. Kat

    Jim Langston Guest

    "Kat" <> wrote in message
    news:...
    > Ok so I finally fixed most of the problems in this program but I've
    > come up with even more. My program(if you saw earlier) is a program
    > for secure file deletion as part of a project. I'm using Visual Studio
    > 6.0(my school refuses to upgrade, I don't know why nor can I persuade
    > them otherwise) Visual C++.
    >
    > Here's a snippet of the code I'm having problems with.
    >
    > //Begin Code\\
    >
    > long size;
    >
    > infile.seekg(0,ifstream::end);
    > size = infile.tellg();
    > infile.seekg(0);
    > infile.close();
    >
    > ofstream outfile (fileloc,ofstream::binary);
    >
    > outfile.write (zero,size);


    How is zero defined? By your error message it would seem it's defined as in
    integer pointer. But you want to write a character. Is it an array?
    Simplest way would be to make zero a character (array?) of '\0' instead of
    an int with value 0. Of course the size of it has to be as big as size
    though.

    I believe what you actually want to do here is write the character '\0' size
    times. You can either create an array and initialize it to '\0'
    (std::vector<char> perhaps) or put your write in a for loop writing one char
    size times.

    char zero = '\0';
    for ( int i = 0; i < size; ++i )
    outfile.write(&zero, size);

    is one way of doing it.

    An alternative is to simply cast your zero to a char pointer, as long as you
    know that it's the proper size. I wouldn't do this, however, I'd just make
    it chars.

    > //End Code\\
    >
    > Bellow is the error given by the compiler.
    >
    > //Begin Error\\
    >
    > Z:\SecureIO.cpp(172) : error C2664: 'write' : cannot convert parameter
    > 1 from 'const int *' to 'const char *'
    > Types pointed to are unrelated; conversion requires
    > reinterpret_cast, C-style cast or function-style cast
    >
    > //End Error\\
    >
    > How do I fix this problem?
    >
    Jim Langston, Apr 27, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pw4getter

    pointer to pointer conversion

    pw4getter, Mar 5, 2004, in forum: C++
    Replies:
    5
    Views:
    3,635
    Casey Carter
    Mar 5, 2004
  2. Replies:
    10
    Views:
    669
    Chris Torek
    Feb 4, 2005
  3. jimjim
    Replies:
    16
    Views:
    822
    Jordan Abel
    Mar 28, 2006
  4. WaterWalk

    Pointer to pointer conversion

    WaterWalk, May 22, 2008, in forum: C Programming
    Replies:
    3
    Views:
    319
    WaterWalk
    May 23, 2008
  5. Replies:
    4
    Views:
    1,229
    Fred Zwarts
    Jul 2, 2009
Loading...

Share This Page