回答
有三种方法可以做到这一点,这取决于您的需求。你可以使用老派的C方法并调用fopen/fread/fclose,或者你可以使用C++ fstream工具(ifstream/ofstream),或者如果你使用的是MFC,使用CFile类,它提供函数来实现实际文件操作。
所有这些都适用于文本和二进制,但没有一个具有特定的readline功能。什么你最有可能做的,而不是在这种情况下是使用的fstream类(fstream.h),并使用流运营商(< <和>>)或读函数读取文本/写块:
int nsize = 10;
char *somedata;
ifstream myfile;
myfile.open("<path to file>");
myfile.read(somedata,nsize);
myfile.close();
请注意,如果您使用的是Visual Studio 2005或更高版本,传统的fstream可能不可用(这里有一个新的Microsoft实现,稍有不同,但完成同样的事情)。
难道你不会在阅读中遇到段错误吗?您没有为数据分配任何空间。应该是`char somedata [10]`,对吧? – swdev 2013-09-16 08:30:38
如果您只想阅读(使用ofstream
书写,或者使用fstream
),则需要使用ifstream
。
要以文本模式打开一个文件,请执行下列操作:
ifstream in("filename.ext", ios_base::in); // the in flag is optional
要以二进制方式打开一个文件,你只需要添加的“二进制”标志。
ifstream in2("filename2.ext", ios_base::in | ios_base::binary);
使用ifstream.read()
函数读取字符块(二进制或文本模式)。使用getline()
函数(它是全局的)来读取整行。
+1注意全局getline()函数将被用来代替成员函数。 – foraidt 2009-08-14 16:57:02
fstream是伟大的,但我会深入一点,并告诉你有关RAII。
一个典型例子的问题是您不得不自己关闭文件,这意味着您将不得不将结构屈服于此需求。 RAII利用C++中的自动析构函数调用为你关闭文件。
更新:似乎std :: fstream已经实现了RAII,所以下面的代码是没用的。我将把它留在这里作为后代,并作为RAII的一个例子。
class FileOpener
{
public:
FileOpener(std::fstream& file, const char* fileName): m_file(file)
{
m_file.open(fileName);
}
~FileOpeneer()
{
file.close();
}
private:
std::fstream& m_file;
};
现在你可以使用这个类在你的代码是这样的:
int nsize = 10;
char *somedata;
ifstream myfile;
FileOpener opener(myfile, "<path to file>");
myfile.read(somedata,nsize);
// myfile is closed automatically when opener destructor is called
学习如何RAII作品可以为您节省一些头痛和一些主要的内存管理错误。
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
ifstream in_stream; // fstream command to initiate "in_stream" as a command.
char filename[31]; // variable for "filename".
cout << "Enter file name to open :: "; // asks user for input for "filename".
cin.getline(filename, 30); // this gets the line from input for "filename".
in_stream.open(filename); // this in_stream (fstream) the "filename" to open.
if (in_stream.fail())
{
cout << "Could not open file to read.""\n"; // if the open file fails.
return;
}
//.....the rest of the text goes beneath......
}
打开和读取每行一个文本文件中的行,你可以使用以下命令:
// define your file name
string file_name = "data.txt";
// attach an input stream to the wanted file
ifstream input_stream(file_name);
// check stream status
if (!input_stream) cerr << "Can't open input file!";
// file contents
vector<string> text;
// one line
string line;
// extract all the text from the input file
while (getline(input_stream, line)) {
// store each line in the vector
text.push_back(line);
}
要打开和阅读你需要显式声明的阅读格式的输入流中的二进制文件是二进制的,和阅读,使用流成员函数read()
没有明确的解释记忆:
// define your file name
string file_name = "binary_data.bin";
// attach an input stream to the wanted file
ifstream input_stream(file_name, ios::binary);
// check stream status
if (!input_stream) cerr << "Can't open input file!";
// use function that explicitly specifies the amount of block memory read
int memory_size = 10;
// allocate 10 bytes of memory on heap
char* dynamic_buffer = new char[memory_size];
// read 10 bytes and store in dynamic_buffer
file_name.read(dynamic_buffer, memory_size);
执行此操作时,您需要#include
头文件:<iostream>
- 1. 如何使用_TCHAR *作为文件名打开文件? C/C++
- 2. 用C#打开.sqlite文件#
- 3. 用C打开文件#
- 4. 打开.dat文件用C
- 5. 打开文件C++
- 6. C#打开文件
- 7. 如何使用打开文件对话框打开文件
- 8. C#打开文本文件
- 9. 如何在打开新文件时打开文件(打开文件)?
- 10. 如何用NSFileHandle打开文本文件
- 11. C - 打开无效文件
- 12. 无法打开文件c#
- 13. 文件打开内存c#
- 14. C++打开文件流
- 15. 从C#打开Excel文件#
- 16. C多次打开文件
- 17. C文件打开错误
- 18. 无法打开文件C
- 19. 打开文件C++错误
- 20. 双击打开文件c#
- 21. 在c中打开文件#
- 22. C#打开SQLite文件
- 23. 如何打开.FIC文件?
- 24. 如何打开aspx.cs文件?
- 25. 如何打开VCE文件?
- 26. 如何打开.hprof文件
- 27. 如何打开.o文件?
- 28. 如何打开“nul”文件?
- 29. 如何打开apk文件
- 30. 如何打开.SQLite文件
[C++:Input/Output with files](http://www.cplusplus.com/doc/tutorial/files.html) – mreggen 2008-08-11 15:53:34
任何对RAII感兴趣的人都应该查看[The Official Resource Management (http://www.relisoft.com/resource/resmain.html)Bartosz Milewski。 – titanae 2008-09-08 12:43:12