473,769 Members | 3,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

writing into file in c

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!!!

Apr 23 '07 #1
3 4764
On 23 Apr, 12:05, sam_...@yahoo.c o.in wrote:
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.

Apr 23 '07 #2
ma**********@po box.com said:

<snip>
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... :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 23 '07 #3
sa*****@yahoo.c o.in wrote:
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_FAILU RE);
}
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_FAILU RE);
}
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.

>
Apr 23 '07 #4

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

Similar topics

48
8506
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential problem. The program may crash unexpectedly while writing to the file. If so, my program should detect this during startup, and then (during startup) probably delete the data added to the file and redo the writing operation.
6
23603
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
3
3406
by: ishekar | last post by:
Hi, I have an application where i want to write data to a file, the data is being sent from an external source. I know the total size of the data and then i retrieve the data in small segments from the source. This data is written to file in a loop. My question. 1. Will it be useful to increase the file size initially then seek to 0 and start writing to file. whether there will be any performance improvements
1
717
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a streamwriter writing to a network share, is it possible for the network to go down in such a way that the tartet file is only partialy written? or are there some kind of check sums to prevent this.
2
6860
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to replace just one item. I know I can get the entire array, then save the whole thing (with a for loop and if statements so that the changed data will be saved), but it seems like a lot of unnecessary reading and writing. Is there a way to directly save...
4
2179
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will write the information about user name, filename upload, filesize, date and time of uploading into the log file. (The name of the log file is constructed by Current Year and Current Month in my program). Is there any problems with writing into the...
0
1719
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005 12:36:14 -0700 Local: Mon,May 23 2005 3:36 pm Subject: Writing to text file Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse
16
7188
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain unchanged). How can I do that in Python? Claudio Grondi
6
5273
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
2696
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID, lsRecordType, lsXNbr, lsFiscYr, "Beg", CStr(H.BegBalAccDepn), CStr(H.BegBalCost), CStr(H.BegBalCostReval), CStr(H.BegBalDepCost), CStr(H.BegBalDepnReval)) The program is running from within a Virtual PC
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.