c-程序在读了几百行之后中断



我做了一个扫描函数,它本质上只是将文件的行扫描到一个名为bufferchar *中。然而,在读取了几百行之后,程序就停止了工作。我刚得到一个程序已经停止工作弹出窗口。假设我在内存分配方面出了问题,但我不确定是什么。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *scan_file(FILE *fp);
#define MAX_LINE                200

int main(void) {
    FILE *fp = fopen("test.txt", "r");
    char *contents = scan_file(fp);
    printf("%sn", contents);
    return 0;
}
// Scan in file into a buffer. Returns a malloc-ed string
char *scan_file(FILE *fp) {
    int buf_len = 1;
    int contents_len = buf_len;
    char *buffer = malloc(sizeof(char) * (MAX_LINE + 1));
    char *contents = malloc(sizeof(char) * (buf_len + 1));
    strcpy(contents, "");
    while (fgets(buffer, MAX_LINE, fp) != NULL) {
        buf_len = strlen(buffer);
        contents_len += buf_len;
        realloc(contents ,contents_len);
        strcat(contents, buffer);
        strcpy(buffer, "");
    }
    free(buffer);
    return contents;
}

代码无法使用realloc() 的返回值

分配大小按1缩小。

重复的strcat()形成慢(n*n(溶液。

int相比,考虑使用size_t来确定阵列大小。

与其调用变量...len,不如考虑...size来确认字符串中最后一个空字符的存在。

char *scan_file(FILE *fp) {
    // int buf_len = 1;
    size_t buf_size = 1;
    // int contents_len = buf_len;
    size_t contents_size = buf_size;
    // char *buffer = malloc(sizeof(char) * (MAX_LINE + 1));
    // fgets(..., MAX_LINE, ...) will only read up to MAX_LINE - 1 characters.
    char *buffer = malloc(MAX_LINE);
    char *contents = malloc(buf_size + 1u);
    if (buffer == NULL || contents == NULL) {
      fprintf(stderr, "Out of memoryn");
      return EXIT_FAILURE;
    }
    // strcpy(contents, "");
    contents[0] = '';
    while (fgets(buffer, MAX_LINE, fp) != NULL) {
        // buf_len = strlen(buffer);
        buf_size = strlen(buffer) + 1u;
        // contents_len += buf_len;
        // realloc(contents ,contents_len);
        void *p = realloc(contents ,contents_size + buf_size);
        if (p == NULL) {
          fprintf(stderr, "Out of memoryn");
          return EXIT_FAILURE;
        }
        contents = p;
        // strcat(contents, buffer);
        strcpy(contents + contents_size, buffer);
        // now add
        contents_size += buf_size;
        // Code here not truly needed, yet helps in debugging.
        // strcpy(buffer, "");
        buffer[0] = '';
    }
    free(buffer);
    return contents;
}
  • 使用realloc()的返回值
  • realloc(NULL, size)malloc(size)一样工作;无需预先分配
  • 避免过度复制和strlen()
  • 只需在第一次(也是唯一一次(触摸数据时将其放在您想要的位置

#include <stdio.h>
#include <stdlib.h>
char *scanfile (FILE *fp)
{
size_t size, used;
char *buff = NULL;
int ch;
for (size=used=0;;      ) {
        ch = getc(fp);
        if (ch == EOF) break;
        if (used+1 >= size) {
                size_t newsize = used? 2*used: 1024 ;
                char *tmp = realloc(buff, newsize);
                if (!tmp) FAIL();
                else {buff = tmp; size = newsize; }
                }
        buff[used++] = ch;
        }
        /* Nothing read: return NULL */
if (!used) return NULL;
buff[used++] = 0;
/* maybe realloc (shrink) buff here */
return buff;
}

相关内容

  • 没有找到相关文章

最新更新