Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 读取多个文件,并为每个文件保留一组数据。_C++_Static - Fatal编程技术网

C++ 读取多个文件,并为每个文件保留一组数据。

C++ 读取多个文件,并为每个文件保留一组数据。,c++,static,C++,Static,我想读取一个文件并将其标题保存在一个变量中,这样当我重写(覆盖)该文件时,我就可以粘贴标题并继续打印修改后的文件的其余部分。在我的例子中,标题不会改变,所以我可以只打印出来。下面是我在类中的代码: . . . static char headerline[1024]; static int read(const char* filename){ fget(var,...; for (int i=0; i<1024; ++i){ headerline[i] = va

我想读取一个文件并将其标题保存在一个变量中,这样当我重写(覆盖)该文件时,我就可以粘贴标题并继续打印修改后的文件的其余部分。在我的例子中,标题不会改变,所以我可以只打印出来。下面是我在类中的代码:

.
.
.
static char headerline[1024];

static int read(const char* filename){
fget(var,...;
    for (int i=0; i<1024; ++i){
        headerline[i] = var[i];
    }    
.
.
.
}

int write(filename){
fprintf(filename, headerline);
//printing rest of file
.
.
.
}
。
.
.
静态字符标题行[1024];
静态整型读取(常量字符*文件名){
fget(var,。。。;

对于(int i=0;i您应该为不同的文件使用不同的头变量。

因此,需要解决的问题是,您正在读取多个文件,并且希望为每个文件保留一组数据

有很多方法可以解决这个问题。其中之一是将
文件名
标题
连接起来。正如注释中所建议的,使用
std::map
是一种方法

static std::map<std::string, std::string> headermap;



static int read(const char* filename){
static char headerline;
fget(var,...;
    for (int i=0; i<1024; ++i){
        headerline[i] = var[i];
    }   
    headermap[std::string(filename)] = std::string(headerline);

...

int write(filename){
  const char *headerline = headermap[std::string(filename)].c_str();
 fprintf(filename, headerline);   
// Note the printf is based on the above post - it's wrong, 
// but I'm not sure what your actual code does. 
static std::map headermap;
静态整型读取(常量字符*文件名){
静态字符头线;
fget(var,。。。;

因为(inti=0;这是生活中的一条普遍规律,如果有人建议用“X”来解决你的问题,而你不知道“X”是什么,你应该问那个人“X是什么?”“如果我能的话,我早就想到了。”他说:“他已经死了。”Deaveld:很抱歉你的损失,但是当他们提出答案的时候,试着问一个问题。在不理解所说的话的情况下走出一个对话是不好的。对我来说,这些词只是指一个静态数据成员(),它恰好是类型地图,即,STD::MAP()。换句话说,它们有两个不同的含义,我可以把它们并列在一个表达中,这样第一个就可以修饰第二个:“静态地图”、“加拿大建筑师”、“红苹果”。创建一个表示文件的类,然后将头存储在它所属的实例中。谢谢。因此,密钥应该是我正在读取的文件的名称?在我的情况下,文件的名称是
file*fp=…
。如何将
file*
类型的名称转换为
string
?我想您几乎可以使用任何唯一的名称(当然,您需要使用适当的类型-因为您的示例代码显示“const char*filename”,所以我使用了它。
文件*
只有在不关闭其他文件的情况下才是唯一的。您可能需要稍微重新构造代码以实现所需的功能。