Thread: Evul Fread() Not Working =(

  1. #1

    Angry Evul Fread() Not Working =(


    struct Ip {
    char *ip;
    struct Container {
    struct Ip IpList[AllowedIps];

    void putiplist(Container *cont) {
    FILE *fp;
    const char sig[] = "Contain File";
    fp = fopen("allowedips.lst", "wb");
    if ( fp == NULL )
    derror("PutIpList(): Null File Pointer");
    int y = fwrite (&sig, sizeof(const char), sizeof(sig), fp);
    int x = fwrite (cont, sizeof(struct Container), 1, fp);
    printf("[%d-%d]-[%s]\n", y, x, cont->IpList[1].ip);

    void getiplist(Container *cont) {
    FILE *lfp;
    char sig[12];
    lfp = fopen("allowedips.lst", "rb");
    if ( lfp == NULL )
    derror("GetIpList(): Null File Pointer");
    int y = fread (sig, sizeof(sig), 1, lfp);
    int x = fread (&cont, sizeof(struct Container), 1, lfp);
    printf("[%d-%d]-[%s]\n", y, x, cont->IpList[1].ip);

    Hopefully this code should explain itself, I'm trying to just save a Container structure. For some reason, this refuses to work, maybe Container is not a good struct name to use? =/
    Thanks for any help.


  2. #2
    Registered User
    Join Date
    Feb 2002
    I see quite a few things wrong with that code. Here is an example that might help you.

    #include <stdio.h>

    struct zigzag {
    int a;
    char b[20];

    int writeBinary(const struct zigzag);
    int readBinary(struct zigzag *);

    int main() {
    struct zigzag myZig, inZig;
    struct zigzag *ptr;

    /* the memory address we will send to readbinary */
    ptr = &inZig;

    /* assign some dummy values */
    myZig.a = 24;
    strcpy(myZig.b, "Blubber");

    /* Write the struct myZig and then read it back in */

    /* proof that it was successful */
    printf("inZig.a: %d\n", inZig.a);
    printf("inZig.b: %s\n", inZig.b);

    int writeBinary(const struct zigzag j) {
    FILE *outFile;

    /* open the file we are writing to */
    if(!(outFile = fopen("binout", "w")))
    return 1;

    /* use fwrite to write binary data to the file */
    fwrite(&j,sizeof(struct zigzag),1,outFile);


    return 0;

    int readBinary(struct zigzag *b) {
    FILE *inFile;

    if(!(inFile = fopen("binout", "r")))
    return 1;

    fread((struct zigzag *)b,sizeof(struct zigzag),1,inFile);


    return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fread is overwriting other variables in my program
    By shortchar in forum C Programming
    Replies: 3
    Last Post: 07-13-2007, 06:25 PM
  2. How to load pixels of BMP into an array
    By brconner in forum Windows Programming
    Replies: 10
    Last Post: 06-02-2007, 04:30 AM
  3. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2007, 11:25 PM
  4. return value of fread()
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-24-2006, 06:31 PM
  5. Reasons as to why fread would fail
    By Happy_Reaper in forum C Programming
    Replies: 4
    Last Post: 04-08-2006, 11:41 AM
Website Security Test