fgetc returns wrong pointer...?

This is a discussion on fgetc returns wrong pointer...? within the C Programming forums, part of the General Programming Boards category; Hey CPeople... I have made some small tolls using fgetc to read from files, for upload purposes. But now I'm ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    7

    Question fgetc returns wrong pointer...?

    Hey CPeople...

    I have made some small tolls using fgetc to read from files, for upload purposes. But now I'm having an error I can't figure out...?

    I'm using Code:blocks with the GCC.

    Code:
    FILE	*pUploadFile;
    fpos_t 	pFilePosition;
    ...


    Code:
        for (uiCounter = 2; uiCounter < DMX_BUF_SIZE; uiCounter++)					
        {
    		if (!bPacketFull)
                   {
    			ucDMXTxBuff[uiCounter]		= iFileCharRead;
    			iFileCharRead			= fgetc(pUploadFile);
    
    			if(iFileCharRead == EOF)
                           {
    				EOF_Reached		= true;									
    				bPacketFull		= true;
                           }
                   }
    		else
    			ucDMXTxBuff[uiCounter]		= 0x00;
        }
    What happens, I am reading a char at a time in to the array ucDMXTxBuff, which starts off OK. When the pointer for the file is 0x12d, it goes wrong. I reads the byte correct, but it returns a pointer for the file that is now 0x328. In the next cycle it jumps even further, and then some times again it increments with only 1 as I would suppose it should...?

    Do any one have a clue of what might be the problem...?


    Best Regards
    Henrik Green
    Last edited by GreenC; 06-23-2010 at 06:25 AM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It seems to me like you could do the same with this:

    Code:
    for(uiCounter = 2; uiCounter < DMX_BUF_SIZE; uiCounter++)
    {
            if( (iFileCharRead = fgetc(pUploadFile)) != EOF)
                    ucDMXTxBuff[uiCounter]  = iFileCharRead;
    }
    And that is only really necessary if you suspect that the file is larger than your buffer size.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    Thanks... I will try this... the buffer is much smaller than the file. The buffer is of 256bytes, which is temporary, until the contents is transmittet by a USB device.

    The file that I'm reading is a AES encrypted file that I have created my self, and the file seems fine with no errors.

    Anyway I will try the more compact version, to se if there is any luck...

    Thanks... :-) Green

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    Well, though this code is looks better, it still starts to jump forward in the file pointer.... :-( It is still at 0x12d where it jumps to 0x328. This is not near the End Of File.

    I believe that any chars are legal, and should not make the fgetc to increment the pointer with more than one...?


    Best Regards
    Henrik Green
    Last edited by GreenC; 06-23-2010 at 06:41 AM.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I agree, it shouldn't and it doesn't by it self. If it does it's most likely due to some logical error in the code, so simplifying the code leads to fewer places where things can go wrong. From what you have posted I can't tell what causes the file pointer to jump ahead in the file. Perhaps someone else can see it.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    Thanks Subsonics, I agree... I do not expect that fgetc is doing something wrong... It myst be the way I'm using it... Wrong definitions etc... Thanks for the hint, I will try to simplify the code... Then hope that I can see what is tricking this...


    :-) Green

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    I might have been missing what happens when fgetc reads a 0xD and 0xA (carriage return & end of line). So it seems like I will need to handle these two characters differently...


    :-) Green

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If it's binary file, don't forget to open in binary mode.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    Thanks Bayint,

    It is an Intel Hex file, never the less I believe that I have not been aware of which mode that I have opened my file in...


    :-) Green

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgetc doesn't return a pointer, it returns an int. Also, iFileCharRead needs to be an int as well, or your EOF check won't test correctly. What value is it that you think you're testing?


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    Oh yeh, I have that right (iFileCharRead is an int)... It was my terminology that was not all ok... That is I'm testing the iFileCharRead to see if I have reached EOF. But I need to take care of the 0x0D 0x0A as well.

    :-) Green

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. custom doublylinkedlist class returns wrong data
    By r0flc0pter in forum C++ Programming
    Replies: 0
    Last Post: 06-13-2009, 05:18 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. fgetc() skips carriage returns?
    By OnionKnight in forum C Programming
    Replies: 2
    Last Post: 02-02-2006, 09:26 AM
  4. Function returns pointer of unknown type..
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 02-06-2002, 07:41 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM

Tags for this Thread


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