I’ve been having this problem for well over an hour and I thought it was about time to ask the opinion of the masses. I’m tring to read in a file that looks like this:
Company Name, Inc
ID contact
Company Name, Inc
ID contact
There are returns after each line in the text file and the space in between the ID and contact is a tab. Each item needs to be read into a name, ID, and contact variable respectively. The code I have for this is:
So, it gets the entire first line and stores it as the name string, it gets the ID as a string and ends at the tab. And finally it gets the rest of the line after the tab and stores it as the contact string. For some reason, however, I get “(?2” for name, I get the correct ID for ID, and then I get “^?2)” for the contact. I’ve tried making up my own file and I got it to work with the following text file:
aaaaa aaaaa
bbbbb cccccc
It will read the line of a’s in as the name, the b’s as the ID, and the c’s as the contact information. When I go back to using the file from above I get the ?2 things. Does anyone know why this is happening??? Thanks in advance!
A new-line in a file is not always a single character, it is, on many systems, it’s the two-character combination of Char(10) and Char(13). That, I’d wager, is what’s messing you up. Your system probably has the single-character (13), but perhaps the system on which the input file was created renders a new-line as the two characters above.
Try using ‘\r’ instead of ’
'. If my guess is right, that should be the solution.
It might be extra characters that are used in MS Word or WordPad files if you are using a Windows machine to create the text file. Try opening it in Notepad and saving it again. Or if you have a Unix/Linux machine handy, open it in vi or something to see if there are extra characters.
Ok…so I think it had something to do with staring at the same code over and over. I had my neighbor look at the same 3 lines and step through them while watching the local variables. Turns out while it was reading in the data correctly it was displaying the local variables differently for some strange reason. But, you’re posts were not wasted. I did learn a little bit from each and at some point in my programming career they will come in handy. Thanks again for all the help!
Ahh, another good lesson learned. Sometimes I would call in a co-worker just to listen to me walk-through my code. For some unknown reason, bugs just jumped out at me when I did that.