Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 5, 2010, 03:23 PM   #1
mac2x
macrumors 65816
 
Join Date: Sep 2009
Question about reading into a character array (C)

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!
__________________
Retina MBP | 2.6 GHz i7 | 16 GB RAM

Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE
mac2x is offline   0 Reply With Quote
Old Nov 5, 2010, 03:40 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
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.
chown33 is offline   0 Reply With Quote
Old Nov 5, 2010, 04:39 PM   #3
mrbash
macrumors 6502
 
Join Date: Aug 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.
mrbash is offline   0 Reply With Quote
Old Nov 5, 2010, 06:58 PM   #4
robvas
macrumors 68000
 
Join Date: Mar 2009
Location: USA
Quote:
Originally Posted by chown33 View Post
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.
What functions are you using to get the input now?
robvas is offline   0 Reply With Quote
Old Nov 6, 2010, 12:40 PM   #5
mac2x
Thread Starter
macrumors 65816
 
Join Date: Sep 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).
__________________
Retina MBP | 2.6 GHz i7 | 16 GB RAM

Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE

Last edited by mac2x; Nov 6, 2010 at 01:50 PM.
mac2x is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
UITableView / array question larswik iPhone/iPad Programming 4 Nov 10, 2012 01:41 AM
Resolved: Array Sorting Question larswik iPhone/iPad Programming 2 Sep 3, 2012 09:56 PM
Discover people's character with Face Reading Booth on iOS BombApp iPhone and iPod touch Apps 0 Aug 8, 2012 07:03 AM
Reading from Text File into an Array in PHP Kilamite Web Design and Development 1 Aug 7, 2012 06:30 AM
Sort Array Question larswik iPhone/iPad Programming 11 Aug 4, 2012 07:33 PM

Forum Jump

All times are GMT -5. The time now is 01:53 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC