Read Integers and Strings From File Into Struct Array C

Reading integers and a string into Array of Structs

Hi, I'thou having trouble figuring out how to store 4 ints separated by a '.' and a string from a "hosts.txt" file which contains:

111.22.iii.44 platte
555.66.7.88 wabash
111.22.five.66 dark-green
555.66.11.24 homer
123.45.67.89 loner
12.34.56.78 pizza
555.66.9.10 ulysses
0.0.0.0 none

Here is my lawmaking and then far

                          1
2
3
4
5
vi
seven
8
9
10
11
12
13
14
fifteen
16
17
xviii
xix
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
forty
41
42
43
44
45
46
47
48
49
fifty
51
52
53
54
55
56
                                                      #include<iostream>                            #include<fstream>                            #include<cord>                            using                            namespace                            std;                            struct                            ADDRESS_TYPE {                            int                            a;                            int                            b;                            int                            c;                            int                            d; 	string name; };                            int                            ReadRecord(ADDRESS_TYPE &chiliad, istream &in) {                            int                            temp; 	in >> m.a, m.b, m.c, chiliad.d;                            // trying to store the ints into int variables of the struct a, b, c, d                            getline(in, m.name);                            // read the cord and store into c++ string of struct                            temp = m.a + m.b + m.c + g.d;                            // checks to see if the ip address is 0.0.0.0                            if(temp == 0) 	{  		cout <<                            "0.0.0.0 record read"                            << endl;                            return                            -1; 	}                            else                            {                            return                            0; 	} }                            int                            master() {                            int                            count = 0;                            int                            done = -1;                            int                            result; 	ADDRESS_TYPE m[200];                            // assortment of structs                            ifstream in;                            char                            infilename[150]; 	cout <<                            "What is the input file proper name?: "                            << affluent;                            // get input file name                            cin >> infilename; 	in.open(infilename);                            if(!in) 	{ 		cout <<                            "Error opening the input file. Exiting."                            << endl;                            return                            0; 	}                            while(count < 200 && ReadRecord(m[count++],in)!=done);                            // reads input file until 0.0.0.0 record is read                            for(int                            ten = 0; x < count; x++)                            // test impress of struct                            { 		 		cout << m[10].a <<                            "."                            << m[x].b <<                            "."                            << m[x].c <<                            "."                            << m[x].d <<                            "\t"; 		cout << g[10].name << endl; 	}                            return                            0; }                        

And here is my output:
What is the input file name?: hosts.txt
0.0.0.0 record read
111.0.0.0 .22.3.44 platte
555.2682292.0.2682292 .66.7.88 wabash
111.1968756680.2682292.131097 .22.v.66 green
555.2683036.1968756738.0 .66.eleven.24 homer
123.24.0.2681224 .45.67.89 loner
12.0.9437326.2681232 .34.56.78 pizza
555.7536745.7471220.6029433 .66.nine.10 ulysses
0.7209065.6029413.7929939 .0.0.0 none
7143525.4391004.7471221.6619250
7274563.7602286.7274610.5439596
4391004.7209071.7471220.7077999
7536748.5439580.7471215.6881396
5636188.7471205.6881395.7209071
0.0.0.0

And then from what I can tell is that when I read the first number 111 from the hosts.txt file information technology stores 111 into m[0].a so 0 is filled into k[0].b , thousand[0].c, m[0].d. Then it reads the rest of the numbers of the ip accost(not sure what they are stored into), then the string is read into the C++ string. I'thou not very familiar with reading input from a txt file and storing them into arrays and structs. If anyone tin give me some insight that would be smashing. Cheers.

the line in >> yard.a, m.b, m.c, m.d; // trying to store the ints into int variables of the struct a, b, c, d is not reading four numbers. Its but reading first number. For reading 4 values you will have to use
in >> m.a >> m.b >> m.c >> one thousand.d;

Fifty-fifty this is not going to become you valid numbers the "." between the number is the culprit, you lot will have to read and ignore information technology.
The code is printing the residue of IP along with the name because of the getline. This telephone call is reading unabridged remaining cord from file till end of line.

To get and ignore the "." one simple play a joke on would be to

                          1
2
3
                                                      char                            ignore; in >> m.a >> ignore >> m.b >> ignore >> m.c >> ignore >> m.d; in >> g.name;                        

Ah! I encounter. I was trying to use in >> m.a >> thou.b >> m.c >> chiliad.d;
at showtime but you are correct, the peroid would still accept to be read and get line was non reading in what I wanted. I did not know that you tin utilize a dummy variable to read in the "." in the text file.

                          ane
2
three
                                                      char                            ignore; in >> chiliad.a >> ignore >> one thousand.b >> ignore >> m.c >> ignore >> 1000.d; in >> m.proper noun;                        

Here is the output now:
What is the input file name?: hosts.txt
0.0.0.0 record read
111.22.3.44 platte
555.66.7.88 wabash
111.22.5.66 dark-green
555.66.xi.24 homer
123.45.67.89 loner
12.34.56.78 pizza
555.66.nine.10 ulysses
0.0.0.0 none

It worked perfectly, thank you for your help codewalker!

Topic archived. No new replies allowed.

shawcompall.blogspot.com

Source: https://www.cplusplus.com/forum/beginner/119632/

0 Response to "Read Integers and Strings From File Into Struct Array C"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel