Thread: HELP! infinite loop

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    Red face HELP! infinite loop

    this function that i have written is generating some infinite loop when reading from the file. if some could help i would be very greatful.


    /*validates an account if it exists-----*/
    /*--------------------------------------*/
    int _validate_account_ins(int vtypeof, char vlname[20], char vfname[20],int vpin){

    int fnd_pin, fnd_type;

    if((ac_p=fopen("account_.txt","r+"))==NULL){
    clrscr();
    gotoxy(25,10);
    printf("::Error occured while attempting to open accounts file::");
    getchar();
    getchar();
    clrscr();
    return 0;
    }else{
    while(!feof(ac_p))
    {
    fscanf(ac_p,"%d\n\n%d",&fnd_pin, &fnd_type);

    if(fnd_pin==vpin){
    if(fnd_type==vtypeof){
    fclose(ac_p);
    clrscr();
    gotoxy(25,10);
    printf("::Account already exits::");
    getchar();
    getchar();
    clrscr();
    return 0;
    }
    }
    /*if you print something here you will see the loop*/
    }
    fclose(ac_p);
    }


    /*insert the record*/
    if(_insert_account(vtypeof,vfname,vlname,vpin)!=1) {
    clrscr();
    gotoxy(25,10);
    printf("::Failed to enter Account::");
    getchar();
    getchar();
    clrscr();
    return 0;
    }else{
    clrscr();
    gotoxy(25,10);
    printf("::Account Inserted Successfully::");
    getchar();
    getchar();
    clrscr();
    return 1;
    }
    }
    /*--------------------------------------*/

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    And read this too

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    printf("::Error occured while attempting to open accounts file::");
    To print errors, use the specific function to this job, perror()

    if(fnd_pin==vpin){
    if(fnd_type==vtypeof){
    Isn't more elegant:
    Code:
    if ((fnd_pin == vpin) && (fnd_type == vtypeof))
    {
        ....
        ....
    }

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    only three lines are being returned

    Code:
     while (!feof(fp))
      {
        fgets(buf, sizeof(buf), fp);
        printf ("Line %4d: %s", i, buf);
        i++;
      }
    the code above is only printing the first three lines of the my file, the one it is reading from.

    the text file reads:
    1
    Jose
    Kurt
    1234

    what is printed:
    1
    Jose
    Kurt

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Don't use feof() to control the loop.
    Change it to:
    Code:
    while (fgets(buf, sizeof(buf), fp) != NULL)
      {
        printf ("Line %4d: %s", i, buf);
        i++;
      }

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    brethren, brethren the thing still a give the same

    same o same o!

    this the same three lines

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    I did a program with the basics, and I can read all the lines:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MYFILE "c:\\Netlog.txt"
    
    int main(void)
    {
        FILE *fpIn;
        int nLine = 0;
        char buf[BUFSIZ];
        
        //lets open the file, if have errors, print out
        if ((fpIn = fopen(MYFILE,"r")) == NULL) {
            perror("Open: ");
            return (EXIT_FAILURE);
        }
        
        //read the file until the end
        while (fgets(buf,sizeof(buf),fpIn) != NULL) {
            printf("Line number %3d: %s\n",nLine,buf);
            nLine++;
        }
        
        //close the file and exit the program
        fclose(fpIn);
        system("PAUSE");
        return 0;
    }
    Can you explain me, what is vpin, vtypeof, I guess the problem is on that part.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    6
    the thing is that i want to retrieve values from particular positin in the file.

    for instance the following
    1
    Jose
    Martin
    1234
    2
    Shane
    Garri
    2345
    1
    wini
    march
    2222

    i want to retrieve the highlited sections from the file into particular variables, and then check if they are equal to vtype and vpin.

  10. #10
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    You can't just jump like this with fscanf().
    To get the numbers, try something like:
    Code:
    while (fscanf(fpIn,"%d%*s%*s%d",&nNum1,&nNum2) == 2)
    or use fgets() and parse it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM