Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 3rd, 2006, 04:08 AM
Authorized User
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default C++ Builder 6 - Opening a File

How to open a file (for reading) in C++ Builder using codes?

Reply With Quote
  #2 (permalink)  
Old April 5th, 2006, 03:10 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

# include the fstream header file,and create an instance of an fstream object i.e

#include <fstream>
using namespace std;

int main () {

  fstream filestr;

  filestr.open ("test.txt", fstream::in | fstream::out | fstream::app); // open a file with mode

  // >> i/o operations here <<

  filestr.close();

  return 0;
}
Reply With Quote
  #3 (permalink)  
Old April 5th, 2006, 04:05 AM
Authorized User
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the relpy.
will this work in c++ builder?

Reply With Quote
  #4 (permalink)  
Old April 5th, 2006, 11:37 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

yeah it is part of the std c++ library...you can get much more in dept explanations on line i.e msdn.com
Reply With Quote
  #5 (permalink)  
Old April 10th, 2006, 07:42 AM
Authorized User
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi thanks for the reply.
 i tried the above code, but it does not open the file!!!

Kind Regards,
Rowin

Reply With Quote
  #6 (permalink)  
Old April 10th, 2006, 08:45 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

my bad had brain fart when writing the mode

this is the format for .open function call,
your_fstream_object.open( const char* szName, int nMode, int nProt = filebuf::openprot );

You can also open a file by calling the constructor, to check out all the specifics of fstream goto www.msdn.com ; type fstream::fstream in the search

#include <fstream> //dont forget to #include header file
using namespace std;

int main () {

  fstream filestr;

  // open somefile with append mode and default protection
  // Note if file does not reside in same file as sorce code you must specify the full path and name
 filestr.open("somefile.txt", ios::app);

  // >> i/o operations here <<

  filestr.close();

  return 0;
}
Reply With Quote
  #7 (permalink)  
Old April 11th, 2006, 04:13 AM
Authorized User
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
the .open does not accept 3 parameters??

What i/o operations are you referring to?

What I need is that when I choose a file from an FileListBox and then click on a button, the file is opened and I can read what is written in it.

Reply With Quote
  #8 (permalink)  
Old April 11th, 2006, 07:12 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

post some of your code
Reply With Quote
  #9 (permalink)  
Old April 13th, 2006, 06:06 AM
Authorized User
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
I managed to do it.

I used:
HINSTANCE hInst = ShellExecute(0,"open",Filename, 0,0,SW_SHOW);

Kind Regards,

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening file 80240210 Flash (all versions) 0 July 24th, 2007 06:08 PM
Opening a file scoobie Pro Java 1 August 10th, 2006 02:30 PM
file opening jain_mj Visual C++ 0 November 4th, 2005 10:42 AM
file opening silver_cuts Classic ASP Basics 3 June 29th, 2004 01:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.