Thread: Help with a file writing program

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    29

    Help with a file writing program

    Hi !!,

    I am trying to write a file writing program. This program writes the data to a text file. I wanted it to keep doing so till i tell it to exit. The code compiles without a problem, but after i input the data i get a message saying that this operation has to be terminated or something like that. Please help


    #include <stdio.h>
    main()
    {
    int num;
    struct test
    {
    char memname [10];
    char memadd [40];
    int ph;
    int age;
    char sex;
    }info;
    /*
    the file should be opened at this stage*/

    //fflush(stdin);
    for (num=1;num<num+1;num++)
    {

    FILE *fp;
    fp=fopen("member.txt","a+");

    printf ("\n\t\t\tENTER MEMBER NO. :%d:\t\t\t\n",num);
    printf ("\nEnter the Member's Name : \n");
    scanf("%s",info.memname);

    printf ("\nEnter the Member's Address : \n");
    scanf ("%s",info.memadd);

    printf ("\nEnter the Phone Number : \n");
    scanf ("%d",&info.ph);

    printf ("\nEnter the Age of the Member : \n");
    scanf ("%d",&info.age);
    fflush (stdin);
    printf ("\nEnter the gender of the Member : \n");
    scanf ("%c",&info.sex);
    printf ("----MEMBER NO :%d:----\n\nName : %s\nAddress : %s\nAge :%d\nPhone Number :%d\nGender :%c\n",num,info.memname,info.memadd,info.age,info. ph,info.sex);

    fprintf (fp,"\n\n\t----MEMBER NO :%d:----\t\nName : %s\nAddress : %s\nAge :%d\nPhone Number :%d\nGender :%c\n",num,info.memname,info.memadd,info.age,info. ph,info.sex);
    system ("cls");

    fclose (fp);
    }


    system("PAUSE");
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (num=1;num<num+1;num++)
    Erm, this loop will never terminate (or run for a very long time)

    > fp=fopen("member.txt","a+");
    Why do you open and close the file each time around the loop?
    Doesn't seem to matter, it seems to work anyway

    > fflush (stdin);
    This isn't standard - or more specifically, the effects are undefined.

    This is better
    &nbsp; while ( getchar() != '\n' ) continue;

    > but after i input the data i get a message saying that this operation has to be terminated
    Did memname or memadd ever have more than 9 or 39 chars respectively input into them?

    Seems OK otherwise (ran here without problems - DJGPP)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    29

    The problem continues !!

    Hi !!,

    Thanx alot for taking time to reply. I did not put more than
    the mentioned range of characters. I use a BloodShed Dev C++ Compiler. But the problem still continues.

    No error or warning is shown while compiling but as soon as the program gets some input till the last field it hangs. Another thing I wanted to ask is how did you find the code? is it good? Does it adhere to programming norms.

    I would be extremely thankful if you could replyb me about the same.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I wanted it to keep doing so till i tell it to exit.
    How do you do this?
    There is no stop in the code at present.

    > fp=fopen("member.txt","a+");
    Add
    Code:
    if ( fp == NULL ) {
        perror( "Unable to open file" );
    }
    > but as soon as the program gets some input till the last field it hangs.
    I'm still not sure what this means?

    I get something like this
    Code:
                            ENTER MEMBER NO. :1:
    
    Enter the Member's Name :
    fred
    
    Enter the Member's Address :
    at-home
    
    Enter the Phone Number :
    1234
    
    Enter the Age of the Member :
    22
    
    Enter the gender of the Member :
    F
    ----MEMBER NO :1:----
    
    Name : fred
    Address : at-home
    Age :22
    Phone Number :1234
    Gender :F
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM