|
-
November 6th, 2008, 09:18 AM
#1
How to open a file in C++?
in vb net i use this code: My.Computer.FileSystem.ReadAllText("C:\test.txt")
How about in C++? thanks!
-
November 6th, 2008, 09:57 AM
#2
Re: How to open a file in C++?
Google is faster than opening a topic.
-
November 6th, 2008, 10:00 AM
#3
Re: How to open a file in C++?
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;
}
-
November 6th, 2008, 10:05 AM
#4
Re: How to open a file in C++?
It's worth noting that the below is *exactly* equivalent to the above in all respects:
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;
}
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.
ifstreams can be used in much the same way for input.
-
November 6th, 2008, 09:18 PM
#5
Re: How to open a file in C++?
Many people still use this, [I use this too]
Code:
FILE*f=fopen("filename.txt");
if they happen to know not about fstream
-
November 6th, 2008, 09:36 PM
#6
Re: How to open a file in C++?
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 )
My English is very bad. So tell me if Something I talked make u confuse.
My Ebook Store: www.coding.vn/book.php
-
November 6th, 2008, 09:39 PM
#7
Re: How to open a file in C++?
Yes, I agree, I would look also at CFile if you work with MFC, it provides multiple to deal with files
-
November 8th, 2008, 04:17 AM
#8
Re: How to open a file in C++?
I just rewatched some articles, and thinking about using Readfile(fHan,xxxxxx). Hope this could help you as some extracted fluid for the program.
-
November 8th, 2008, 04:51 AM
#9
Re: How to open a file in C++?
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.
hi,,,
-
November 8th, 2008, 10:58 AM
#10
Re: How to open a file in C++?
 Originally Posted by Khiem
fstream also fails on several cases if the files was encoded differently.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|