parseString funtion (stream I/O)

This is a discussion on parseString funtion (stream I/O) within the C++ Programming forums, part of the General Programming Boards category; In the code provided below, I have written a program (word for word) from my text book. This program reads ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    30

    Question parseString funtion (stream I/O)

    In the code provided below, I have written a program (word for word) from my text book. This program reads the text from a file named Accounts.txt and parses the string input to a different format (shown below as well).

    My only problem is that when the program executes, I receive the unexpected error "Error, parsing string" (also shown below). After I used my debugger, I stepped into the function parseString() and put a watch on all the values. The value balance was the only one not receiving the correct input (something really wacky....like 5.1233242523532e-308....should be 56.6).

    I need to know why I am getting this error.

    The text book I am currently using has a few typo's here and there, but I'm still new to C++ and do not know if I could pick any out of the displayed code.

    If anyone has any questions about the code or my question, please ask.

    thanks in advance!

    parseString funtion
    Code:
    // parseAccountInfo - read a passed buffer as if it were
    //                    an actual file - read the following
    //                    format:
    //                    name, account balance
    //                    return true if all worked well
    bool parseString(char* pString, char* pName, int arraySize,
                        long& accountNum, double& balance)
    {
        //associate an istrstream object with the input
        //character string
        istringstream inp(pString);
        
        //read up to the comma seperator
        inp.getline(pName, arraySize, ',');
        
        //now the account number
        inp >> accountNum;
        
    //balance not receiving correct value...
        //and the balance
        inp >> balance;
        
        //return the error status
        return !inp.fail();
    }

    ....now main
    Code:
    int main(int nNumberOfArgs, char* pszArgs)
    {
        //get a file stream
        ifstream* pFileStream = new ifstream("Accounts.txt");
        if(!pFileStream->good())
        {
            cout << "Can't open Accounts.txt" << endl;
            system("PAUSE");
            return 0;
        }
        
        //read a line out of file, parse it and display results
        for(;;)
        {
            //add a divider
            cout << "========================" << endl;
            // read a buffer
            char buffer[256];
            pFileStream->getline(buffer, 256);
            if(pFileStream->fail())
            {
                    break;
            }
            
            //parse the individual fields
            char name[80];
            long accountNum;
            
            double balance;
           //using debugger, I step into function below
            bool result = parseString(buffer, name , 80,
                                            accountNum, balance);
                                            
            //output the results
            //program goes into if statement below....result returned false
            cout << buffer << "\n";
            if (result == false)
            {
                    cout << "Error parsing string\n";
                    continue;
            }
            cout << "name = " << name << ","
                 << "account = " << accountNum << ", "
                 << "balance = " << balance << endl;
                 
            //put the fields back together in a different
            //order (inserting the 'ends' makes sure the
            //buffer is null terminated
            ostringstream out;
            out << name << ", "
                << balance << " "
                << accountNum << ends;
        
            //output the result - istringstream also works with
            //the string class but I have been staying with
            //character arrays until the discussion of the templates
            string oString = out.str();
            cout << oString << "\n" << endl;
        }
        system("PAUSE");
        return 0;
    }

    the text file i'm using is called "Accounts.txt" and is located in the same directory as executable.

    File format for Accounts.txt
    Code:
    Chester, 12345, 56.60,\n

    what the output statement looks like now:
    Code:
    ========================
    Chester, 12345, 56.60,\n
    Error parsing string
    ========================
    what should happen:
    Code:
    ========================
    Chester, 12345, 56.60
    name = Chester, account = 12345, balance = 56.6
    Chester, 56.6, 12345
    ========================

  2. #2
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Never mind, I got it.

    In the text file Accounts.txt, the format should have been this:
    Code:
    Chester, 12345 56.60\n
    The comma between the account number and balance should have not been there. I still do not know why though. If you want, you can explain that part to me.

    Thanks!

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by dhardin View Post
    The comma between the account number and balance should have not been there. I still do not know why though. If you want, you can explain that part to me.

    Thanks!
    Explain what? Explain why there was a comma there? I don't know. Explain why it failed?
    Code:
        //now the account number
        inp >> accountNum;
        
    //balance not receiving correct value...
        //and the balance
        inp >> balance;
    After reading the name (and comma), you reach these lines. The first use of operator>> on the stream would be fine parsing out "12345". At the second use of operator>> the stream would have contained ", 56.60,\n". Since you are attempting to read into balance, a variable of type double, and the first bit of text in that stream is a comma, the read operator fails (can't read a comma into a double) and the stream goes into a failed state.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    okay, that seems pretty self explanatory. The book did not provide these files on a cd and I was trying to duplicate a file that the book used...considering that they said thats what the file looked like...I was just confused

    thanks though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raw I/O vs. Stream I/O
    By NuNn in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 08:32 AM
  2. Stream I/O
    By NuNn in forum C Programming
    Replies: 7
    Last Post: 02-23-2009, 01:33 PM
  3. I/O stream files
    By malooch in forum C Programming
    Replies: 4
    Last Post: 12-12-2006, 08:58 AM
  4. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 04:14 PM
  5. i/o stream
    By lupi in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2001, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21