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!

You're on the right track. You want to use the fread() and fwrite() functions with a pointer to an instance of your structure and the FILE pointer.


#define Tax 0.19

It is customary to use all-upper case when you define a constant

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;

You need to add a ";" at the end of the struct products_array declaration or to remove the struct product_array part out of the declaration of the variable products.

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 */
else
return TRUE;
}


void create_file( char * filename[25], FILE * fptr )
{
FILE * fptr;

fptr = fopen( filename, "w" );
return fptr;
}

void display_file( char *filename[25] ) returns TRUE or FALSE so it can't be of type void.
Same with void create_file( char * filename[25], FILE * fptr )
using gets() function is asking for troubles. Check this link

/*
 *    write_structure.c
 *
 */
#include <stdio.h>

struct info
{
    char name[30];
    int    age;
    float height;
    char sex;
};

int main( void )
{
    FILE *f_info;
    struct info subject;
    
    strcpy( subject.name, "I am me" );
    subject.age = 24;
    subject.height = 5.4;
    subject.sex = 'M';
    
    f_info = fopen( "subjects.dat", "w" );
    if( f_info == NULL )
    {
        puts( "Error opening file" );
        exit(1);
    }
    fwrite( &subject, sizeof subject, 1, f_info );
    
    fclose( f_info );
    
    puts( "subject.dat saved to disk" );
    
    getchar();
    return 0;
}
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.