如何在C ++中读取文件并维护文件指针 [英] How to read file and maintain file pointer in c++

查看:85
本文介绍了如何在C ++中读取文件并维护文件指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的txt文件

i have a txt file like this

name=abc
age=25
language=english
//blank line
name=def
age=55
language=hindi
//blank line



我在这样的课程中有一个结构



i have a structure in a class like this

class Demo
{
public:
struct DATA
{
char* name;
char* Lang;
int age;
};
};



读取文件,我使用以下代码
filestr是一个文件流指针,str的类型为字符串



to read the file i use the following code
filestr is a filestream pointer and str is of type string

while(!filestr.eof())
  {
      getline(filestr,str);

      cout<<str<<endl;
  }



我想做的是:-
从文件中读取名称值到结构文件名中,将结构字段年龄中的年龄值和lang字段中的lang值传递给函数,然后再次读取结构中的值并传递结构.

如果它的空白行留了它.您能帮我怎么做吗



What i want to do is:-
in read name value from file into structure filed name,age value in structure field age and lang value in lang field then pass the structure to the function and then again read the values in structure and pass the structure .

if its blank line leave it.Can you please help me to how to do this

推荐答案

// demo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <string>
#include <iostream>
bool ParseLine(const std::string& line, std::string& key, std::string& value);

class Demo
{
public:
    typedef struct _Data 
    {
        std::string name;
        std::string email;
    } Data;

    Data data;
};

int main()
{
    std::ifstream ifs("d:\\demo.txt");
    std::string line;
    std::string key;
    std::string value;
    Demo obj;
    while(!ifs.eof())
    {
        std::getline(ifs, line);
        if (ParseLine(line, key, value))
        {
            if (0 == key.compare("name"))
            {
                obj.data.name = value;
            }
            else if (0 == key.compare("email"))
            {
                obj.data.email = value;
            }
            else
            {
                std::cerr<<"Unknow key:" << key << std::endl;
            }
        }
    }
    return 0;
}

bool ParseLine(const std::string& line, std::string& key, std::string& value)
{
    bool flag = false;
    if (line.find("//") != 0)
    {
        size_t pos = line.find("=");
        if (pos != std::string::npos)
        {
            key = line.substr(0, pos);
            value = line.substr(pos + 1, line.length());
            flag = true;
        }
    }
    return flag;
}
</iostream></string></fstream>



demo.txt:



demo.txt:

name=jhon
email=icerlion@163.com


很简单.似乎文本文件中的每一行都有两个字段,键和值,注释行除外(以"//"开头).

您可以读取行到内存,然后自己从行中解析键和值(通过"="将行拆分为向量,您可以编写函数来实现该功能),然后检查键,然后将值分配给您的结构数据

关键点:通过"="将每一行分割为一个向量.
It''s easy. It seems that every line in your text file has two fields, key and value, except comment line(starts with "//").

You can read line to memory, then parse the key and value from line yourself(split the line to vector by "=", you can write a function to implement it), then check the key, and assign the value to your struct DATA

Key point: split every line to a vector by "=".


阅读^ ]以及之前的所有页面以及之后的所有页面.开始正确使用MSDN.
Read this[^], and all the pages before it, and all the pages after it. Start making proper use of MSDN.


这篇关于如何在C ++中读取文件并维护文件指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆