IOStream file overwriting

K

Kat

Ok my problem with this program is that currently it isn't overwriting
a dummy TXT file I have set up to test the program. At one point it
did however so I know it should work but now, for some reason, it
won't. I'm using Visual C++ 6.0(yes I know, my school is upgrading
next year) and this program compiles and runs without error(minus the
fact that it doesn't work).

Here's the code

//Begin Code\\


const char zeroa = '0';
const char *zero = &zeroa;


const int MAXCHARS = 81;
char fileloc[MAXCHARS];
cout << "File name(including extension):";
cin.getline(fileloc,MAXCHARS,'\n');


// Open the file in binary mode for read access
ifstream infile (fileloc,ifstream::binary);


// Get the size of the file and close it
long size;


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


// Open the file for write access
ofstream outfile (fileloc,ofstream::binary);


// Overwrite the file with numbers


int inte;


for(inte = 0; inte < size; inte++)
{
outfile.write (zero,size);


}


inte = 0;

//End Code\\

Thanks
 
J

Jonathan Mcdougall

Kat said:
Ok my problem with this program is that currently it isn't overwriting
a dummy TXT file I have set up to test the program. At one point it
did however so I know it should work but now, for some reason, it
won't. I'm using Visual C++ 6.0(yes I know, my school is upgrading
next year) and this program compiles and runs without error(minus the
fact that it doesn't work).

The code you provided does not compile as is. Take the habit of posting
compilable code (as much as possible, of course).
Here's the code

//Begin Code\\


const char zeroa = '0';
const char *zero = &zeroa;

What does that mean?
const int MAXCHARS = 81;
char fileloc[MAXCHARS];
cout << "File name(including extension):";
cin.getline(fileloc,MAXCHARS,'\n');

Do read about std::string and std::getline().
// Open the file in binary mode for read access
ifstream infile (fileloc,ifstream::binary);

// Get the size of the file and close it
long size;
infile.seekg(0,ifstream::end);
size = infile.tellg();

Watch out. std::basic_istream::tellg() returns a std::ios::pos_type,
not a long. I would suggest the use of std::size_t instead.
infile.seekg(0);
infile.close();
// Open the file for write access
ofstream outfile (fileloc,ofstream::binary);

// Overwrite the file with numbers

int inte;

for(inte = 0; inte < size; inte++)
{
outfile.write (zero,size);

That's wrong. "zero" is a pointer to a single char and write() uses its
second argument to determine how many characters to write. So unless
your file has only one character in it, this is undefined behavior. I
think you mean

outfile.write(zero, 1);

or simply

outfile.put('0');

or still

outfile << '0';


Jonathan
 
K

Kat

sorry

//Begin Code\\

#include <iostream>
#include <fstream>


// Set up number 0 as a constant character then set up a pointer(zero)
to point to constant(zeroa)
const char zeroa = '0';
const char *zero = &zeroa;

using namespace std;

int main(int argc, char* argv[])
{

/* Set up a constant that will(supposedly) define the maximum amount of
characters for the string that will reference the file name and path */

const int MAXCHARS = 81;
char fileloc[MAXCHARS];
cout << "File name(including extension):";
cin.getline(fileloc,MAXCHARS,'\n');


// Open the file in binary mode for read access
ifstream infile (fileloc,ifstream::binary);


// Get the size of the file and close it
long size;


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


// Open the file for write access
ofstream outfile (fileloc,ofstream::binary);


// Overwrite the file with numbers


int inte;


for(inte = 0; inte < size; inte++)
{
/* Tell the computer to write zero's over the file. The size variable
defines the size of the file to be overwritten so that every bit in the
file is overwritten with zero's */
outfile.write (zero,size);


}


inte = 0;
return 0;
}
//End Code\\
 
M

mlimber

Kat said:
sorry [etc.]

You may have posted compilable code this time, but you didn't take
Jonathan's other points into account. Listen to what he has already
said, and then if you still need help, ask afresh.

Cheers! --M
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top