I have a text file that (partial snippet) looks like the following:
Bugs Bunny Jr. 1234 1001.01
Porky Pig 2345 1002.02
Its format is a name (including spaces) is 20 characters, then 4 digit number, a space, and a number thats not a set number of characters.
I have to read the name, first set of numbers (pin), and last numbers (balance) into a linked list struct. The trouble I am running into is the name portion. I made name a char array of 20 chars, but when it reads in it skips the whitespaces and reads the numbers as well as the name. Is there a way to get the input operator to count spaces as a character so it will not skip them? Also someone mentioned I could use a cstring instead of a char array but didn't elaborate, so didn't help me at all. Any help would be appreciated!
I am using DEV C++ on winxp (if that makes any difference).
Here is what I have
ifstream fin;
struct customerType{
char name[20];
int pin;
float balance;
customerType *link;
};
int main(){
customerType *head, *curptr, *nnptr;
fin.open("input.txt");
if(!fin){
cout << "File not found\n";
system("pause");
return 0;
}
head = new customerType;
for(int i=0; i<20; i++)
{
fin >> head->name[i];
}//END FOR
fin >> head->pin >> head->balance;
curptr = head;
while(fin)
{
nnptr = new customerType;
for(int i=0; i<20; i++)
{
fin >> nnptr->name[i];
} // END FOR
fin >> nnptr->pin >> nnptr->balance;
curptr->link = nnptr;
curptr = nnptr;
if(fin.peek()=='\n')fin.ignore();
} //END WHILE
curptr->link = NULL;
curptr = NULL;
nnptr = NULL;
}//END MAIN