2008-08-11 81 views
19

我想打开一个文件阅读,C++的方式。我需要能够做到这一点:如何用C++打开文件?

  • 文本文件,这将涉及某种读线功能。

  • 二进制文件,这将提供一种方法来读取原始数据到一个char*缓冲区。

+1

[C++:Input/Output with files](http://www.cplusplus.com/doc/tutorial/files.html) – mreggen 2008-08-11 15:53:34

+0

任何对RAII感兴趣的人都应该查看[The Official Resource Management (http://www.relisoft.com/resource/resmain.html)Bartosz Milewski。 – titanae 2008-09-08 12:43:12

回答

8

有三种方法可以做到这一点,这取决于您的需求。你可以使用老派的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实现,稍有不同,但完成同样的事情)。

+6

难道你不会在阅读中遇到段错误吗?您没有为数据分配任何空间。应该是`char somedata [10]`,对吧? – swdev 2013-09-16 08:30:38

24

如果您只想阅读(使用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()函数(它是全局的)来读取整行。

+0

+1注意全局getline()函数将被用来代替成员函数。 – foraidt 2009-08-14 16:57:02

-2

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作品可以为您节省一些头痛和一些主要的内存管理错误。

0
#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...... 
} 
2

打开和读取每行一个文本文件中的行,你可以使用以下命令:

// 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>