[Show all top banners]

chanaa_tarkaari
Replies to this thread:

More by chanaa_tarkaari
What people are reading
Subscribers
Subscribers
[Total Subscribers 1]

desert_rain
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 c++ question to programmers ----
[VIEWED 4739 TIMES]
SAVE! for ease of future access.
Posted on 07-13-07 6:04 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I know there are many programming experts here in Sajha, I am going to put here a simple problem I faced today seeking your suggestions.

I am trying to write a numeric array into a binary file using fwrite. The variable is three dimensional converted into two dimensional double type, var[k][t], where [k] contains two-dimension inside. k is not sequential but has to be picked up from i and j released by separate routine. I coded

fwrite(var[k][t], sizeof(var[0][t]), sizeof(var[k][t]), FILE*);

but it displayed the following error message,

error: cannot convert `double' to `const void*' for argument `1' to `size_t fwrite(const void*, size_t, size_t, FILE*)'

I have seen fwrite is used successfully in writing character arrays. How can I use this to write numeric array?

It seems that I need to add something to successfully convert 'double' to 'const void*', but did not get any clue on that.

Any help or suggestions are welcome.
 
Posted on 07-13-07 10:08 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 
 
Posted on 07-14-07 1:51 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

cast the return of the double.. it might work or barf for some other reason later.. but u will b able to pass this one..

(const void * (var [k][t]) )

this should do it.. for ur compilation
 
Posted on 07-14-07 4:16 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

From the error message and your code fragment it seems that var is declared as a 2 dimensional array (look at the error message, it says cannot convert 'double' instead of 'double *' => var[k][t] is double instead of double *).

The first argument to fwrite() has to be a pointer. You can use type casting but make sure that the pointer points to contiguous block of memory (might not be contiguous if you dynamically allocate 2 or higher dimensional arrays).

The third argument to fwrite() in your code fragment seems to have some problems. Although it is correct syntactically, it might have some semantic issues. sizeof(var[k][t]) is sizeof(double) if var is 2D or sizeof(double *) if var is 3D. The point is that it will always be constant (unless this is what you want).
 
Posted on 07-14-07 9:23 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Thanks Thapap and |<1$|-| for valuable suggestions.

Thapap's idea worked out to solve the compiling error message when I added
const void * (var [k][t]) ;
just before the fwrite statement. But it produced segmentation fault while running the program.

It is true the var[k][t] is declared as two dimensional double. The memory allocation is dynamic. Declaring const void*(var[k][t]) in the beginning of program caused further problem of conflict in declaration, with the following error message.

error: conflicting declaration 'const void*var[((long unsigned int)((long int)k))][((long unsigned int)((long int)t))]'

All examples I could find on fwrite illustrate just writing character type data array. Could not find any good referece to write the numeric data array in binary form,
 
Posted on 07-14-07 1:12 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Here is a sample code to write a 2D array of double to a file and read it back. Things work similarly for higher dimensions and dynamically assigned arrays.

#include
#include

int main()
{
FILE * opfile, * readfile;
double var[5][5], varr[5][5];
int i,j,k;

for(i = 0; i < 5; i++)
for(j = 0; j < 5; j++)
var[i][j] = i+j+1;

opfile = fopen("storefile", "w");
if(opfile == NULL){
printf("Oops!\n");
return 0;
}

fwrite(var, sizeof(double), 25, opfile);

fclose(opfile);
readfile = fopen("storefile", "r");

fread(varr, sizeof(double), 25, opfile);
for(i = 0; i < 5; i++){
for(j = 0; j < 5; j ++)
printf("%f %f\n", varr[i][j], var[i][j]);
}

return 0;
}
 
Posted on 07-14-07 1:20 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

heres the code again:

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

int main()
{
FILE * opfile, * readfile;
double var[5][5], varr[5][5];
int i,j,k;

for(i = 0; i < 5; i++)
for(j = 0; j < 5; j++)
var[i][j] = i+j+1;

opfile = fopen("storefile", "w");
if(opfile == NULL){
printf("You suck\n");
return 0;
}
fwrite(var, sizeof(double), 25, opfile);

fclose(opfile);
readfile = fopen("storefile", "r");

fread(varr, sizeof(double), 25, opfile);
for(i = 0; i < 5; i++){
for(j = 0; j < 5; j ++)
printf("%f %f\n", varr[i][j], var[i][j]);
}

return 0;
}


 
Posted on 07-14-07 1:50 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

looks like ^ bro overtook me hehe.

but since i put my 5 mins into it, here's how i would write numeric values to a binary file :P
========

#include
#define size 3
int main ()
{

FILE * pFile;
struct var{
double x1;
double x2;
};
typedef struct var var;

var vararr[size]={{3.4,4.5},{5.4,6.1},{7.1,8.9}};

pFile = fopen ( "file.bin" , "wb" );
fwrite (vararr , sizeof(var) , size , pFile );

fclose (pFile);
return 0;
}
======
 
Posted on 07-15-07 9:41 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Thanks |<1$|-| and Loote for sample codes. Sajha members rock.
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 200 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
Anybody gotten the TPS EAD extension alert notice (i797) thing? online or via post?
TPS EAD auto extended to June 2025 or just TPS?
Toilet paper or water?
ढ्याउ गर्दा दसैँको खसी गनाउच
Mamta kafle bhatt is still missing
ChatSansar.com Naya Nepal Chat
whats wrong living with your parents ?
TPS advance parole Travel document i-131, Class of Admission ?
Tourist Visa - Seeking Suggestions and Guidance
Now Trump is a convicted criminal .
Ajay Kumar Dev sentenced to 378 yrs
I hope all the fake Nepali refugee get deported
Biden said he will issue new Employment visa for someone with college degree and job offers
Why Americans reverse park?
Problems of Nepalese students in US
Nepali Passport Renewal
lost $3500 on penny stocks !!!
Biden out, Trump next president, so what’s gonna happen to TPS, termination?
They are openly permitting undocumented immigrants to participate in federal elections in Arizona now.
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters