C中使用fopen打开文本文件后,多了字符

发布于 2024-12-27 09:45:53 字数 1157 浏览 1 评论 0原文

我需要以 x*[tab]*y*[tab]*z*[tab]\n* 格式读取数据表,所以我使用 fopen和 fgetc 来传输字符。当c==EOF时循环结束。 (c 是字符。) 但我对此遇到了困难,因为它溢出了我的数组。经过一些调试后,我意识到最后一行之后打开的文件包含:

北安普顿牛津 68 呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜卍卍卍卍卍卍卍[...]呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýý ««««««««îþîþ

那是什么?为什么它没有出现在我的纯文本文件中?我该如何克服这个问题?

destination = fopen("ukcities.txt", "rt"); // r = read, t=text 

if (destination != NULL) {
    do {
       c = fgetc (destination);
              if (c == '    ') {
                temp_input[i][n] = '\0';
                i++;
                n=0;
              } else if (c == '\n') {
                  temp_input[i][n] = '\0';
                  printf("%s %s %s \n", temp_input[0], temp_input[1], temp_input[2]);
                  i = 0;
                  n=0;
              } else {
                  temp_input[i][n] = c;
                  n++;
              }
        } while (c != -1);  

    return 1;
} else {
    return 0;       
}

I need to read in table of data in a format x*[tab]*y*[tab]*z*[tab]\n* so I am using fopen and fgetc to stream characters. Loop is ending when c==EOF. (c is character.)
But I had difficulties with that as it overflows my array. After doing some debugging I realised that the opened file after the last line contains:

Northampton Oxford 68
ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ[...]ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««îþîþ

What is that? And why does that not appear in my plain text file? And how do I overcome this problem?

destination = fopen("ukcities.txt", "rt"); // r = read, t=text 

if (destination != NULL) {
    do {
       c = fgetc (destination);
              if (c == '    ') {
                temp_input[i][n] = '\0';
                i++;
                n=0;
              } else if (c == '\n') {
                  temp_input[i][n] = '\0';
                  printf("%s %s %s \n", temp_input[0], temp_input[1], temp_input[2]);
                  i = 0;
                  n=0;
              } else {
                  temp_input[i][n] = c;
                  n++;
              }
        } while (c != -1);  

    return 1;
} else {
    return 0;       
}

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

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

发布评论

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

评论(2

撕心裂肺的伤痛 2025-01-03 09:45:53

看着我的水晶球,我发现 fread 或您正在使用的任何东西(显然是 fgetc 这使得它更加真实)不会以 null 终止它的数据读取并且您尝试将其打印为 C 字符串。以 NUL 字符(0)结束数据,然后它将正确打印。

Looking into my crystal ball, I see that fread or whatever you're using (apparently that's fgetc which makes it even more true) doesn't null-terminate the data it reads and you're trying to print it as a C-string. Terminate the data with a NUL character (a 0) and then it will print correctly.

挽心 2025-01-03 09:45:53

该字符串看起来未终止。在 C 中,不以 '\0' 字符(又名空字符)结尾的字符串会导致持续的麻烦,因为许多标准库和系统库都希望字符串以空字符结尾。

确保读完所有数据后,字符串就终止了;在某些情况下,必须手动完成。有几种方法可以做到这一点(下面使字符串的所有字符为空,因此只要不覆盖最后一个字符,字符串将始终以空结尾):

// (1) declare an array of char, set all characters to null character
char buffer[1000] = {0};

或者,如果您正在跟踪无论您在缓冲区中的位置,您也可以执行以下操作:

// (2) after reading in all data, add the null character yourself:
int n; // number of bytes read
char buf[1000];

// read data into buf, updating n

buf[n] = '\0'; // (tip: may need to use buf[n+1])

无论哪种情况,重要的是不要超出缓冲区的末尾。如果您只分配了 1000 个字节,则仅使用 999 个字节并为空字符保存 1 个字节。

That string looks unterminated. In C, strings that don't end with a '\0' character (a.k.a. null character) lead to constant trouble because a lot of the standard library and system libraries expect strings to be null-terminated.

Make sure that when you have finished reading in all the data, that the string is terminated; in some cases it must be done manually. There are a few ways to do this (the below makes all characters of the string null, so as long as you don't overwrite the very last one, the string will always be null terminated):

// (1) declare an array of char, set all characters to null character
char buffer[1000] = {0};

Alternatively, if you are keeping track of where you are in the buffer, you can also do this:

// (2) after reading in all data, add the null character yourself:
int n; // number of bytes read
char buf[1000];

// read data into buf, updating n

buf[n] = '\0'; // (tip: may need to use buf[n+1])

In either case, it is important that you don't overstep the end of the buffer. If you've only allocated 1000 bytes, then use only 999 bytes and save 1 byte for the null character.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文