CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    Join Date
    Oct 2008
    Posts
    59

    Question Problem on using VC++ 2008

    I have worked on VC++6.0,
    Now first time, am on VC++.Net 2008 platform....

    Here I want to Save some datas of a Listbox to a file... So I have tried the following code

    CFileDialog FileDialog(FALSE, "txt","downloader", OFN_CREATEPROMPT|OFN_HIDEREADONLY |
    OFN_SHOWHELP|OFN_OVERWRITEPROMPT|OFN_LONGNAMES, "Save the file as (*.txt)|*.txt||");
    if (FileDialog.DoModal() == IDCANCEL)
    return;

    char sFileName[255];
    BeginWaitCursor();
    UpdateData(true);

    strcpy(sFileName, FileDialog.GetFileName());

    sourceFile.open(sFileName,ios:ut);
    if(!sourceFile.is_open())
    AfxMessageBox("file for saving not opened");
    else
    for (POSITION Pos1 = pFrame->Batch_boardList.GetHeadPosition(); Pos1 != NULL
    sourceFile <<pFrame->Batch_boardList.GetNext(Pos1)+'\n';

    sourceFile.close();

    This was the code I have used in VC++ 6.0,
    On the same routine, I've tried it on 2008... But the CFileDialog class itself creates some errors.. n solved.(without error)

    Here I can't copy "strcpy(sFileName, FileDialog.GetFileName());"
    The error noted as The Parameter 2 cannot convert from CString to char*..

    Plz explain how it is so?? and how can I solve it??

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Problem on using VC++ 2008

    Probably a UNICODE - MCBS issue. Try:

    Code:
      CFileDialog FileDialog(FALSE, "txt","downloader", OFN_CREATEPROMPT|OFN_HIDEREADONLY |
        OFN_SHOWHELP|OFN_OVERWRITEPROMPT|OFN_LONGNAMES, "Save the file as (*.txt)|*.txt||");
      if (FileDialog.DoModal() == IDCANCEL)
        return;
    
      _TCHAR sFileName[255];
      BeginWaitCursor();
      UpdateData(true);
    
      _tcscpy(sFileName, FileDialog.GetFileName());
    
      sourceFile.open(sFileName,ios:ut);
      if(!sourceFile.is_open())
        AfxMessageBox("file for saving not opened");
      else
        for (POSITION Pos1 = pFrame->Batch_boardList.GetHeadPosition(); Pos1 != NULL
          sourceFile <<pFrame->Batch_boardList.GetNext(Pos1)+'\n';
    
      sourceFile.close();

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem on using VC++ 2008

    I'd do some more changes:
    Code:
      CFileDialog FileDialog(FALSE, _T("txt"), _T("downloader"), OFN_CREATEPROMPT|OFN_HIDEREADONLY |
        OFN_SHOWHELP|OFN_OVERWRITEPROMPT|OFN_LONGNAMES, _T("Save the file as (*.txt)|*.txt||)");
      if (FileDialog.DoModal() == IDCANCEL)
        return;
    
      _TCHAR sFileName[MAX_PATH]; // 260, not 255 
      BeginWaitCursor();
      UpdateData(true);
    
      _tcscpy(sFileName, FileDialog.GetFileName());
    
      sourceFile.open(sFileName, ios:ut);
      if(!sourceFile.is_open())
        AfxMessageBox( _T("file for saving not opened"));
      else
        for (POSITION Pos1 = pFrame->Batch_boardList.GetHeadPosition(); Pos1 != NULL
          sourceFile << pFrame->Batch_boardList.GetNext(Pos1) + _T('\n');
    
      sourceFile.close();
    Victor Nijegorodov

  4. #4
    Join Date
    Oct 2008
    Posts
    59

    Re: Problem on using VC++ 2008

    Thank u victor & Hox...

    I've cleared the problem.... n modified it like this...

    #include<ifstream>;
    using namespace std;


    ifstream sourcefile;
    CFileDialog FileDlg(FALSE, _T("txt"),_T("ParameterFile"), OFN_CREATEPROMPT|OFN_HIDEREADONLY |
    OFN_SHOWHELP|OFN_OVERWRITEPROMPT|OFN_LONGNAMES, _T("Save the file as(*.txt)|*.txt||"),NULL,0,TRUE);

    if (FileDlg.DoModal() == IDCANCEL)
    return;
    _TCHAR sFileName[255];

    BeginWaitCursor();
    UpdateData(true);

    _tcscpy(sFileName, FileDlg.GetFileName());
    sourcefile.open(sFileName,ios:ut);

    if(!sourcefile.is_open())
    AfxMessageBox(_T("file for saving not opened"));
    else
    for (POSITION Pos1 = parList.GetHeadPosition(); Pos1 != NULL
    sourcefile<< parList.GetNext(Pos1)+'\n';

    sourcefile.close();

    Now the file is created.... but when it is opening, the data in the listbox is not there... Some unknown Hexadecimal values are seeing there...

    How to resolve it??

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Problem on using VC++ 2008

    1) Does GetNext() return a CString ? Maybe the streams do not understand how
    to output a CString and it is using the void* conversion (basically, printing
    out the address of the object). Try casting to LPCTSTR

    Code:
    sourcefile<< (LPCTSTR)parList.GetNext(Pos1)<< '\n';

    2) Depending an compiler options, you might need to change to
    wifstream (also why istream types if outputing ?).


    3) If GetNext() does return a CString, it would make more sense to use the
    CStdioFile class which can directly read/write Cstring objects.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem on using VC++ 2008

    Quote Originally Posted by arunkr6 View Post
    ... Now the file is created.... but when it is opening, the data in the listbox is not there... Some unknown Hexadecimal values are seeing there...

    How to resolve it??
    What listbox do you mean? I don't see any...
    Victor Nijegorodov

  7. #7
    Join Date
    Oct 2008
    Posts
    59

    Unhappy Re: Problem on using VC++ 2008

    Sorry Victor, for not providing sufficient data...

    I've entered the datas of Listbox to a CStringList parList.

    GetNext() returns CString itself... I've verified and it gives the exact data in particular CStringList. I hope the prblm is in the line 'sourcefile<<Parlist.Get....... Here the data is not loading to fstream sourcefile.


    I've tried wifstream and ifstream.It gives an error as binary '<<' no operator found which takes a left hand operand of type std::wifstream.. So I've used fstream and it's working...

    The problem still exists.....As the data written is something hexvalue(hope the address)....

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Problem on using VC++ 2008

    Did you try the cast as I posted earlier ?

  9. #9
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Problem on using VC++ 2008

    Is parList an CAtlList? How is it defined (what is the type parameter)?

  10. #10
    Join Date
    Oct 2008
    Posts
    59

    Re: Problem on using VC++ 2008

    Ya... I've casted it... Bt it doesn't resemble any change..same effect..

    parList is assigned as CStringList only..

    Can you plz give me a code to save and load the datas(user entering in fields and it combines to a ListBox) to a file....This is my requirement.. By using the above code I've done it in VC++ 6.0... Bt here it failed,,.!!

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Problem on using VC++ 2008

    Quote Originally Posted by arunkr6 View Post
    Ya... I've casted it... Bt it doesn't resemble any change..same effect..

    parList is assigned as CStringList only..

    Can you plz give me a code to save and load the datas(user entering in fields and it combines to a ListBox) to a file....This is my requirement.. By using the above code I've done it in VC++ 6.0... Bt here it failed,,.!!
    Why don't you just post your complete code and we'll suggest how to fix your code?

  12. #12
    Join Date
    Oct 2008
    Posts
    59

    Re: Problem on using VC++ 2008

    the exact code has pasted already..plz check above...

    That's the code I've written for this function(Save to a file).

  13. #13
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Problem on using VC++ 2008

    Quote Originally Posted by arunkr6 View Post
    the exact code has pasted already..plz check above...

    That's the code I've written for this function(Save to a file).
    Use code tags & more people will read it.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem on using VC++ 2008

    Quote Originally Posted by arunkr6 View Post
    the exact code has pasted already..plz check above...
    The code has smiley faces and is unformatted. Why not fix it the formatting first by using code tags?

    Regards,

    Paul McKenzie

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Problem on using VC++ 2008

    Quote Originally Posted by arunkr6 View Post
    the exact code has pasted already..plz check above...

    That's the code I've written for this function(Save to a file).
    You talk about the data not getting into a listbox, but there isn't any listbox code. How about posting the complete code?

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured