From: raaki on
Hello group members,

i am a newbie and trying to write array values to a file.

array values are dynamic

so i need to create file first and later append the values

here is the code i tried...

the file is created but it is empty

can any one help....

void filewrite (unsigned int *sad, unsigned char *path)
{

unsigned int i;
unsigned int *dat;
unsigned int *write_tmp;
int op_return;
unsigned int tmp;
dat = &sad[0];

write_tmp = malloc (sizeof (unsigned int));

op_return = open (path, O_CREAT | O_APPEND, S_IRWXU | S_IRWXG);
if (op_return < 0)
{
printf ("Error in opening file %s\n", path);
//exit(0);
}

for (i = 0; i < 980; i++)
{
tmp = (unsigned int) dat[0];
write_tmp = &tmp;
write (op_return ,write_tmp, 2);
dat++;
}


close (op_return);

}


main(){
constant char* path = "./file.txt";
....
...
filewrite (sad,path);
...
...
}


there is one warning
In function `filewrite':
warning: passing arg 2 of `write' from incompatible pointer type
From: Jerry Coffin on
In article <adc51911-fdcb-4735-8ba5-360ad83c8141@
79g2000hsk.googlegroups.com>, hemanth004(a)gmail.com says...
> Hello group members,
>
> i am a newbie and trying to write array values to a file.
>
> array values are dynamic
>
> so i need to create file first and later append the values

[ ... ]

At least for now you should probably avoid using open() and write()
directly. You haven't said whether you're using C or C++ though. If
you're using C++, I'd advise using an std::ofstream. For that matter,
I'd advise that you avoid using an array directly, and use an
std::vector instead. An std::vector automatically handles the dynamic
allocation for you, and keeps track of its own size.

If you're using C, you probably want to use a FILE * instead.

If I'm understanding the intent of your code correctly, a rough
equivalent using an ofstream would look something like:

void filewrite(unsigned int *sad, char const *path) {
std::ofstream outfile(path, std::ios::binary);

outfile.write((char *)sad, 980*sizeof(unsigned int));
}

Using a FILE *, this would look only slightly different:

void filewrite(unsigned int *sad, char const *path) {
FILE *outfile = fopen(path, "wb");
fwrite(sad, sizeof(unsigned int), 980, outfile);
}

Even if you insist on continuing to use open() and write(), you probably
want to do it differently -- specifically, you want to write the entire
array as a single chunk instead of writing each element individually:

void filewrite(unsigned int *sad, char const *path) {
int file_handle = open(path, O_CREAT);

write(file_handle, sad, 980*sizeof(unsigned int));
}

In all of these, I've left the assumption that sad points to an array of
980 unsigned ints. You almost never want to make an assumption like that
in real code; likewise, you almost never want to hard-code a value like
'980' into your code either.

--
Later,
Jerry.

The universe is a figment of its own imagination.
From: raaki on
Hi Jerry,

thanks for ur suggestions.i forget to menction that i am using C and
in real script 980 is replaced by variable.

i am not using stdio.h to use FILE option.

we use our own written lib(headers) where write is defined as

int write(int fdesc,char* buf,unsigned long int nbyte) // usava
unsigned int
{
asm("break 0x131");
asm("nop");
}

in the header file .

even after ur suggestions i still have the same problem

tried options are

void filewrite(unsigned int *sad, char const *path) {
int file_handle = open(path, O_CREAT|O_APPEND, S_IRWXU|
S_IRWXG);

write(file_handle, sad, 1);

}
other method ...

void filewrite (unsigned int *sad, unsigned char *path)
{

unsigned int i;
unsigned int *dat;
unsigned int *write_tmp;
int op_return;
unsigned int tmp;
dat = &sad[0];

write_tmp = malloc(sizeof (char));

op_return = open (path,O_CREAT|O_APPEND, S_IRWXU|S_IRWXG);
if (op_return < 0)
{
printf ("Error in opening file %s\n", path);
exit(0);
} else {


tmp = (char)dat[0];
write_tmp=&tmp;
write (op_return , write_tmp , 1);
dat++;
}


close (op_return);

}

i still get the same warning as i have stated bfore and the file is
empty.

any other ideas...

thks,
raaki

From: Jerry Coffin on
In article <8021a285-b73b-49f2-b3f4-f37f0c7871a6
@m44g2000hsc.googlegroups.com>, hemanth004(a)gmail.com says...
> Hi Jerry,
>
> thanks for ur suggestions.i forget to menction that i am using C and
> in real script 980 is replaced by variable.
>
> i am not using stdio.h to use FILE option.
>
> we use our own written lib(headers) where write is defined as

[ ... ]

> i still get the same warning as i have stated bfore and the file is
> empty.
>
> any other ideas...

It sounds a great deal as if the 'write' function you've defined simply
doesn't work.

--
Later,
Jerry.

The universe is a figment of its own imagination.
From: raaki on
On Jun 25, 6:02 pm, Jerry Coffin <jcof...(a)taeus.com> wrote:
> In article <8021a285-b73b-49f2-b3f4-f37f0c7871a6
> @m44g2000hsc.googlegroups.com>, hemanth...(a)gmail.com says...
>
> > Hi Jerry,
>
> > thanks for ur suggestions.i forget to menction that i am using C and
> > in real script 980 is replaced by variable.
>
> > i am not using stdio.h to use FILE option.
>
> > we use our own written lib(headers) where write is defined as
>
> [ ... ]
>
> > i still get the same warning as i have stated bfore and the file is
> > empty.
>
> > any other ideas...
>
> It sounds a great deal as if the 'write' function you've defined simply
> doesn't work.
>
> --
> Later,
> Jerry.
>
> The universe is a figment of its own imagination.

my intension is to show you those are the parameters needed for write
func,but exact header description i couldnt find

what about the procedures tried,is there any mistake?

thks ,
raaki