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;
         cout << "File not found\n";
         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;
         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;
    } //END WHILE
    curptr->link = NULL;
    curptr = NULL;
    nnptr = NULL;

Another thought, is there a way to limit a string input to 20 characters? ex, replace the name char array with string name and have fin>> head->name stop at 20 characters?

You could probably write your own >> operator, but if you use the regular >> operator, it's going to use white space as a delimiter. getline may be useful. Depending which getline you use, you can it to read up to 20 characters or up to a specified delimiter.

http://www.cplusplus.com/reference/iostream/istream/getline/ strtok could come in useful here too.


You can use get and read a character at a time and use isalpha , isspace , and isdigit from the cctype library.


Here's an example using strtok to peel off everything in a C-Style string up to the first digit.

#include <cstring>
#include <iostream>
using namespace std;

int main ()
    char aString[20] = "abc def ghi  54";
    char* letters = strtok (aString, "0123456789");
    cout << letters << endl;
    cin.get (); 
    return 0;

>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.
Probably it's a wrong line format description (judging by the file contents example the name field has not fixed width). However if it's true, you may use read member function to get a fixed number of chars from the stream (change the target as you wish):

char name[21];
name[20] = '\0'; // terminate a string in C style.

Take into account that now customerType::name array can contain 19-char strings only + terminating zero byte. If you want to process names as strings in C style you must reserve at least 21 char for name member array.