473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

IOStream file overwriting

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

May 24 '06 #1
3 8831
Kat wrote:
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

May 24 '06 #2
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\\

May 24 '06 #3
Kat wrote:
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

May 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: anas.hashmi | last post by:
I am trying to write to the beginning of a file. The reason: I want to make a form where board webmasters can use it to insert in updates to a webpage without having to go directly into the web...
5
by: Joe Delphi | last post by:
Hi Newbie to VB.Net and I have a question I need to open a text file, read each line, and if I find something in the line, delete that line from the text file. Can anyone tell me how to do...
1
by: ma740988 | last post by:
Trying to some of my reading on file manipulation (ifstream/ofstream/fstream) into action. So now consider. # include <iostream> # include <fstream> # include <cstring> # include <sstream> ...
3
by: Mark | last post by:
My experience with reading/writing files is to open one file, read it into memory close it and open another file and write to it. Now I would like to learn how to read and write to the same file....
7
by: Adrián E. Córdoba | last post by:
Hi, there! I'm developing a little application which must record some events in a log file (.txt). How can I delete the first line in the log file every time I add a new line at the end, in order...
4
by: piruk | last post by:
How to overwrite text to a file? When I use: file << text then the text is added to the end of file. piruk
5
by: JonathanB | last post by:
Ok, so this is the scenario. I need to create a simple, no-frills XML editor for non-technical users. It doesn't have to do anything fancy, what I want is a series of text boxes with the text...
7
by: CodeGrommet | last post by:
I'm testing out the system() command. I coded the following. How can I output the results into the given output file? Your help will be appreciated. /* system example : DIR */ //code based...
9
by: larsjohanson | last post by:
Hello! I have used freeASPUpload.asp (from http://www.freeaspupload.net/ ) to upload files to my server. However, there where two problems: It doesn't support Unicode (I use UTF-8) Already...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.