Class and Objects//Files And Fstream

Anyone knows how to make funcqtion, wich reads from file any line as a string, than it splits up this string, and with Set() function we put it in class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

#include <fstream>
#include <sstream>
#include <iostream>
#include <iterator>
#include <vector>
#include <string>

class Monimolimnion {
  public:
    void Set(const std::vector<std::string>& line_tokens)
    {
        tokens = line_tokens;
    }
    
    std::vector<std::string> tokens;
};

std::vector<std::string> funcqtion(const std::string& s, int line_num)
{
    std::ifstream f(s);
    std::string line;
    
    // discard lines before the one you want (starting at 1)
    for (int i = 0; i < line_num - 1; i++)
        getline(f, line);
    
    // get the line'th line
    if (getline(f, line))
    {
        // split the string up into each token
        std::istringstream iss(line);
        return { std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
    }
    else
    {
        std::cout << "error, line not found" << std::endl;
    }
}

int main()
{
    Monimolimnion monimolimnion;
    
    std::vector<std::string> tokens = funcqtion("test.txt", 3);

    monimolimnion.Set(tokens);
    
    for (auto& token : monimolimnion.tokens)
    {
        std::cout << token << "\n";
    }
    
    /*
    
    Example file:
    
    1 a b c
    2 d e f
    3 g h i
    4 j k l

    Example output:
    
    3
    g
    h
    i
    
    */
}
thanks. And also do you know how to convert string to char*? i know c_str, but you need const char
std::string::c_str() returns a const char*.
A const char (no pointer) would just be a single character.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>

using namespace std;

class Person{
	protected:
		 char* Name;
		 char* SurName;
	public:
		Person( char *Name,  char* SurName){
			Set(Name,SurName);
		}
		void Set( char *Name,  char* SurName){
			SetName(Name);
			SetSurName(SurName);
		}
		void SetName( char *Name){
			this->Name=Name;
		}
		void SetSurName( char* SurName){
			this->SurName=SurName;
		}
		 char *GetName(){
			return Name;
		}
		 char *GetSurName(){
			return SurName;
		}
};

class Abon:public Person{
	protected:
		 char* PhNum;
	public:
	    Abon( char *Name,
			  char *Surname,
			  char* PhNum) : Person(Name, Surname){
	    	SetPhNum(PhNum);
		}
		void SetPhNum(char* PhNum){
			this->PhNum=PhNum;
		}
		 char* GetNum(){
			return PhNum;
		}
};
class Citizen:public Abon{
	protected:
		char *IdNum;
		char *Addr;
	public:
		Citizen( char *Name=" ",
				 char *SurName=" ",
				 char *PhNum=" ",
				 char *Addr=" ",
				char *IdNum=" "): Abon(Name,SurName,PhNum){
					
				Set(IdNum,Addr);
		}
	 void Set(char *IdNum, char* Addr){
	 	SetId(IdNum);
	 	SetAddr(Addr);
	 }
	 void SetId(char* IdNum){
	 	this->IdNum=IdNum;
	 }
	 void SetAddr(char* Addr){
	 	this->Addr=Addr;
	 }
	 char* GetAddr(){
		return this->Addr;
	}
	char* GetIdNum(){
		return this->IdNum;
	}
};

class ContactList{
	private:
		const char *FileName;
	public:
		ContactList(const char *MyFileName){
			this->FileName=MyFileName;
		}
		void WriteData(Citizen PPL){
			fstream MyData;
			MyData.open (FileName,std::fstream::app);
			MyData<<PPL.GetName()<<";"<<PPL.GetSurName()<<";"
			<<PPL.GetNum()<<";"<<PPL.GetIdNum()<<";"<<PPL.GetAddr()<<";"<<endl;
			MyData.close();
		}
		Citizen ReadData(){
			Citizen TMP=Citizen();
		     string s;
		     string Sign=";";
			 fstream f(FileName, fstream::in );
             getline( f, s, '\n');
             f.close();
             ////////////////////////
             size_t pos = 0;
			 string token;
		        pos = s.find(Sign);
    			token = s.substr(0, pos);
    			char* A=strdup(token.c_str());;
    			TMP.SetName(A);
    			s.erase(0, pos + Sign.length());
    			
    			pos = s.find(Sign);
    			token = s.substr(0, pos);
    			TMP.SetSurName(token);
    			s.erase(0, pos + Sign.length());
    			
    			pos = s.find(Sign);
    			token = s.substr(0, pos);
    			TMP.SetId(token);
    			s.erase(0, pos + Sign.length());
    			
    			pos = s.find(Sign);
    			token = s.substr(0, pos);
    			TMP.SetPhNum(token);
    			s.erase(0, pos + Sign.length());
    			
    			pos = s.find(Sign);
    			token = s.substr(0, pos);
    			TMP.SetAddr(token);
    			s.erase(0, pos + Sign.length());
    			
    		    return TMP;
    				
}
            
};

int main(int argc, char** argv) {
	
	Citizen CTZ=Citizen("name","surname","12345","New York","54612");
	ContactList Friend=ContactList("Friends.txt");
	Friend.WriteData(CTZ);
	Friend.ReadData();

return 0;
}
            

Thats My class and i need to convert string to char to set in Set funcqtion
IN Citizen ReadData()
@Ganado,
Monimolimnion is a funny name. Does it have a meaning ?
@andriass
Ugh, where to begin... your code actually isn't even legal C++ because you try to assign string literals to non-const char*'s. The mixing of std::strings and char*'s is usually only done for compatibility reasons and can be a total mess to work with.

The strdup function you call is a posix function that allocates memory, so by not freeing that memory, you have a memory leak. In C++ this kind of thing is best handled with constructors that allocate data, and destructors that delete data.

The sloppy way that you could copy the data from an std::string and copy it into a null-terminated C string would be something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

char* convert(const std::string& s)
{
	char* c_str = new char[s.length() + 1];
	for (int i = 0; i < s.length(); i++)
	{
		c_str[i] = s[i];
	}
	c_str[s.length()] = '\0';
	return c_str;
}
 
int main() {
 
	std::string str = "Hello";
	char* new_string = convert(str);
 
	delete [] new_string;
	return 0;
}


Your program will still run if you don't delete your strings, but just note that it's a memory leak and bad practice.

@Thomas1965
Got it from Wiktionary, lol.
https://en.wiktionary.org/wiki/monimolimnion
The lower, dense stratum of a meromictic lake (one with permanently stratified layers) that does not mix with the waters above.
Last edited on
Topic archived. No new replies allowed.