Thread: while(!feof)

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    13

    while(!feof)

    Code:
        do
        {
            fscanf(file, "%s", wordlist[i]);
            printf("%s\n", wordlist[i]);
            i++;
        }while (!feof(file));
    Every time it reaches the end of the file its crashing...

    The array is big enough...I tryed so many things to fix it... I'm lost...

  2. #2
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    I may be mistaken, but do-while doesn't check until the end of the block, so at the end of the file, it still tries to scan for another string, therefore it crashes because there's nothing there. Use a while loop (while at the beginning.)
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,003
    Generally, it is not advisable to use feof to control a loop because the EOF condition is usually only set after a failed read that detects the end of file. Furthermore, you should check that you are not writing to the array out of bounds. For example:
    Code:
    for (i = 0; i < wordlist_size && fscanf(file, "%s", wordlist[i]) == 1; i++)
    {
        printf("%s\n", wordlist[i]);
    }
    Note that there is still the assumption that each element of wordlist has enough space to store the string read. This tends to be a bad assumption, so you should specify a field width for %s.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    Quote Originally Posted by laserlight View Post
    Generally, it is not advisable to use feof to control a loop because the EOF condition is usually only set after a failed read that detects the end of file. Furthermore, you should check that you are not writing to the array out of bounds. For example:
    Code:
    for (i = 0; i < wordlist_size && fscanf(file, "%s", wordlist[i]) == 1; i++)
    {
        printf("%s\n", wordlist[i]);
    }
    Note that there is still the assumption that each element of wordlist has enough space to store the string read. This tends to be a bad assumption, so you should specify a field width for %s.
    I tryed your solution and it still doesn't work...
    Same crash...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,003
    Quote Originally Posted by mconflict
    I tryed your solution and it still doesn't work...
    Well, I am certain that my example demonstrates the correct idea, so if it still doesn't work, you're doing something wrong...
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    Well.. hmm I have main.c and it opens the file and I call the function and after the function is done I close the file in the main.c... It is the right way?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,003
    Yes, that sounds correct. You need to provide more of your updated code if you want more help.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    Quote Originally Posted by laserlight View Post
    Yes, that sounds correct. You need to provide more of your updated code if you want more help.
    Ok well I think... I found the error but I don't understand it...

    After that little sample of code I have that :

    Code:
        *NumbersOfWordsInList = i;
            *err = OK;
    Its Crashing on the *err pointer... I changed it to err = OK; and it worked perfectly. Why so?


    Thats the definition :

    chargingGame(FILE *file, Grid g, Word wordlist[], int *NumbersOfWordsInList, int *err)
    Last edited by mconflict; 04-02-2012 at 10:22 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,208
    > Its Crashing on the *err pointer... I changed it to err = OK; and it worked perfectly. Why so?
    How should we know, you keep posting random lines you think are relevant to the problem rather than a complete program we can analyse and tell you exactly what is wrong.

    If *err crashes, then your pointer is garbage.

    How it became garbage (or started as garbage) is completely invisible to us.
    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.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    Quote Originally Posted by Salem View Post
    > Its Crashing on the *err pointer... I changed it to err = OK; and it worked perfectly. Why so?
    How should we know, you keep posting random lines you think are relevant to the problem rather than a complete program we can analyse and tell you exactly what is wrong.

    If *err crashes, then your pointer is garbage.

    How it became garbage (or started as garbage) is completely invisible to us.
    The program is huge and it's written in french, so I have to translate it manually when I post it here. I dunno what's else you need... I have some define for the OK like...

    #define OK 0
    #define FICH_INT -1 // Can't find the file
    #define IMPOSSIBLE -2 // Impossible....
    #define PASTROUVE -3 // Can't find...

    If err = OK; means everything is good...
    In the main.c, when the function is done I'm checking that pointer if it's equal to OK.... If It's not OK well something went bad... and prints it.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,208
    If it's that large, then you REALLY need to learn how to use a debugger.

    A debugger would catch the access to a bad pointer, and allow you to investigate where the pointer came from, why it's bad, and help you think about how to make it good.
    Printing variables, setting breakpoints, single-stepping - all should be familiar debug steps to you.

    By the time your programs are too large to post, you should be fairly adept at finding and fixing your own problems yourself.

    You can't dump the whole mess for us to sort out, and nor does posting random lines help either.
    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.

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    Quote Originally Posted by Salem View Post
    If it's that large, then you REALLY need to learn how to use a debugger.

    A debugger would catch the access to a bad pointer, and allow you to investigate where the pointer came from, why it's bad, and help you think about how to make it good.
    Printing variables, setting breakpoints, single-stepping - all should be familiar debug steps to you.

    By the time your programs are too large to post, you should be fairly adept at finding and fixing your own problems yourself.

    You can't dump the whole mess for us to sort out, and nor does posting random lines help either.
    Where can I learn how to use a debugger, I'm using CodeBlock to do my programming

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    2,674

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP: feof
    By dlf723 in forum C Programming
    Replies: 5
    Last Post: 07-23-2010, 08:49 AM
  2. feof() from FAQ
    By salvadoravi in forum C Programming
    Replies: 6
    Last Post: 01-25-2008, 01:08 PM
  3. cannot get out of while( !feof (f) )
    By SoFarAway in forum C Programming
    Replies: 2
    Last Post: 02-19-2005, 03:36 PM
  4. feof()
    By XSquared in forum C Programming
    Replies: 2
    Last Post: 06-02-2004, 12:16 AM
  5. feof ....EOF
    By GanglyLamb in forum C Programming
    Replies: 12
    Last Post: 12-04-2002, 12:38 PM