C++怎么把一个字符串作为文件名

比如我有多个储存链表的dat文件,我用字符串读入其中一个文件的名称,然后我要打开并对这个文件里的信息进行操作,怎么做?

 你这个属于二进制文件,大概的代码如下,具体要看你的dat的定义和链表的定义

    string fn = "xxx.dat"; //字符串文件名
        char * filename = fn.c_str();
    FILE *fid;  
    fid = fopen(filename,"rb");  
    if(fid == NULL)  
    {  
        printf("读取文件出错");  
        return;  
    }  
    int mode = 1;  
    printf("mode为1,知道pos有多少个;mode为2,不知道pos有多少个\n");  
    scanf("%d",&mode);  
    if(1 == mode)  
    {  
        double pos[200];  
        fread(pos,sizeof(double),200,fid);  
        for(int i = 0; i < 200; i++)  
            printf("%lf\n", pos[i]);  
        free(pos);  
    }  
    else if(2 == mode)  
    {  
        //获取文件大小  
        fseek (fid , 0 , SEEK_END);         
        long lSize = ftell (fid);    
        rewind (fid);   
        //开辟存储空间  
        int num = lSize/sizeof(double);  
        double *pos = (double*) malloc (sizeof(double)*num);    
        if (pos == NULL)    
        {    
            printf("开辟空间出错");     
            return;   
        }   
        fread(pos,sizeof(double),num,fid);  
        for(int i = 0; i < num; i++)  
            printf("%lf\n", pos[i]);  
        free(pos);     //释放内存  
    }  
    fclose(fid);  

打开文件用fopen,或者楼上的ifstream.open都可以啊
楼主遇到了什么麻烦事?

// writing on a text file

#include

int main () {

ofstream out("out.txt");

if (out.is_open())

{

out << "This is a line.\n";

out << "This is another line.\n";

out.close();

}

return 0;

}

// reading a text file

#include

#include

#include

int main () {

char buffer[256];

ifstream in("test.txt");

if (! in.is_open())

{ cout << "Error opening file"; exit (1); }

while (!in.eof() )

{

in.getline (buffer,100);

cout << buffer << endl;

}

return 0;

}

//结果 在屏幕上输出

This is a line.

This is another line

楼上给了C语言的,我给个C++的读写