Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Reading file from OpenFileDialog.

Reading file from OpenFileDialog.

Scheduled Pinned Locked Moved Managed C++/CLI
questionc++wpfwcfoop
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Link2600
    wrote on last edited by
    #1

    private: System::Void menuOpenFile_Click(System::Object * sender, System::EventArgs * e) { StreamReader* InputStream; OpenFileDialog* openFileDialog1 = new OpenFileDialog(); openFileDialog1->InitialDirectory = S"c:\\" ; openFileDialog1->Filter = S"txt files (*.txt)|*.txt|All files (*.*)|*.*" ; openFileDialog1->FilterIndex = 2 ; openFileDialog1->RestoreDirectory = true ; if(openFileDialog1->ShowDialog() == DialogResult::OK) { if((InputStream = openFileDialog1->OpenFile())!= 0) { // Insert code to read the stream here. txtBoxSource->Clear(); String* lineOfText = InputStream->ReadLine(); while(lineOfText != NULL) { txtBoxSource->Text = String::Concat(txtBoxSource->Text, lineOfText, S"\r\n"); lineOfText = InputStream->ReadLine(); } InputStream->Close(); } } } This program invoke a open file dialog. Now when I select a file in the open file dialog, how do I read it? I used StreadReader, but I got an error saying that it can't convert from System::IO.Stream to System::IO.StreamReader. I want to read the file and put it to the text box named txtBoxSource. Thanks ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".

    A 1 Reply Last reply
    0
    • L Link2600

      private: System::Void menuOpenFile_Click(System::Object * sender, System::EventArgs * e) { StreamReader* InputStream; OpenFileDialog* openFileDialog1 = new OpenFileDialog(); openFileDialog1->InitialDirectory = S"c:\\" ; openFileDialog1->Filter = S"txt files (*.txt)|*.txt|All files (*.*)|*.*" ; openFileDialog1->FilterIndex = 2 ; openFileDialog1->RestoreDirectory = true ; if(openFileDialog1->ShowDialog() == DialogResult::OK) { if((InputStream = openFileDialog1->OpenFile())!= 0) { // Insert code to read the stream here. txtBoxSource->Clear(); String* lineOfText = InputStream->ReadLine(); while(lineOfText != NULL) { txtBoxSource->Text = String::Concat(txtBoxSource->Text, lineOfText, S"\r\n"); lineOfText = InputStream->ReadLine(); } InputStream->Close(); } } } This program invoke a open file dialog. Now when I select a file in the open file dialog, how do I read it? I used StreadReader, but I got an error saying that it can't convert from System::IO.Stream to System::IO.StreamReader. I want to read the file and put it to the text box named txtBoxSource. Thanks ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".

      A Offline
      A Offline
      AnsGe
      wrote on last edited by
      #2

      Hai Alex, you can solve the problem with this help of FileStram class FileStream* fs; StreamReader* InputStream; OpenFileDialog* openFileDialog1 = new OpenFileDialog(); if(openFileDialog1->ShowDialog() == DialogResult::OK) { fs=new FileStream(openFileDialog->FileName,FileMode::Open); InputStream=new StreamReder(fs); //Then done your reading using InputStream } NB: please include using namespace System::IO;

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups