Thread: Format output

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Malaysia
    Posts
    78
    Rep Power
    12

    Question Format output


    I try to format my output in the list box control, but the run-time error occur. following is my codeBOOL SuppList::OnInitDialog()
    {
    CDialog::OnInitDialog();
    char buf[1024];

    ifstream myFile("Supplier.txt",ios::in);

    while(!myFile.eof())
    {
    myFile.getline(buf,1024); //get the address of buffer
    CString* p = NULL;
    p = new CString(buf);
    char aa[5];
    char bb[50];
    sprintf(aa, "%s", p->Mid(0,5)); //get the first 5 characters
    sprintf(bb, "%s", p->Mid(6,7)); // try to get the following characters, but run-time error occur
    m_SuppInfo.AddString(buf);
    m_SuppInfo.AddString(aa); //display the first 5 characters
    m_SuppInfo.AddString(bb); //display the following char, but run-time error occur
    }

    // TODO: Add extra initialization here

    return TRUE; // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
    }

    Data that i store in the data file
    A1234Terence

    Desire output
    A1234 Terence

    I hope somebody can help me
    Thank
    Programming is interesting
  2. #2
  3. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    I haven't analyzed the entire code, but just skimming by it quickly it appears that you are storing 5 characters into aa, and it can only hold 5 characters. Shouldn't it have a 6th character for the \0 termination? Otherwise, how do the functions know where the string ends?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Malaysia
    Posts
    78
    Rep Power
    12
    Is it possible 4 me 2 send u the source code and u debug 4 me?
    Programming is interesting
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Malaysia
    Posts
    78
    Rep Power
    12
    Using this code, i am able to display the info oledi!!!

    BOOL SuppList::OnInitDialog()
    {
    CDialog::OnInitDialog();
    char buf[1024];
    char *pch = NULL;
    ifstream myFile("Supplier.txt",ios::in);

    while(!myFile.eof())
    {
    myFile.getline(buf,1024);
    if ( isalnum(buf[0]) )
    {
    while ( (pch = strchr(buf, '|')) )
    pch[0] =' ';
    m_SuppInfo.AddString(buf);
    }
    }
    // TODO: Add extra initialization here

    return TRUE; // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
    }

    But the result the i get is in this format:
    A1111 Terence Address1
    A1234 AS ***

    But,
    How can i display the result with the format below??
    A111 Terence Address
    A1234 AS ***

    I hope that someone may help .
    Programming is interesting
  8. #5
  9. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Originally posted by thcc
    Is it possible 4 me 2 send u the source code and u debug 4 me?
    No. Please read my reply one more time, and check your code to see if this is the problem. I am fairly sure that it is, since I am pretty sure %s requires a NULL terminated string. Check to make sure that this is what you are passing to it. If you cannot do this because you don't know enough about the functions you are using, then you should use MSDN to read up on them. If you don't know enough about the language to do this, then you should learn more about it from a tutorial. If I were to debug the code for you, all that does is waste my time, and you'll learn nothing. This does not benefit you in the long run.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Malaysia
    Posts
    78
    Rep Power
    12
    this is the NULL terminated string that i have declared

    CString* p = NULL;
    Programming is interesting
  12. #7
  13. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    That statement declares a pointer that points to NULL, meaning it doesn't point to anything. The NEXT statement makes it point to a NULL terminated string. In any case, this has nothing to do with your problem.

    The aa array is too small to hold 5 characters plus a NULL termination character. You need one extra character for the NULL. So, make it hold 6 characters if you wish to store a string of 5 into it. This is what I stated in my frist reply. Have you tried this fix, yet? If not, please try it. I also think you need to find a C++ tutorial to learn more about the language. I can recommend a good book if you are interested.
    Last edited by Jason Doucette; August 7th, 2003 at 09:02 AM.

IMN logo majestic logo threadwatch logo seochat tools logo