For Programmers: Free Programming Magazines  


Home > Archive > VC Language > May 2006 > Writing to files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Writing to files
Kat

2006-05-22, 7:15 pm

Ok I'm currently working on a project that requires writing to external
files. I've seen many modes for opening files then writing to them.
This includes ios::out, ifstream::binary, and a couple of others.

My current program uses ofstream::binary to write to a file. I have a
test text file sitting in the same folder and every time I try to write
to it, I don't get an error message but it simply does not work(runtime
error). My guess is that I'm trying to write to an ASCII file when the
program is in binary mode but I can't be sure.

Is there a mode that will write to both ASCII and Binary or what mode
writes to ASCII files?

Sean M. DonCarlos

2006-05-22, 7:15 pm

"Kat" wrote:

> My current program uses ofstream::binary to write to a file. I have a
> test text file sitting in the same folder and every time I try to write
> to it, I don't get an error message but it simply does not work(runtime
> error). My guess is that I'm trying to write to an ASCII file when the
> program is in binary mode but I can't be sure.
>
> Is there a mode that will write to both ASCII and Binary or what mode
> writes to ASCII files?


I am not terribly familiar with the Standard C++ Library itself, and without
a code sample illustrating the problem, I'm not going to be able to offer
specific help.

The fact that you said "ofstream::binary" might have something to do with
it. The correct way is
ofstream ofs("Test.txt", ios_base::binary);
but I think the compiler would scream if you use "ofstream::binary" here.

You may also want to check that the program's current directory is the
directory that contains your test file, or else specify a fully-qualified
path to the test file.

You can write to ASCII files in binary mode. The only difference is that LF
characters are not automatically expanded to CR/LF pairs, as they are in
ASCII mode. In binary mode, whatever's in the buffer being written goes
straight to the file without translation. Whether you still have a valid
ASCII text file when you're done depends on what was in the buffer.

You might also consider Win32's WriteFile function, MFC's CFile::Write
method, and/or .NET's StreamWriter class, but you would be getting outside
the Standard C++ Library at that point.

Sean
Jim Langston

2006-05-23, 4:15 am


"Kat" <Rem.Intellegare@gmail.com> wrote in message
news:1148320886.240250.320670@j33g2000cwa.googlegroups.com...
> Ok I'm currently working on a project that requires writing to external
> files. I've seen many modes for opening files then writing to them.
> This includes ios::out, ifstream::binary, and a couple of others.
>
> My current program uses ofstream::binary to write to a file. I have a
> test text file sitting in the same folder and every time I try to write
> to it, I don't get an error message but it simply does not work(runtime
> error). My guess is that I'm trying to write to an ASCII file when the
> program is in binary mode but I can't be sure.


Please show the code that isn't working.

>
> Is there a mode that will write to both ASCII and Binary or what mode
> writes to ASCII files?
>



Kat

2006-05-23, 7:11 pm

Ok here's a code snippet. The program compiles and works fine. I
guess that's because I'm using Visual C++ 6.0(I know, it's my school's
fault, but they're upgrading next year).

//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.sg(0,ifstream::end);
size = infile.tellg();
infile.sg(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\\

Kat

2006-05-23, 7:11 pm

**Runs without error I should say**

Tamas Demjen

2006-05-23, 7:11 pm

Kat wrote:

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

[...]
> size = infile.tellg();

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


You're writing size bytes size times. Even worse, zeroa is a 1-byte
variable, but you're reading size bytes out of it, which will make your
application crash.

I assume you want to write size 0 bytes to the file. To do that, change
your write() call to this:

outfile.write(&zero, sizeof(zero));

By the way, it's pretty inefficient to write single bytes millions of
times. You could probably allocate a 16k buffer, fill it with 0s, and
then write that buffer periodically. Something like this (untested):

const int buff_size = 16384;
char buff[buff_size];
memset(buff, 0, buff_size);
long size_left = size;
while(size_left > 0)
{
const int curr_size = std::min(size_left, buff_size);
outfile.write(buff, curr_size);
size_left -= curr_size;
}

Tom
Tamas Demjen

2006-05-23, 7:11 pm

Tamas Demjen wrote:
> outfile.write(&zero, sizeof(zero));


Oops, using your naming convention, the correct way would be

outfile.write(&zeroa, sizeof(zeroa));

or simply

outfile.write(&zeroa, 1);

Tom
Kat

2006-05-24, 7:10 pm

No the program doesn't crash anymore. It will write 0's as well. I
actually want 0 to be handled as a character instead of an integer for
this particular program. Plus the buffer size has to be variable.
It's the size of the file it's going to overwrite(it's a secure
deletion program that overwrites files with 0-9, and a-z then random
data).

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com