VC++ Express, Strings and files - CONFUSED

L

Lee J

Gang,
I am self taught in C and fairly proficient for last 20 years. Don't
program much, just little utilities for work. Been wanting to create
windows forms and found MS VC++ Express. Installed it, bought book, etc.

Book doesn't cover files. From other sources I have the following:

#pragma once
#include <string>
#include <fstream>

//in a buttonclick function

std::ifstream infile;
String^ testText;

openFileDialog1->ShowDialog(); //Search for file and put name in
textBox1->Text = openFileDialog1->FileName; // a textbox on the form and in
testText = textBox1->Text; // a text variable.

I then want to open the file listed in textBox1 and testText.

infile.open(testText);

That doesn't work. I get an error, C2664

I am totally confused by different strings, different ways of opening files,
managed, unmanaged .....

I created the project as just a CLR Windows Forms Application in VC++
Express. All I need to do is get a filename from openFileDialog into a
textbox for reading and another for writing from another textbox, read the
lines from one file, manipulate the string a bit and write it out to the
other.

Any help appreciated.

Lee
 
B

Ben Voigt [C++ MVP]

Peter Anthony said:
You might want to consider these classes instead for file manipulation:

StreamWriter class
http://msdn2.microsoft.com/en-us/library/system.io.streamwriter.aspx
StreamReader class
http://msdn2.microsoft.com/en-us/library/system.io.streamreader.aspx

They are much easier to use than the old fstream approach.

C++ iostreams are much easier for pure I/O, the difficulty is, as the OP
noted, mixing the different types of strings.

The System::IO::* classes mentioned will accept the .NET gotten from a .NET
textbox.

At some point though, you'll need to use native routines. The functions for
converting strings are PtrToStringChars, Marshal::ptrToStringAnsi,
Marshal::ptrToStringUni, and friends.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top