Hi! As you can see by my name I'm new to programming. I have a program that only works partially. It consists of two functions. One function creates a text file and reads it and the other one creates a binary file and reads it. The text file part works fine, but the binary part doesn't. It seems to create a file and write the contents but it doesn't read. So my question is why doesn't it read the contents of the file I created?
Thanks in advance!
totalnoob 0 Light Poster
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
float re;
float im;
} complex ;
complex* entervalues( int amount ,complex*z);
void textfile(complex*,int amount);
void binaryfile(complex*,int amount);
int main(int argc, char *argv[])
{
char answer1,choice;
int m,i,amount;
complex *z,*z_array;
printf("This program reads an arbitrary amount of complex numbers\n");
printf("and stores them in a new file as either a text file or a binary file\n\n");
printf("Do you wish to continue? (press 'y' to continue)");
scanf("%c",&answer1);
while(answer1=='y')
{
printf("How many complex numbers n will be written?");
scanf("%d",&amount);
z_array= entervalues(amount,z);
printf(" \n\n Textfile or binary file?\n");
printf("To write to a text file please press t\n");
printf("To write to a binary file please press b\n");
printf("enter choice: ");
fflush(stdin); scanf("%c",&choice);
switch(choice)
{
case 't':
textfile(z_array,amount);
break;
case 'b':
binaryfile(z_array,amount);
break;
}
printf("\n Again?\n");fflush(stdin);
printf("Please enter answer(press 'y' to continue): ");scanf("%c",&answer1);
}
system("PAUSE");
return 0;
free(z);free(z_array);
}
complex* entervalues( int amount,complex* z)
{
z = (complex *) calloc(amount,sizeof(complex));
printf("\n\n Please enter two values per number(z)\n ");
int m;
for (m=0; m<amount;m++)
{
static int i=1;
printf("Please enter z%d: ",i++); //Here you write a complex number e.g. 1+2
scanf("%f %f",&(z+m)->re,&(z+m)->im);
}
return z;
}
void textfile(complex*z_array, int amount)
{
FILE *textfile;
char name[20],filename[20];
float temp;
printf("Please enter file name: ");
scanf("%s",name);
sprintf(filename,"%s", &name);
textfile = fopen(filename, "w+");
int k=0;
for ( k=0;k<amount;k++)
{
fprintf(textfile, "%f %f ",(z_array+k)->re,(z_array+k)->im);
}
rewind(textfile);
printf("The numbers are: ");
while (fscanf( textfile,"%f",&temp), !feof( textfile ))
{
fprintf( stdout," %.2f ",temp);
}
fclose( textfile );
return ;
}
void binaryfile(complex*z_array ,int amount)
{
char filename[20];
FILE *binaryfile;
float temp;
char name[20];
printf("Please enter file name: ");
scanf("%s",filename);
binaryfile = fopen( filename, "w+b");
fwrite( z_array ,amount* sizeof( *z_array ) , 1 , binaryfile);//I think this part works. When i open the
// file with notepad I can see that the program
//has written something in jiberish, but I assume
rewind(binaryfile); //that the numbers are right.
printf("The numbers are: ");
while (!feof(binaryfile))
{
fread (&temp,amount*sizeof( *z_array ), 1 ,binaryfile); //This part doesn't work! It only writes the 1st number
printf(" %.2f ", temp ) ; //to the screen...
}
fclose( binaryfile );
return ;
}
Sci@phy 97 Posting Whiz in Training
How do you expect it to work when you write "complex" into file, and try to get "float" out of it?
Narue 5,707 Bad Cop Team Colleague
>fread (&temp,amount*sizeof( *z_array ), 1 ,binaryfile);
temp is a float. Read your reference on fread again, double check the actual values you're passing it, and you'll see a problem.
totalnoob 0 Light Poster
Thanks for your help!
It works! :icon_cheesygrin:
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.