This program is really getting on my nerves:
I am trying to read a line from a file with following information:
512 MB 136.186.99.1 00-25-B3-0B-31-29
which is in the format of double string string string
and the code I’m using is
fscanf(filePtr, "%lf %s %s %s", &Computer[i].ram, Computer[i].ram_unit, Computer[i].MACNo, Computer[i].IPV4);
but when I print Computer[i].ram_unit I get:
MB136.186.99.1
Please help me to find out what I’m doing wrong. Let me know if you like me to paste the entire code.
Thanks
First, you have
MACNoandIPV4reversed in yourfscanfrelative to the sample input.Can’t tell for sure without seeing the structure definition, but it looks like a possible array overrun. For example, if your
Computerwas defined like this:when you read “MB” into
ram_unit, you could end up havingAnd then when you read in the IP address into IPV4 that makes it
When you go to print out
ram_unit, the print function will start at the memory location&ram_unit[0]and keep on printing until it sees aNULL. But since theNULLended up inIPV4[0]and that got overwritten when you read in the IP address, it won’t stop printing until it gets to the NULL atIPV4[11]and so you get the unexpected concatenation.