Thread: I have a file I/O question as well

  1. #1

    I have a file I/O question as well

    here is the code I am having a problem with, I cannot get the save or load of my files to work. Can anyone help me find my errors? I am new to programming but here is what I have:

    void filesave(void)
    FILE *fpout;

    if(first==(struct account *)NULL)
    puts("There are no records to print!");
    if ( (fpout = fopen("test.txt","w+")) == NULL )
    printf("\n error opening the test.txt file!");



    /* copy to file */

    fprintf (fpout,"%i\n", out->custID);
    fprintf (fpout,"%s\n", out->lastname);
    fprintf (fpout,"%s\n", out->firstname);
    fprintf (fpout,"%s\n", out->street);
    fprintf (fpout,"%s\n", out->city);
    fprintf (fpout,"%s\n", out->state);
    fprintf (fpout,"%s\n", out->zip);
    fprintf (fpout,"%s\n", out->rented);
    fprintf (fpout,"%f\n", out->balance);

    while((current=current->next) != (struct account *)NULL);

    puts("Files Copied!");

    return 0;

    void openrecords(void)
    FILE *fpin;

    if ((fpin = fopen("test.txt","r")) == NULL)
    printf("\n error opening the test.txt file!");

    /* a while() statement to find eof */

    newptr = (struct account *)malloc(sizeof(struct account));

    if(first==(struct account *)NULL)
    first = current = newptr;
    current->prior = NULL;

    current = first;

    while(current->next != (struct account *)NULL)
    current = current->next;

    last = current;
    current->next = newptr; /*save the address of the new record*/
    current = newptr;
    current->prior = last;

    fscanf(fpin, "%i", out->custID);
    fscanf(fpin, "%s", out->lastname);
    fscanf(fpin, "%s", out->firstname);
    fscanf(fpin, "%s", out->street);
    fscanf(fpin, "%s", out->city);
    fscanf(fpin, "%s", out->state);
    fscanf(fpin, "%s", out->zip);
    fscanf(fpin, "%s", out->rented);
    fscanf(fpin, "%f", out->balance);

    current->next = (struct account *)NULL;

    return 0;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > if(first==(struct account *)NULL)
    There's no need to cast NULL

    And you may as well add the {}, even if it's only one statement. It saves finding some tricky bugs later on when you try and add another statement, and forget the {}

    > fprintf (fpout,"%i\n", out->custID);
    Shouldn't this be
    fprintf (fpout,"%i\n", current->custID);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File i/o and ASCII question
    By muzihc in forum C Programming
    Replies: 13
    Last Post: 11-04-2008, 11:46 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  5. Another dumb question about file i/o
    By Cobras2 in forum C++ Programming
    Replies: 23
    Last Post: 03-14-2002, 04:15 PM