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
| |
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
| |
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.
| |
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.
Source: https://www.cplusplus.com/forum/beginner/119632/
0 Response to "Read Integers and Strings From File Into Struct Array C"
إرسال تعليق