in vb net i use this code: My.Computer.FileSystem.ReadAllText("C:\test.txt")
How about in C++? thanks!
Printable View
in vb net i use this code: My.Computer.FileSystem.ReadAllText("C:\test.txt")
How about in C++? thanks!
Google is faster than opening a topic.
Hi,
Look at the example below and make your own file reading.
I hope this help!
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
It's worth noting that the below is *exactly* equivalent to the above in all respects:
There's never a need for an explicit close() if the ofstream object is going out of scope, and likewise you can open the file as soon as you construct the object.Code:// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream myfile("example.txt");
myfile << "Writing this to a file.\n";
return 0;
}
ifstreams can be used in much the same way for input.
Many people still use this, [I use this too]
if they happen to know not about fstreamCode:FILE*f=fopen("filename.txt");
using FILE* is so ok.
fstream ( ifstream, ofstream ) is nice too.
however if you just work with text let using CStidioFile ( but it's only for MFC )
Yes, I agree, I would look also at CFile if you work with MFC, it provides multiple to deal with files
I just rewatched some articles, and thinking about using Readfile(fHan,xxxxxx). Hope this could help you as some extracted fluid for the program. :)
I prefer C#'s File class or FileInfo as well as Directory or DirectoryInfo for files and directory tasks. You can write one byte, or the whole content to a particular file. I think I just made a little test yesterday about the speed of fsteam for reading content line by line of a file and discovered that it worked worse than C#'s ReadLine function. fstream also fails on several cases if the files was encoded differently. About MFC, it has the reputation of slow interface design and speed as well as Standard compatiblity problem.
In general, you should not expect standard IO mechanisms to understand non-ASCII encodings. There are some exceptions, and some partial exceptions: getline() will do the right thing on a UTF8 file, for instance, is the \n character is the same in UTF8 as it is in ASCII.
Either do the decoding yourself, or get a library specifically designed for the task to do so.