c - 使用字符串缓冲区读写文件

标签 c segmentation-fault

我正在尝试读取和写入文本文档,然后对其执行操作。

#include<stdio.h>
#include<string.h>

int WRITE(char *FILENAME, char *DATA)
{
    FILE *ptr_file;
    ptr_file =fopen(FILENAME, "w");
    if (!ptr_file)
        return 1;
    fprintf(ptr_file,"%s", DATA);
    fclose(ptr_file);
    return  0;
}

char READ(char *FILENAME)
{
    FILE *ptr_file;
    char buf[1000];
    char* ret="";
    ptr_file =fopen(FILENAME,"r");
    if (!ptr_file)
        return "FAIL\n";
    while (fgets(buf,1000, ptr_file)!=NULL)
        ret=strcat("%s",ret);
    fclose(ptr_file);
    return ret;
}

int main()
{   
    char* DAT = "lol";
    char* FILENAME = "output.txt";
    char* NEWDAT;
    int count=0;
    int max=10;

    while (count<max) {
        NEWDAT=READ(FILENAME);
        WRITE(FILENAME,strcat(DAT,NEWDAT));
        count++;
    }
    READ(FILENAME);
    return 0;
}

这是编译器说的

gcc -Wall -o "filewrite" "filewrite.c" (in directory: /home/x/Documents/programming/C code)
filewrite.c: In function ‘READ’:
filewrite.c:22:3: warning: return makes integer from pointer without a cast [enabled by default]
filewrite.c:26:2: warning: return makes integer from pointer without a cast [enabled by default]
filewrite.c: In function ‘main’:
filewrite.c:38:9: warning: assignment makes pointer from integer without a cast [enabled by default]
Compilation finished successfully.

然后它向我抛出代码 139/段错误。我……真的无法理解这里发生了什么;我现在的水平。

#

答案后编辑: 固定代码工作

#include <stdio.h>
#include <string.h>
#include <stdlib.h>  // malloc

int WRITE(char *FILENAME, char *DATA)
{
    FILE *ptr_file;
    ptr_file =fopen(FILENAME, "w");
    if (!ptr_file)
        return 1;
    fprintf(ptr_file,"%s", DATA);
    fclose(ptr_file);
    return  0;
}

char* READ(char *FILENAME)
{
    FILE *ptr_file;
    char buf[1000];     
    char *ret = malloc(1); 
    int retsize = 1; 
    ret[0]='\0';
    buf[0]='\0';

    ptr_file = fopen(FILENAME,"r");
    if (!ptr_file) {
        ret = realloc(ret, strlen("FAIL\n") + 1);
        strcpy(ret, "FAIL\n");
        return ret;
    }
    while (fgets(buf,1000, ptr_file)!=NULL)
    {
        retsize += strlen(buf)+1;  // new size is old size + length of string in buf
        ret = realloc(ret, retsize);  // increase the size of the allocation
        strcat(ret, buf);          // append the new data
    }
    fclose(ptr_file);
    return ret;
}

int main()
{   
    char* DAT = malloc(1);
    int datsize = 1;
    char* FILENAME = "output.txt";
    char* NEWDAT; 
    strcpy(DAT, "LOL");
    int count=0;
    int max=5;

    while (count<max) {
        NEWDAT=READ(FILENAME);
        datsize += strlen(NEWDAT) + strlen(DAT);
        DAT = realloc(DAT, datsize);
        strcat(DAT,NEWDAT);
        WRITE(FILENAME,DAT);

        printf("%i\n",count);
        printf("%s\n",DAT); 
        count++;
    }
    READ(FILENAME);
    return 0;
}

最佳答案

READ(糟糕的命名约定 BTW)声称返回一个 char,但您的代码表现得像 char*

strcat("%s", ret) 是错误的,因为目标 ("%s") 没有任何空间来附加 "ret")

关于c - 使用字符串缓冲区读写文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17418080/

相关文章:

c - 在 GCC 中实现编译时只读函数指针表

c - 没有任何非法访问的随机段错误

c - 数独生成器导致段错误

c++ - 在 Visual Studio 2010 的 C++ 中使用 lapack C header 的错误

c - 计算算术数组下标表达式作为 C 中条件运算符的操作数的一部分

c - "blkclr (mach_kernel)"出现在 mac 的 Shark 分析器中。它有什么作用?

for 循环末尾的 C++ 段错误

c - strtol 未按预期工作

c - 分配给整数指针会导致崩溃

c - 读入文件导致段错误