Question about reading into a character array (C)

Discussion in 'Mac Programming' started by mac2x, Nov 5, 2010.

  1. mac2x macrumors 65816

    Sep 19, 2009
    Hi all,

    I would like to clarify that I want general answers and ideas so I can THINK about how to use it in my code. I will NOT post any code since it is an assignment.

    I'm having a problem with reading into a character array from a file which consists of a list of single characters (all letters from the alphabet). All that is going on is that it is reading control characters such as \r into the array as well, which messes up the results when the array is used in a later function.

    I have used fflush() before to remove unwanted newlines in console input into a char array, but that doesn't seem to work in my function to read data into the array from a file.

  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    General ideas.

    1. Find a way to remove newlines or other control characters directly from the input stream. You mentioned fflush(). So either investigate that function more deeply as to why it's not working on a file, or find another function that does what you want.

    "Investigate more deeply" means at least reading its man page, since I don't see anything there that suggests fflush() does what you said. If you have an example, you should post the code that shows this behavior. I realize you said you weren't posting code, so make it the smallest possible example that illustrates what you said, since what you said it does isn't working for what you want. It would hardly count against you to post code that doesn't work in fulfilling an assignment.

    2. Read the data including the control chars you don't want, and then remove anything you don't want after it's been read into memory.

    3. Read characters one by one, ignoring anything you don't want, so all the chars you don't want are never placed into memory.

    4. Any or all of these can be done by writing your own function that performs a filtered read, then your main code calls your filtered-read function instead of fread() or fgets() or whatever.
  3. mrbash macrumors 6502

    Aug 10, 2008
    I agree with Cown33's suggestions. You want to escape the control characters as they are read before storing them into your array. Or if you want to ignore them, ignore them as you read them; using a filter in this circumstance may not be helpful.
  4. robvas macrumors 68030

    Mar 29, 2009
    What functions are you using to get the input now?
  5. mac2x, Nov 6, 2010
    Last edited: Nov 6, 2010

    mac2x thread starter macrumors 65816

    Sep 19, 2009
    @robvas: I am writing my own function to do it.

    @chown33: I decided not to try to use fflush().

    I had another thought this morning...the characters I need are actually limited to A, B, C, and D. Would it be possible to compare the characters read to their decimal/octal/hex equivalent and only read in ASCII characters A-D (dec 65, 66, 67, 68)? [edit] I may have gotten this working...

    [late edit] it works great now! Thanks for the suggestion(s). :)

Share This Page