Thread: What's wrong in thses two functions?

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    33

    What's wrong in thses two functions?

    First one :
    Code:
    void Full_Movie_List()
    {
        struct Movies INFO;  //declare a structure variable
    
        FILE *pfile;
        pfile=fopen("zero_requiem.txt","rb");
    
    if (pfile!=NULL)
        {
         printf("%.20s%.20s%.20s%.20s%s,Movie Name, Movie Genre, Movie Language, Movie Year, Actor Name");
         do
         {
             fread(&INFO,sizeof(INFO),1,pfile);
             if(!feof(pfile))
         {
            printf("%.20s%.20s%.20s%.20s%s,Movie Name, Movie Genre, Movie Language, Movie Year, Actor Name");
         }
         }while(!feof(pfile));
          fclose(pfile);
         }
    else
        {
            printf("ERROR");
        }
    
    
    }
    The program stops working when I choose to view the full list.


    The second one is a searching function.

    Code:
    void Search_Movie()
    {
         struct Movies INFO;
         char target[100];
         int flag=0;
    
        FILE *pfile;    //FILE pointer
        pfile=fopen("zero_requiem.txt","rb");
    
        if (pfile!=NULL)
        {
            printf("\nEnter name of movie to find");
            scanf("%s",&target);
            fflush(stdin);
            do
            {
                fread(&INFO,sizeof(INFO),1,pfile);
                if(!feof(pfile));
                {
                    if(strcmp(target,INFO.Movie_Name)==0)
                    printf("\nRecord found");
                    printf("Movie Name%s Movie Genre%s Movie Year%s Movie Language %s Actor Name",INFO.Movie_Name,INFO.Movie_Genre,INFO.Movie_Language,INFO.Movie_Year,INFO.Actor_Name);
                    flag=1;
                }
            }while(!feof(pfile));
            }fclose(pfile);
            if(flag==0)
            {
            printf("\n\n Match not found");
            }
    
    
    }
    And this one doesn't work either.

    I'm not sure, but the problem might be with my menu function as well.
    So, here's the menu function.

    Code:
    void Menu()
    
    
    {
    
             int SELECT;
    
        do
        {
    
            printf("\nMAIN_MENU:\n");
            printf("\t\t1.ADD MOVIE\n\t\t2.SEARCH MOVIE\n\t\t3.FULL MOVIE LIST\n\t\t4.EDIT MOVIE\n\t\t5.EXIT");
            printf("\n\nSELECT:");
            scanf("%d",&SELECT);
    
            if (SELECT==1)
            {
                Add_Movie();
    
    
    
            }
    
            else if(SELECT==2)
            {
    
                Search_Movie();
    
    
            }
            else if(SELECT==3)
    
            {
    
                Full_Movie_List();
    
    
            }
    
            else if(SELECT==4)
            {
    
                Edit_Movie();
    
    
            }
    
            else if(SELECT==5)
            {
                system("cls");
                printf("\n\n\n\t\t\t\tGOOD BYE\n\n\n\n\n");
                system("pause");
                system("cls");
            }
    
    
             else
            {
                system("cls");
                printf("\n\n\n\aINVALID INPUT\nPLEASE TRY AGAIN\a\n\n\n\n\n");
    
            }
    
    
    
    
    
        } while(SELECT!=5);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You're mis-using feof() all over the place
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    All you need is
    while ( fread(&INFO,sizeof(INFO),1,pfile) == 1 )

    Code:
                    if(strcmp(target,INFO.Movie_Name)==0)
                    printf("\nRecord found");
                    printf("Movie Name%s Movie Genre%s Movie Year%s Movie Language %s Actor Name",INFO.Movie_Name,INFO.Movie_Genre,INFO.Movie_Language,INFO.Movie_Year,INFO.Actor_Name);
                    flag=1;
    You also need to understand that indentation has nothing to do with scope.
    Here, only the first line is controlled by the if statement. The other two lines are going to happen anyway, regardless.

    > scanf("%s",&target);
    > fflush(stdin);
    1. You don't need the & when scanning into a char array with %s
    2. fflush(stdin) is undefined - FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    > if(!feof(pfile));
    Aside from the feof() issue already mentioned, you need to watch out for misplaced ; at the end of control statements.

    > printf("%.20s%.20s%.20s%.20s%s,Movie Name, Movie Genre, Movie Language, Movie Year, Actor Name");
    Perhaps you meant
    printf("%.20s%.20s%.20s%.20s%s","Movie Name", "Movie Genre", "Movie Language", "Movie Year", "Actor Name");
    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 2014
    Posts
    33
    Still not working.
    did this:
    Code:
    void Search_Movie()
    {
         struct Movies INFO;
         char target[100];
         int flag=0;
    
        FILE *pfile;    //FILE pointer
        pfile=fopen("zero_requiem.txt","rb");
    
        if (pfile!=NULL)
        {
            printf("\nEnter name of movie to find");
            scanf("%s",target);
            fflush(stdin);
            do
            {
                fread(&INFO,sizeof(INFO),1,pfile);
                if(!feof(pfile));
                {
                    if(strcmp(target,INFO.Movie_Name)==0)
                    printf("\nRecord found");
                    printf("Movie Name%s Movie Genre%s Movie Year%s Movie Language %s Actor Name",INFO.Movie_Name,INFO.Movie_Genre,INFO.Movie_Language,INFO.Movie_Year,INFO.Actor_Name);
                    flag=1;
                    break;
                }
            }while ( fread(&INFO,sizeof(INFO),1,pfile) == 1 );
            }fclose(pfile);
            if(flag==0)
            {
            printf("\n\n Match not found");
            }
    
    
    }

    Code:
    void Full_Movie_List()
    {
        struct Movies INFO;  //declare a structure variable
    
        FILE *pfile;
        pfile=fopen("zero_requiem.txt","rb");
    
    if (pfile!=NULL)
        {
         printf("%.20s%.20s%.20s%.20s%s","Movie Name", "Movie Genre", "Movie Language", "Movie Year", "Actor Name");
         do
         {
             fread(&INFO,sizeof(INFO),1,pfile);
             if( fread(&INFO,sizeof(INFO),1,pfile) == 1 );
         {
            printf("%.20s%.20s%.20s%.20s%s","Movie Name", "Movie Genre", "Movie Language", "Movie Year", "Actor Name");
         }
         }while( fread(&INFO,sizeof(INFO),1,pfile) == 1 );
          fclose(pfile);
         }
    else
        {
            printf("ERROR");
        }
    
    
    }
    The program stops working after giving any input.

    My instructor used fflush(stdin) that's why I don't want to change it.
    Last edited by philia; 12-13-2014 at 09:10 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by philia View Post
    My instructor used fflush(stdin) that's why I don't want to change it.
    Find another instructor.

    As to your other problems, you would need to provide information on what your struct Movies type is. If it contains pointers (as distinct from arrays) you should not be using fwrite() to save it save it, or fread() to retrieve from file. Because that is a almost 100% guaranteed way to achieve undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Code:
    struct Movies
    {
        int  Movie_Year;
        char Movie_Name[100],Movie_Genre[50],Movie_Language[50],Actor_Name[100];
    
    };
    Should I use fscanf and fprintf?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Technically, the size of int and size of structs (e.g. padding) are implementation defined (i.e. vary between compilers). You don't need to use fscanf() and fprintf() unless the compiler (or compiler settings that affect data layout) used to build the writing code differ from the compiler used to build the reading code.

    Your last version of Search_Movie will skip every second entry.

    Your last version of Full_Move_List() will only print one entry in three (or, more accurately, it skips two entries of every three).


    You haven't really provided enough information to point to your problem though. The key advice I can offer though: stop guessing (i.e. don't just type away in the hope that your code will start to work). Try REASONING about what you want your program to do. If you don't understand what the effect is of changes you make, the odds against your code ever working.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Quote Originally Posted by grumpy View Post
    Your last version of Search_Movie will skip every second entry.

    Your last version of Full_Move_List() will only print one entry in three (or, more accurately, it skips two entries of every three).
    Why? And how do I solve this?

    For more info, I'm posting the whole code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct Movies
    {
        int  Movie_Year;
        char Movie_Name[100],Movie_Genre[50],Movie_Language[50],Actor_Name[100];
    
    };
    void Menu();
    void Add_Movie();
    void Search_Movie();
    void Full_Movie_List();
    void Edit_Movie();
    void EXIT();
    
    int main()
    
    {
        Menu();
    
        return 0;
    }
    
    
    void Menu()
    
    
    {
    
             int SELECT;
    
        do
        {
    
            printf("\nMAIN_MENU:\n");
            printf("\t\t1.ADD MOVIE\n\t\t2.SEARCH MOVIE\n\t\t3.FULL MOVIE LIST\n\t\t4.EDIT MOVIE\n\t\t5.EXIT");
            printf("\n\nSELECT:");
            scanf("%d",&SELECT);
    
            if (SELECT==1)
            {
                Add_Movie();
    
    
    
            }
    
            else if(SELECT==2)
            {
    
                Search_Movie();
    
    
            }
            else if(SELECT==3)
    
            {
    
                Full_Movie_List();
    
    
            }
    
            else if(SELECT==4)
            {
    
                Edit_Movie();
    
    
            }
    
            else if(SELECT==5)
            {
                system("cls");
                printf("\n\n\n\t\t\t\tGOOD BYE\n\n\n\n\n");
                system("pause");
                system("cls");
            }
    
    
             else
            {
                system("cls");
                printf("\n\n\n\aINVALID INPUT\nPLEASE TRY AGAIN\a\n\n\n\n\n");
    
            }
    
    
    
    
    
        } while(SELECT!=5);
    }
    
    
    
    void Add_Movie()
    
    {
    
    
       struct Movies INFO;  //declare a structure variable
    
        FILE *pfile;    //FILE pointer
        pfile=fopen("zero_requiem.txt","ab");
    
        if (pfile!=NULL)
     {
    
      printf("\t\t\tMovie Name\t\t\n\n");
    
      printf("\nENTER Movie Name:");
      fflush(stdin);
      gets(INFO.Movie_Name);
    
      printf("\nENTER Genre:");
      fflush(stdin);
      gets(INFO.Movie_Genre);
    
      printf("\nENTER Language:");
      fflush(stdin);
      gets(INFO.Movie_Language);
    
      printf("\nENTER Actor Name:");
      fflush(stdin);
      gets(INFO.Actor_Name);
    
    
      printf("\nENTER Year:");
      scanf("%d",&INFO.Movie_Year);
      fwrite(&INFO,sizeof(INFO),1,pfile);
    
    
    
    
    
    
     fclose(pfile);
     }
    
     else        //NULL means file not opened successfully
       {
            printf("\nCould not open file\n");
    
    
        }
    
    
    
    system("pause");
    system("cls");
    
    }
    
    
    
    
    
    void Search_Movie()
    {
         struct Movies INFO;
         char target[100];
         int flag=0;
    
        FILE *pfile;    //FILE pointer
        pfile=fopen("zero_requiem.txt","rb");
    
        if (pfile!=NULL)
        {
            printf("\nEnter name of movie to find");
            scanf("%s",&target);
            fflush(stdin);
            do
            {
                fread(&INFO,sizeof(INFO),1,pfile);
                if(!feof(pfile));
                {
                    if(strcmp(target,INFO.Movie_Name)==0)
                    printf("\nRecord found");
                    printf("Movie Name%s Movie Genre%s Movie Year%s Movie Language %s Actor Name",INFO.Movie_Name,INFO.Movie_Genre,INFO.Movie_Language,INFO.Movie_Year,INFO.Actor_Name);
                    flag=1;
                    break;
                }
            }while ( fread(&INFO,sizeof(INFO),1,pfile) == 1 );
            }fclose(pfile);
            if(flag==0)
            {
            printf("\n\n Match not found");
            }
    
    
    }
    void Full_Movie_List()
    {
        struct Movies INFO;  //declare a structure variable
    
        FILE *pfile;
        pfile=fopen("zero_requiem.txt","rb");
    
    if (pfile!=NULL)
        {
         printf("%.20s%.20s%.20s%.20s%s","Movie Name", "Movie Genre", "Movie Language", "Movie Year", "Actor Name");
         do
         {
             fread(&INFO,sizeof(INFO),1,pfile);
             if( fread(&INFO,sizeof(INFO),1,pfile) == 1 );
         {
            printf("%.20s%.20s%.20s%.20s%s","Movie Name", "Movie Genre", "Movie Language", "Movie Year", "Actor Name");
         }
         }while( fread(&INFO,sizeof(INFO),1,pfile) == 1 );
          fclose(pfile);
         }
    else
        {
            printf("ERROR");
        }
    
    
    }
    
    
    
    
    void Edit_Movie()
    {
        struct Movies INFO;
        char target;
        FILE *ptr=fopen("zero_requiem.txt","rb");
        FILE *ptemp=fopen("temp.txt","wb");
        if(ptr  != NULL && ptemp != NULL)
        {
            printf("Enter Name: ");
            fflush(stdin);
            scanf("%s",&target);
            do
            {
                fread(&INFO,sizeof(INFO),1,ptr);
    
                if(!feof(ptr))
                {
                    if(strcmp(target, INFO.Movie_Name) == 0)
                    {
                        scanf("%s",&INFO.Movie_Name);
                    }
    
                    fwrite(&INFO,sizeof(INFO),1,ptemp);
    
                }
            }while(!feof(ptr));
    
        }fclose(ptr);
        fclose(ptemp);
          remove("zero_requiem.txt");
        rename("temp.txt","zero_requiem.txt");
    }
    
    void EXIT()
    {
    
    }

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by philia View Post
    Why? And how do I solve this?
    Every call of fread() reads an entry from the file. If nothing is done with the result it is discarded.

    This code (which I've extracted and formatted a bit better) from your Full_Movie_List().
    Code:
         do
         {
             fread(&INFO,sizeof(INFO),1,pfile);
             if( fread(&INFO,sizeof(INFO),1,pfile) == 1 );
            {
                printf("%.20s%.20s%.20s%.20s%s","Movie Name", "Movie Genre", "Movie Language", "Movie Year", "Actor Name");
            }
         }while( fread(&INFO,sizeof(INFO),1,pfile) == 1 );
    There are three calls of fread() for a every printf() - two before, one after. So only one line in three is printed.

    To solve it you need one call of fread() per call of printf().
    Last edited by grumpy; 12-13-2014 at 10:22 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Code:
    {       printf("%.20s%.20s%.20s%.20s%s","Movie Name", "Movie Genre", "Movie Language", "Movie Year", "Actor Name");
    
       }
    Do I need to use this after every fread?
    EDIT: I get it. I'm trying it.

    And in the function search_movie, is the problem with fflush(stdin)?
    What should I use instead of fflush?

  10. #10
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    I did this:
    Code:
    void Full_Movie_List()
    {
        struct Movies INFO;  //declare a structure variable
    
        FILE *pfile;
        pfile=fopen("zero_requiem.txt","rb");
    
    if (pfile!=NULL)
        {
         printf("%.20s%.20s%.20s%.20s%s","Movie Name", "Movie Genre", "Movie Language", "Movie Year", "Actor Name");
         do
         {
             fread(&INFO,sizeof(INFO),1,pfile);
             {
            printf("%.20s%.20s%.20s%.20s%s","Movie Name", "Movie Genre", "Movie Language", "Movie Year", "Actor Name");
         }
             if( fread(&INFO,sizeof(INFO),1,pfile) == 1 );
         {
            printf("%.20s%.20s%.20s%.20s%s","Movie Name", "Movie Genre", "Movie Language", "Movie Year", "Actor Name");
         }
         }while( fread(&INFO,sizeof(INFO),1,pfile) == 1 );
         {
            printf("%.20s%.20s%.20s%.20s%s","Movie Name", "Movie Genre", "Movie Language", "Movie Year", "Actor Name");
         }
          fclose(pfile);
         }
    else
        {
            printf("ERROR");
        }
    
    
    }
    But I can't see the added movies, in the full movie list. When I slelct Full movie list, the input it get is:
    movie namemovie yearmovie genremovie languagemovie yearactor namemovie namemovie yearmovie genremovie languagemovie yearactor namemovie namemovie yearmovie genremovie languagemovie yearactor namemovie yearmovie genremovie languagemovie yearactor namemovie namemovieyearmovie genremovie languagemovie yearactor

    This is kinda depressing lol.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    <sigh>

    Did I suggest at all that you need to add more printf() statements??? No, I did not.

    The first call of printf() prints fields from an uninitialised struct Movies. That gives undefined behaviour.

    The second call of printf() (the first in the do/while loop) will occur whether or not the fread() call succeeds or fails. That gives undefined behaviour if the first fread() fails.

    The third call of printf() will only occur if the second call fread() succeeds.

    The data read by the third call of fread() - the one within the while() condition - will be discarded if the fread() succeeds, since the first statement calling fread() will be executed again.

    The last printf() will only be called after a fread() call has failed. That will be okay if at least one call of fread() succeeds (it will print the last lot of data successfully read), but will give undefined behaviour if all calls of fread() have failed (which will happen if the file is empty).



    You have taken my point about there being three fread() calls per printf(), and added one call of printf() for every fread(). But, again, you're guessing and not thinking about what you are trying to achieve.


    Essentially your loop needs to call printf() for every call of fread() that succeeds, and do nothing otherwise. I'll give a hint: you should be able to do it with exactly ONE statement that calls fread(), and exactly ONE statement that calls printf() exactly ONCE if the last call of fread() has succeeded.

    And, an even bigger hint: if you need to use the return value of fread() more than once (for example: to check if the printf() is needed, then to check if the loop is done) then store it in a variable, and check the value of the variable. Don't call fread() multiple times - each time you call it discards data before printing it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by philia View Post
    My instructor used fflush(stdin) that's why I don't want to change it.
    If millions of people use the wrong way, it will not become the truth.
    In this forum are really good coder and if they say 'its wrong, don't do that', than, please, follow the suggestion.

    The intelligense is a constant, but the universe is growing every day.
    Other have classes, we are class

  13. #13
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Quote Originally Posted by grumpy View Post
    And, an even bigger hint: if you need to use the return value of fread() more than once (for example: to check if the printf() is needed, then to check if the loop is done) then store it in a variable, and check the value of the variable.
    How to do that? Storing fread value in a variable? I don't think I know about this.
    However I tried it with 1 printf & one fread.
    But the out is not correct.
    Code:
    void Full_Movie_List()
    {
    
        struct Movies INFO;  //declare a structure variable
     
        FILE *pfile;
        pfile=fopen("zero_requiem.txt","rb");
    
    
    
    if (pfile!=NULL)
    {
    
    
         if(fread(&INFO,sizeof(INFO),1,pfile));
    
         {
            printf("%.20s%.20s%.20s%.20s%s","Movie Name", "Movie Genre", "Movie Language", "Movie Year", "Actor Name");
         }
    
         fclose(pfile);
    }
    
    else
        {
            printf("ERROR");
        }
    
    
    }

  14. #14
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by philia View Post
    How to do that? Storing fread value in a variable? I don't think I know about this.
    However I tried it with 1 printf & one fread.
    But the out is not correct.
    Your program print allways the same static line.
    You should print the content from stuct Movies.
    Code:
    int Full_Movie_List(void)
    {
    
        struct Movies INFO;  //declare a structure variable
     
        FILE *pfile;
        pfile = fopen("zero_requiem.txt", "rb");
    
        if (pfile != NULL)
        {
            if(fread(&INFO, sizeof(INFO), 1, pfile))
            {
                printf("%.20s%.20s%.20s%.20s%s",
                    INFO.Movie_Name, INFO.Movie_Genre, INFO.Movie_Language,
                    INFO.Movie_Year, INFO.Actor_Name
                );
            }
            fclose(pfile);
        }
        else
        {
            printf("ERROR\n");
            return -1;
        }
        return 0;
    }
    I have taken the struct members from your first post. I hope they are correct.
    I also changed the returned value of your function to int.
    With this, the caller can check if the function runs with or without problems.
    Last edited by WoodSTokk; 12-15-2014 at 06:17 AM.
    Other have classes, we are class

  15. #15
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    ^Doesn't work unfortunately. The program runs. But when I select full movie list in the menu, it stops working. I'm really not sure, what to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What s wrong with my functions?
    By letmoon in forum C Programming
    Replies: 1
    Last Post: 10-18-2009, 02:16 AM
  2. what is the difference between thses commands?
    By transgalactic2 in forum C Programming
    Replies: 10
    Last Post: 04-10-2009, 09:56 PM
  3. cant imagine the structure of root after thses stages..
    By transgalactic2 in forum C Programming
    Replies: 18
    Last Post: 04-01-2009, 10:24 AM
  4. Functions, what am I doing wrong here?
    By mattbbx in forum C++ Programming
    Replies: 12
    Last Post: 08-15-2003, 02:27 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM