writing into file in c

S

sam_cit

Hi Everyone,

I have a buffer having some raw ASCII data and i want to write it
into a file.
One way is to loop through the buffer and write a single character at
a time and close the file after the loop exits.

However i feel this will be a bit slow, is there any way the entire
buffer's contents in RAM can be copied directly into the file?

Thanks in advance!!!
 
M

mark_bluemel

Hi Everyone,

I have a buffer having some raw ASCII data and i want to write it
into a file.
One way is to loop through the buffer and write a single character at
a time and close the file after the loop exits.

However i feel this will be a bit slow, is there any way the entire
buffer's contents in RAM can be copied directly into the file?

On many platforms " *(int *)0 = -1; " will force the whole process
image, including the buffer, to be written to a core file...

fwrite is probably what you are really looking for, though.
 
R

Richard Heathfield

(e-mail address removed) said:

On many platforms " *(int *)0 = -1; " will force the whole process
image, including the buffer, to be written to a core file...

Bad programmer! No biscuit!
fwrite is probably what you are really looking for, though.

Oh, you chickened out... :)
 
M

Martin Ambuhl

Hi Everyone,

I have a buffer having some raw ASCII data and i want to write it
into a file.

Note that "raw ASCII data" is meaningless as far as C is concerned. I
will assume you meant "char data"
One way is to loop through the buffer and write a single character at
a time and close the file after the loop exits.

As long as the data contains no '\0' chars, fprintf will probably do
what you want. But see below.
However i feel this will be a bit slow, is there any way the entire
buffer's contents in RAM can be copied directly into the file?

It may or may not be slow. But, in any case, fwrite and fread may be
optimized (see below).
Thanks in advance!!!
Save some bangs for your next premature ejaculation.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char data[] = "This is 'data', some character data which should\n"
"be written at one go to a file.\n"
"When we check the file, we will read it into 'check',\n"
"which starts life holding \"deadbeaf\\n\". That should be\n"
"overwritten.\n";
char check[sizeof data] = "deadbeaf\n";
FILE *f;
const char fname[] = "./tmpfile";
printf("The original 'data' string:\n%s\n", data);
printf("The original 'check' string:\n%s\n", check);
if (!(f = fopen(fname, "wb"))) {
fprintf(stderr, "Attempt to open \"%s\" for output failed.\n\""
"Giving up ...\n", fname);
exit(EXIT_FAILURE);
}
printf("\"%s\" is open for output,\n"
"writing 'data' using fwrite\n\n", fname);
printf("%d chars written.\n",
(int) fwrite(data, 1, sizeof data, f));
printf("fclose %s.\n\n", fclose(f) ? "failed" : "succeeded");


if (!(f = fopen(fname, "rb"))) {
fprintf(stderr,
"Attempt to open \"%s\" for input failed.\n\""
"Giving up ...\n", fname);
exit(EXIT_FAILURE);
}
printf("\"%s\" is open for input,\n"
"reading 'check' using fread\n\n", fname);
printf("%d chars read.\n", (int) fread(check, 1, sizeof check, f));
printf("fclose %s.\n\n", fclose(f) ? "failed" : "succeeded");



printf("The final 'data' string:\n%s\n", data);
printf("The final 'check' string:\n%s\n", check);


printf("remove %s.\n\n", remove(fname) ? "failed" : "succeeded");
return 0;
}

The original 'data' string:
This is 'data', some character data which should
be written at one go to a file.
When we check the file, we will read it into 'check',
which starts life holding "deadbeaf\n". That should be
overwritten.

The original 'check' string:
deadbeaf

"./tmpfile" is open for output,
writing 'data' using fwrite

205 chars written.
fclose succeeded.

"./tmpfile" is open for input,
reading 'check' using fread

205 chars read.
fclose succeeded.

The final 'data' string:
This is 'data', some character data which should
be written at one go to a file.
When we check the file, we will read it into 'check',
which starts life holding "deadbeaf\n". That should be
overwritten.

The final 'check' string:
This is 'data', some character data which should
be written at one go to a file.
When we check the file, we will read it into 'check',
which starts life holding "deadbeaf\n". That should be
overwritten.

remove succeeded.
 

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,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top