C fscanf 奇怪地连接两个字符串

发布于 2024-11-09 05:51:30 字数 438 浏览 0 评论 0原文

这个程序真的让我很紧张:

我试图从文件中读取一行,其中包含以下信息:

512 MB 136.186.99.1 00-25-B3-0B-31-29

其格式为双字符串字符串

,我正在使用的代码是

fscanf(filePtr, "%lf %s %s %s", &Computer[i].ram, Computer[i].ram_unit, Computer[i].MACNo, Computer[i].IPV4);

但是当我打印 Computer[i].ram_unit 我得到:

MB136.186.99.1

请帮助我找出我做错了什么。如果您希望我粘贴整个代码,请告诉我。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝影如岚 2024-11-16 05:51:30

首先,您在 fscanf 中相对于示例输入颠倒了 MACNoIPV4

在没有看到结构定义的情况下无法确定,但看起来可能是数组溢出。例如,如果您的Computer定义如下:

struct ComputerType {
    double ram;
    char ram_unit[2];  /* This needs to be 3 (assuming unit is always 2 chars long) */
    char IPV4[16];
    char MACNo[17];
};

当您将“MB”读入ram_unit时,您最终可能会得到

ram_unit[0] = 'M'
ram_unit[1] = 'B'
IPV4[0] = '\0'

然后当您将IP地址读入IPV4时这使得

ram_unit[0] = 'M'
ram_unit[1] = 'B'
IPV4[0] = '1'
IPV4[1] = '3'
[etc]
IPV4[10] = '1'
IPV4[11] = '\0'

当你去打印 ram_unit 时,打印函数将从内存位置 &ram_unit[0] 开始并继续打印,直到看到 >空。但由于 NULL 最终出现在 IPV4[0] 中,并且当您读取 IP 地址时它被覆盖,因此它不会停止打印,直到到达 NULL IPV4[11] 所以你会得到意想不到的串联。

First, you have MACNo and IPV4 reversed in your fscanf relative 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 Computer was defined like this:

struct ComputerType {
    double ram;
    char ram_unit[2];  /* This needs to be 3 (assuming unit is always 2 chars long) */
    char IPV4[16];
    char MACNo[17];
};

when you read "MB" into ram_unit, you could end up having

ram_unit[0] = 'M'
ram_unit[1] = 'B'
IPV4[0] = '\0'

And then when you read in the IP address into IPV4 that makes it

ram_unit[0] = 'M'
ram_unit[1] = 'B'
IPV4[0] = '1'
IPV4[1] = '3'
[etc]
IPV4[10] = '1'
IPV4[11] = '\0'

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 a NULL. But since the NULL ended up in IPV4[0] and that got overwritten when you read in the IP address, it won't stop printing until it gets to the NULL at IPV4[11] and so you get the unexpected concatenation.

掌心的温暖 2024-11-16 05:51:30

要从这样的文件中读取一行:

1603 Lu,Ma,Mi,Ju,Vi,Sa,No Mar del Plata 08:25 09:20 阿根廷航空公司

使用此:

fscanf(pf, "%d %2s,%2s,%2s,%2s,%2s,%2s,%2s %[^\n]s %d:%d %d:%d %[^\n]s",
    ®istro.code,
    ®istro.dia.lunes,
    ®istro.dia.martes,
    ®istro.dia.miercoles,
    ®istro.dia.jueves,
    ®istro.dia.viernes,
    ®istro.dia.sabado,
    ®istro.dia.domingo,
    ®istro.destino,
    ®istro.hSalida.hh,
    ®istro.hSalida.mm,
    ®istro.hLlegada.hh,
    ®istro.hLlegada.mm,
    ®istro.aerolinea
);

To read a line from a file like this one:

1603 Lu,Ma,Mi,Ju,Vi,Sa,No Mar del Plata 08:25 09:20 Aerolineas Argentinas

Use this:

fscanf(pf, "%d %2s,%2s,%2s,%2s,%2s,%2s,%2s %[^\n]s %d:%d %d:%d %[^\n]s",
    ®istro.code,
    ®istro.dia.lunes,
    ®istro.dia.martes,
    ®istro.dia.miercoles,
    ®istro.dia.jueves,
    ®istro.dia.viernes,
    ®istro.dia.sabado,
    ®istro.dia.domingo,
    ®istro.destino,
    ®istro.hSalida.hh,
    ®istro.hSalida.mm,
    ®istro.hLlegada.hh,
    ®istro.hLlegada.mm,
    ®istro.aerolinea
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文