C code assistance please

This is a discussion on C code assistance please within the C Programming forums, part of the General Programming Boards category; I have been working on this code for over 2 weeks now and it finally compiles. I am required to ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    12

    C code assistance please

    I have been working on this code for over 2 weeks now and it finally compiles. I am required to open a text file and validate it. It is supposed to then print the applicable errors to screen .

    What I need help with is:

    1. Regardless of what I type in when prompted for the disk file name in dos, it still opens the file for me. My fopen code must be wrong?

    2. The only error that comes up is error 2 (@%c separator line is expected...). I have rigged the text file so as to bring up the other errors but still only that error comes up.

    3. My "for loop" code doesn't seem right (for you experienced programmers you're probably laughing at it right about now). I don't see what is wrong with it but ...?

    Here is the portion of my code that is applicable. I would really appreciate any suggestions.


    int validate_file ( const char Bank[], int D_A[], int summary )
    {
    FILE * bank;

    int err,
    LargestMark = 1,
    mark, /* difficulty level of an item */
    bankOK = 1, /* no error has been found so far */
    orderOK = 1, /* no out of order difficulty level is met */
    end_old = 0, /* line number at which previous item ends */
    end_new = 0, /* line number at which new item ends */
    separap,
    itembank,
    i; /* loop control */

    char separator ; /* digit that identifies the @ separator */
    /* line of a section in error, if err==2 */
    clrscr () ;

    bank = fopen("C\\TMA307\\itembank.txt", "r");

    if (itembank == NULL)
    {
    printf("\n \n\%sError 7: Unable to open itembank file. %s \n", fopen);
    hit_a_key("\n\n\n");
    return (3);
    }

    if (itembank == EOF)
    {
    printf("\n\n\n\ %sError 1: Unexpected end-of-file has occurred. %s\n");
    hit_a_key("\n\n\n");
    return ( 1 );
    }

    for (i=1; i!=NULL; ++i)
    {
    if(itembank > MAXITEMS)
    {
    printf("\n\n\%sError 8: The number of items in the bank . %s\n", bank, MAXITEMS)
    hit_a_key("\n\n\n");
    return ( 0 );
    }

    if(separap != '@')
    {
    printf("\n\n\n\ %sError 2: @%c separator line is expected. %s\n", separap);
    hit_a_key("\n\n\n");
    return ( 2 );
    }

    //...additional error codes are included here...


    fclose(bank);
    hit_a_key("\n\n\n");
    return (0);
    }
    }















  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    for (i=1; i!=NULL; ++i)
    The compiler converts this to:

    for (i=1; i != 0; ++i) //<---uh-oh!

    This is called a 'dangerous' loop.

    if(itembank > MAXITEMS)
    How are you incrementing 'itembank' ? I don't see it anywhere in your code...You must explicitely 'do something' in the file - whether it's with fgetc, fgets, or fread, you must do more than merely open the file!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    12
    Good point Sebastiani - and thank you for responding to my post so quickly!

    I have only defined the maximum number of items in my header file as #define MAXITEMS 1000. Does it appear that I am not getting into each error code enough? I am attempting to reference a separate function (I was afraid to paste too much code here so I didn't include it in my post).

    I have a real problem with for loops for some reason. I believe what I have is an infinite loop if I'm understanding you right.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    if (itembank == EOF)
    EOF is simply a numerical value: -1, contained within a single byte. Since there is no guarantee that the file will in fact contain this value at the end, then the more reliable method is the function feof(FILE *) which works fine with text files.

    As to your other problem, again, I do not know what you are doing with the file to fill the itembank variable, but from the look of it, not much!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    printf("\n\n\n\ %sError 2: @%c separator line is expected. %s\n", separap);
    Your string is expected to print 3 variables: a string, a char, and another string, in that order...where are they?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    char tester[100];
    
    while( fgets(tester, 100, filePointer) )
    itemBank++;
    
    if(itemBank > MAX)
     {
       fclose(filePointer);
       return someErrorCode;
     }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Sebastiani

    EOF is simply a numerical value: -1, contained within a single byte.
    Not so..... EOF is an int, therefore at least 2 bytes. It can never be a single byte. If it was, you'd never be able to tell the difference between EOF and a char of a matching value.
    Since there is no guarantee that the file will in fact contain this value at the end,....
    EOF isn't contained within the file at all, it is a return code passed to your program by the OS when it recognises the end of the file. Therefore, EOF will always be returned by the OS at end correct time.

    From the code (to go with the comments already posted):

    >bank = fopen("C\\TMA307\\itembank.txt", "r");
    check bank is not null before proceeding too far past this point.

    >if (itembank == NULL)
    You have to set itembank to a value before using it.

    >for (i = 1; i != NULL; ++i)
    don't test against NULL, use 0. And if you start at 1 and count UP, you won't reach 0 particularly quick

    >if (separap != '@')
    Again, you haven't set this variable to a value before testing it.

    As Seb said, trying adding some form of read statement as well !

    Have a go at applying these few things, and post again, with code tags, when you have trouble.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    12

    Thank you

    I just wanted to post my thanks to all that helped with my code. Every reply put me in the direction I needed to correct it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  2. Code Assistance
    By Nezmin2 in forum C Programming
    Replies: 12
    Last Post: 12-19-2008, 12:26 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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