Hi! i am creating a struct and i want to save it in a FILE (example FILE *fptr). it creates a file anw and it will fill some elements in the struct. Below is what i hv done so far. Btw sorry for my bad english.
The reason for my unknowledge is cz i am studing in a uni in greece where everything is upsidedown with no books and proffesors who are not give a shit(most of them))... and its up to us to learn how to find the solutions of the exercises.
#include <stdio.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
#define Tax 0.19
void display_file( char * filename[25] );void create_file( char * filename[25], FILE * fptr );
struct products_array
{char Code[3];char description[20];int available_stock;float price_per_item;float Tax;float total_price_per_item;
}struct products_array products;
char filename[25];
int main( void )
{int opt, i;
FILE * fptr;if ( display_file( filename ) == FALSE )
{
puts( "\n Couldn't open data file!" );
printf( "\n Choose one fron the below choices" );
printf( "\n [1] Enter a new name to search" );
printf( "\n [2] Create file with current name" );
scanf( "%d", & i );switch ( i )
{case 1:
{
display_file( filename );break;
}
case 2:
{
create_file( filename, fptr );break;
}
}
}else
puts( "\n Data file found!" );while ( opt > 4 || opt < 1 )
{
printf( "\n [1] Insert Entry" );
prinrf( "\n [2] View Entry" );
printf( "\n [3] Show File" );
printf( "\n [4] Show Contents of the File" );
printf( "\n Choose an option\n" );
scanf( "%d", & opt );
}switch ( opt )
{case 1:{blah blah}
.....................................
....................................
}
}
void display_file( char * filename[25] ) /* read an ASCII file and display on screen */
{int ch;/* use int since EOF = -1 */
FILE * fptr;
clrscr();
printf( "\n Enter name of file : " );
gets( filename );
fptr = fopen( filename, "r" );/* open file for disk reading */
if ( fptr == 0 )return FALSE, filename;/* file opening unsuccessful */elsereturn TRUE;
}
void create_file( char * filename[25], FILE * fptr )
{
FILE * fptr;
fptr = fopen( filename, "w" );return fptr;
}
THANKS IN ADVANCE!