Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
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.

Thanks!
 

chown33

Moderator
Staff member
Aug 9, 2009
10,739
8,415
A sea of green
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.
 

mrbash

macrumors 6502
Aug 10, 2008
251
1
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.
 

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
@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). :)
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.