Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem in file writing.

Reply
Thread Tools

Problem in file writing.

 
 
Bangalore
Guest
Posts: n/a
 
      09-14-2005
Hi all,
I was trying out the following program, I want to copy the content of
struct x
variable to file. Here using write of fwrite(used with FILE *fl) I am
not able to
write integer part to the file.
Is there any to copy all contents of structure to file?

struct x{
char a[10];
int t;
}z={"String",10};

int main()
{
int fd;
char buff[100];

if( fd=open("/root/users/PRI/text",2|O_CREAT,0766))<0)
printf("Error in getting fd \n");
else
write(fd,&z,sizeof(z));

read(fd,buff,100);
printf("The content is = %s \n",buff);
}

Thanks,
Bangalore

 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      09-14-2005
Bangalore wrote:

Note that this is a C program rather than a C++ program.

> I was trying out the following program, I want to copy the content of
> struct x variable to file.


probably a bad idea. If you write raw binary to a file you will only
be able to read it back with the same version of the compiler.
Different compilers use different padding and byte ordering. This can
even apply to different versions from the same supplier. Even different

compiler options.

An alternative is to write text. There are portable file formats.
XDR, ASN.1, XML

> Here using write of fwrite (used with FILE *fl)


um. actually you don't you are using (the non-standard) write().

> I am not able to write integer part to the file.
> Is there any to copy all contents of structure to file?


fwrite() I'd have said...

#include <stdio.h>
if you use fopen(), fwrite() and fread()

> struct x{
> char a[10];
> int t;
> }z={"String",10};
>
> int main()
> {
> int fd;
> char buff[100];
>
> if( fd=open("/root/users/PRI/text",2|O_CREAT,0766))<0)


non-standard

> printf("Error in getting fd \n");
> else
> write(fd,&z,sizeof(z));


non-standard

>
> read(fd,buff,100);


non-standard. Why not read sizeof(z)? I can't see any obvious reason
why this wouldn't work.

> printf("The content is = %s \n",buff);
> }


you read *binary* data then tried to print it as a string.
Try declaring another x structure.

struct x z2;
memcpy (z2, buff, sizeof(struct x));
printf ("%s %d\n", z2.a, z2.t);

untested, uncompiled, unchecked against the documentation...

If you have more questions on this C program post to comp.lang.c and
not comp.lang.c++


--
Nick Keighley

A ruby trembled. Two tourmaline nets failed to rectify the laser beam.
A diamond noted the error. Both the error and the correction went into
the general computer.
Corwainer Smith "The Dead Lady of Clown Town"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting JPG file ppt file [Powerpoint] file zxcvar Digital Photography 7 06-22-2009 07:54 PM
Reading of file by next of map file and by next of file descriptor. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 1 07-10-2007 02:46 AM
How to create MDF file (.mdf) file from XML file. Dave ASP .Net 1 06-07-2007 11:32 PM
In file parsing, taking the first few characters of a text file after a readfile or streamreader file read... .Net Sports ASP .Net 11 01-17-2006 12:44 AM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57