C++ Question

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:

getline(inStr, name, ’
');
getline(inStr, ID, ’ ');
getline(inStr, contact, ’
');

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.

I put ‘\r’ in for ’
’ and now I get “è@2” for the name string, and it doesnt even read anything in for the ID or contact.

Well, I guess not. Sorry.

Chaim Mattis Keller

Suggestion: open the text file in a binary editor, and see if what you think should be there is what’s ACTUALLY there.

The weirdness of the result, and the fact that it works with your own file, suggests something like cmkeller thought.

Just a thought: nothing uses wide-characters, does it?

Maybe try fscanf(). I think getline() can be funky at times (as you have noticed).

How exactly would fscanf() work in my situation ccwaterback?

I’d be tempted to write it like this:



// read company name
getline(inStr, name);

// read ID and contact
string temp;
getline(inStr, temp);
string::size_type tabpos = temp.find('	');
ID = temp.substr(0, tabpos);
contact = temp.substr(tabpos + 1);


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.

Oooops … this “should” work.

#include <stdio.h>
#include <string.h>

int main ()
{
char CoName [120];
char CoID[120], CoContact[120], CoTemp[120];
FILE * pFile;

pFile = fopen (“myfile.txt” , “r”);
if (pFile == NULL) perror (“Error opening myfile.txt”);
else {
while (fgets (CoName , sizeof(CoName) , pFile))
{
CoTemp[0] = NULL;
fgets (CoTemp, sizeof(CoTemp), pFile);
// assuming CoID is one contiguous string
sscanf (CoTemp, “%s”, &CoID);
// assuming CoContact may be more than one word
CoID[0] = NULL;
CoContact[0] = NULL;
while (sscanf(&CoTemp[strlen(CoContact) + strlen(CoID) + 1], “%s”, &CoContact[strlen(CoContact)])
strcat(CoContact, " ");
;
}
}
return 0;
}

I like hacksword C++ solution better though.

CoID[0] = NULL; should be before

sscanf (CoTemp, “%s”, &CoID);

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. :slight_smile:

Man, did I ever get spoiled with Perl:

A word can be split into characters, a sentence split into words and a paragraph split into sentences:

@chars = split(//, $word);
@words = split(/ /, $sentence);
@sentences = split(/./, $paragraph);

Same with VB. I hate languages without built-in parsing.