I have a file I/O question as well

This is a discussion on I have a file I/O question as well within the C Programming forums, part of the General Programming Boards category; here is the code I am having a problem with, I cannot get the save or load of my files ...

  1. #1
    Unregistered
    Guest

    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!");
    else
    {
    if ( (fpout = fopen("test.txt","w+")) == NULL )
    {
    printf("\n error opening the test.txt file!");
    exit(0);
    }

    current=first;

    do
    {

    /* 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!");
    system("pause");

    fclose(fpout);
    return 0;
    }

    void openrecords(void)
    {
    FILE *fpin;

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

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

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

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

    else
    {
    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;
    }


    fclose(fpin);
    return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    > 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, 10: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-17-2005, 11:09 PM
  5. Another dumb question about file i/o
    By Cobras2 in forum C++ Programming
    Replies: 23
    Last Post: 03-14-2002, 03:15 PM

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