File opening

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • milindiit
    New Member
    • Mar 2008
    • 5

    File opening

    Hi.
    I am developing a small code in VC++. I have created a GUI where I want to open a file (*.doc or any other I/P or O/P file) with a button click,i.e., when a button is clicked while running specific file should get opened. If it can be achieved through Menu bar that is also OK. Somebody Please help me.
    Thank you
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Sure, what do you have so far?

    Comment

    • milindiit
      New Member
      • Mar 2008
      • 5

      #3
      Originally posted by sicarie
      Sure, what do you have so far?
      Thank you for your interest.
      I have just created MFC application, which on running produces a form. It has a menu bar "Help". Clicking on this gives default "About Dialog box". Instead of this I want to display any '.doc' or similar file when "Help" menu is clicked.
      Not a major code is developed till date but I just want to link the file. Same way i want to read/edit the files (as they are in the working directory) by a button click. What else should I give? Any sanps or code?? Please guide.
      Regards
      Milind

      Comment

      • Studlyami
        Recognized Expert Contributor
        • Sep 2007
        • 464

        #4
        I'm a little confused, Does the user get to select which file to open (an open file dialog), or does a button link to one specific file?. I find the default menu bar useless especially when creating your own menu bar is really easy to do (check out this link for info.). That tutorial also tells you how to capture when a user has clicked on a menu item. From there when you receive the message saying the user clicked on the menu item you would open the specific file (or an open file dialog to allow the user to select the file). As far as a button is concerned you would capture when the user clicked on the button and then you would perform the same operation as i described in the menu part above. If you don't know how to capture the button click then you need to read some more tutorials. I find that the website given above is very helpful and explains things quite well.

        Comment

        • milindiit
          New Member
          • Mar 2008
          • 5

          #5
          Thank you for the suggestion. I'll go through the manual you suggested and come back to you. By the way, the default FILE->OPEN dialog box actualy does nothing when user try to open any file. I want specificaly : Opening any file for editing or reading purpose by any means (Menu bar or button click). Any particular message map is there for this purpose??
          Regards

          Comment

          • Studlyami
            Recognized Expert Contributor
            • Sep 2007
            • 464

            #6
            Alright now the question comes down to "what do you mean open/ edit the file". If its a word document do you want the item to open up in word? Or, do you mean open the contents into your project allow them to edit it and then save it? The tutorial i gave you above is about menu bars seeing how that isn't your main focus this tutorial will tell you how to capture a button click and how to create an open file dialog (this allows the user to navigate to the file they want), then tells shows you how to open the file using CFile.

            Comment

            • milindiit
              New Member
              • Mar 2008
              • 5

              #7
              Originally posted by Studlyami
              Alright now the question comes down to "what do you mean open/ edit the file". If its a word document do you want the item to open up in word? Or, do you mean open the contents into your project allow them to edit it and then save it? The tutorial i gave you above is about menu bars seeing how that isn't your main focus this tutorial will tell you how to capture a button click and how to create an open file dialog (this allows the user to navigate to the file they want), then tells shows you how to open the file using CFile.
              Thanks for the reply. that solves my problem halfway.
              What if I want to open the doc file in m/s word? Also I tried opening big txt file (containing only numbers). But it could open only few lines out of it. Any suggestions for this?

              Comment

              • Studlyami
                Recognized Expert Contributor
                • Sep 2007
                • 464

                #8
                Originally posted by milindiit
                What if I want to open the doc file in m/s word?
                I really don't know how to open a document in another program, but first i would search google. Then if i still have problems i would post up a new question here in the forums.

                As far as opening a large text file could we see your code that you use to: 1. open the file 2. how you read the file 3. any other code you think is relevant.

                Comment

                • milindiit
                  New Member
                  • Mar 2008
                  • 5

                  #9
                  Originally posted by Studlyami
                  I really don't know how to open a document in another program, but first i would search google. Then if i still have problems i would post up a new question here in the forums.

                  As far as opening a large text file could we see your code that you use to: 1. open the file 2. how you read the file 3. any other code you think is relevant.
                  I am using exactly the same code as given in the tutorial.
                  [code=cpp]
                  this->UpdateData() ;

                  CFile f;
                  //char strFilter[] = { "Doc Files (*.doc)|*.doc|A ll Files (*.*)|*.*||" }; ///////This Option doesn't work
                  char strFilter[] = { "Txt Files (*.txt)|*.txt|A ll Files (*.*)|*.*||" };

                  //CFileDialog FileDlg(TRUE, ".doc", NULL, 0, strFilter);
                  CFileDialog FileDlg(TRUE, ".txt", NULL, 0, strFilter);

                  if( FileDlg.DoModal () == IDOK )
                  {
                  if( f.Open(FileDlg. GetFileName(), CFile::modeRead ) == FALSE )
                  return;
                  CArchive ar(&f, CArchive::load) ;

                  ar >>m_edit; // m_edit is CString type member variable associated with the Edit box.
                  //ar >>m_edit; ////writting this line 2nd time has a effect of displaying only the next(2nd) line of the file
                  ar.Close();
                  }
                  else
                  return;

                  f.Close();
                  this->UpdateData(FAL SE);
                  [/code]
                  For some files (typicaly text starting from the first line & first column) Run time Error message comes "An attempt was made to access an unnamed file past its end". For the other files it displays only the first 1 or 2 lines (though I have given HScroll & VScroll options to the Edit box where it is displaying the file).
                  Regards
                  Last edited by sicarie; Apr 9 '08, 01:11 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.

                  Comment

                  Working...