Click here to Skip to main content
15,883,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C++
class Demo
{
public:
struct DATA
{
char* name;
char* Lang;
int age;
};
};


to read the file i use the following code
filestr is a filestream pointer and str is of type string
C++
while(!filestr.eof())
  {
      getline(filestr,str);

      cout<<str<<endl;
  }


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
Posted

C++
// 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:
VB
name=jhon
email=icerlion@163.com
 
Share this answer
 
v2
Comments
Tarun Batra 22-Feb-14 6:22am    
i need some more help from you actually a guidance
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 "=".
 
Share this answer
 
v2
Comments
Tarun Batra 7-Nov-12 9:51am    
Can you please give some pseudo code sir
Read this[^], and all the pages before it, and all the pages after it. Start making proper use of MSDN.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900