fwrite

This is a discussion on fwrite within the C Programming forums, part of the General Programming Boards category; the program inputs data to an array of structures then prints out the results.. how could i create a function ...

  1. #1
    Unregistered
    Guest

    fwrite

    the program inputs data to an array of structures then prints out the results.. how could i create a function that would write the data to a file?
    also how could i stop getting input if the user just hits return?


    #include <stdio.h>

    //structure to hold entries

    struct entry {
    char fname[15];
    char lname[15];
    char email[30];
    };

    struct entry list[20];

    int x;

    int main()
    {

    //loop to input data

    for (x=0; x<20; x++)
    {
    printf("Enter first name: ");
    scanf("%s", list[x].fname);
    printf("Enter last name: ");
    scanf("%s", list[x].lname);
    printf("Enter Email adress: ");
    scanf("%s", list[x].email);

    }

    printf("\n\n");

    //loop to display data

    for (x=0; x<20; x++)
    {
    printf("Name: %s %s", list[x].fname, list[x].lname);
    printf("\t \tEmail: %s\n", list[x].email);
    }

    return(0);
    }

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    For writing into a file you use command "fwrite" like this:

    FILE *fin;

    for (x=0; x<20; x++)
    {
    printf("Enter first name: ");
    scanf("%s", list[x].fname);
    printf("Enter last name: ");
    scanf("%s", list[x].lname);
    printf("Enter Email adress: ");
    scanf("%s", list[x].email);
    fin = fopen("File.dat", "ab");
    fwrite(&list[x], sizeof(list[x]), 1, fin);
    fclose(fin);
    }

    For reading from the file you use command "fread" like this:

    for (x=0; x<20; x++)
    {
    fread(&list[x], sizeof(list[x]), 1, fin);
    printf("Name: %s %s", list[x].fname, list[x].lname);
    printf("\t \tEmail: %s\n", list[x].email);
    }


    That's all.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I would suggest writing and reading all 20 of the entries at once...
    Code:
    #include <stdio.h> 
    
    //structure to hold entries 
    
    struct entry { 
    char fname[15]; 
    char lname[15]; 
    char email[30]; 
    }; 
    
    struct entry list[20]; 
    
    int x; 
    
    void getData (const char * fname)
    { 
     FILE * f ;
     //loop to input data 
    
     for (x=0; x<20; x++) 
     { 
      printf("Enter first name: "); 
      scanf("%s", list[x].fname); 
      printf("Enter last name: "); 
      scanf("%s", list[x].lname); 
      printf("Enter Email adress: "); 
      scanf("%s", list[x].email); 
     } 
    
     f = fopen (fname, "wb");
     fwrite (list, sizeof(list), 20, f);
     fclose (f);
    
     return;
    }
    
    
    void printData (const char * fname)
    {
    
     FILE * f = fopen (fname, "rb");
     fread (list, sizeof (list), 20, f);
     fclose (f);
    
     //loop to display data 
     
     for (x=0; x<20; x++) 
     { 
      printf("Name: %s %s", list[x].fname, list[x].lname); 
      printf("\t \tEmail: %s\n", list[x].email); 
     } 
    
     return; 
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using fwrite
    By kenkoh in forum C Programming
    Replies: 10
    Last Post: 02-16-2008, 10:34 AM
  2. WriteFile and fwrite
    By George2 in forum C Programming
    Replies: 4
    Last Post: 08-10-2007, 05:33 AM
  3. fwrite error: bad address
    By icebabe in forum C Programming
    Replies: 6
    Last Post: 05-06-2006, 07:35 AM
  4. Determining fwrite errors
    By cboard_member in forum C Programming
    Replies: 2
    Last Post: 04-26-2006, 06:30 AM
  5. fwrite file size
    By lambs4 in forum C Programming
    Replies: 4
    Last Post: 04-04-2003, 05:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21