Jump to content

Writing a structure into a file

- - - - -

  • Please log in to reply
3 replies to this topic

#1
teensicle

teensicle

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
I still am trying to find out how to do this! i've written a simple code but it doesn't seem to build.



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


typedef struct {
    char  p_name[10];
    int age;
} records;


int main (void)
{




    FILE *fp;
    fp=fopen("test.bin", "wb");


        printf("Enter a name: \n");
            scanf("%s", &records.p_name);


        printf("Enter an age: \n");
            scanf("%d", &records.age);


    fwrite(p_name, sizeof(p_name),1,fp);


    fwrite(age, sizeof(age), sizeof(age),1,fp);


}


PePe===> BLACKSPADE EMPIRE<===PePe

www.blackspade-ent.com

#2
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts

 fwrite(p_name, sizeof(p_name),1,fp);

 fwrite(age, sizeof(age), sizeof(age),1,fp);


How you are gonna access p_name and age directly? And the first statement got 4 arguments for the function while the second have 5.

---------- Post added at 12:23 AM ---------- Previous post was at 12:01 AM ----------

fwrite
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

Parameters
ptr
Pointer to the array of elements to be written.
size
Size in bytes of each element to be written.
count
Number of elements, each one with a size of size bytes.
stream
Pointer to a FILE object that specifies an output stream.

Return Value
The total number of elements successfully written is returned as a size_t object, which is an integral data type.
If this number differs from the count parameter, it indicates an error.

If I edit your code,

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>



struct record{

    char  p_name[10];

    int age;

};



int main()

{


    struct record rec;



    FILE *fp;

    fp=fopen("test.bin", "wb");



        printf("Enter a name: \n");

            scanf("%s", &rec.p_name);



        printf("Enter an age: \n");

            scanf("%d", &rec.age);



    fwrite(rec.p_name, 1, sizeof(rec.p_name),fp);

    fwrite(rec.age, sizeof(int), sizeof(rec.age),fp);


    fclose(fp);


}


Hope this Helps!
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
typedef struct A{
...
} B;
This is just a shortcut so you don't have to type struct for each instance. You haven't created an actual object to use it.
//instead of typing
struct A object;
// you can say
B object;

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
A declaration:
struct date{
   int day;
   int month;
   int year;
};
The above declares a structure, date, with three fields. With the above declaration we could declare variables of type struct date like this:
struct date dob; // hold data for a give date of birth
We can now access the different field of date.
dob.day // would access the day field
dob.month //would access the month field. etc
You cannot access a date field by directly using the field name. You need to use the member operator, period(.).
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users