Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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;
};
};
 
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;
  }
 
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 7-Nov-12 3:40am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

// 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:
name=jhon
email=icerlion@163.com
  Permalink  
v2
Comments
Tarun Batra at 22-Feb-14 6:22am
   
i need some more help from you actually a guidance
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 "=".
  Permalink  
v2
Comments
Tarun Batra at 7-Nov-12 9:51am
   
Can you please give some pseudo code sir
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Read this[^], and all the pages before it, and all the pages after it. Start making proper use of MSDN.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 392
1 CPallini 365
2 Sergey Alexandrovich Kryukov 348
3 BillWoodruff 264
4 George Jonsson 255
0 OriginalGriff 5,095
1 CPallini 4,285
2 Sergey Alexandrovich Kryukov 3,767
3 George Jonsson 2,941
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100