【问题标题】:Copying array of characters into a text file将字符数组复制到文本文件中
【发布时间】:2018-01-27 21:23:51
【问题描述】:

我想使用fwrite 函数将句子写入文本文件。所以我必须将这些作为函数参数:

fwrite( const void *restrict buffer, size_t size, size_t count, FILE *restrict stream )
  1. 缓冲区 - 指向要写入的数组中第一个对象的指针
  2. size - 每个对象的大小
  3. count - 要写入的对象数
  4. 流 - 指向输出流的指针

正如How to dynamically allocate memory space for a string and get that string from user? 所说,浪费内存是一种不好的做法。我阅读了答案并有了一个想法,以我的方式编写代码。

我的想法是:

  1. 创建一个字符数组并写入其元素
  2. 使用mallocrealloc 使该数组越来越大
  3. 继续写入直到到达EOF

不幸的是,我遇到了一个问题。每当我构建和执行代码时,它都会给我这个错误:

已停止工作

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    size_t index=0,size=1;
    char ch;
    FILE *fptr;
    fptr=fopen("E:\\Textsample.txt","w");

    /////////////////Checking///////////////
    if(fptr==NULL)
        {
            free(fptr);
            puts("Error occured!\n");
            exit(EXIT_FAILURE);
        }
    /////////////////Checking///////////////

    char *sentence =(char*) malloc(sizeof(char)) ;
    puts("Plaese write :\n");

    while((ch=getchar()) != EOF)
        {
            sentence[index]=ch;
            index++;
            size++;
            realloc(sentence,size);

            /////////////////Checking///////////////
            if(sentence==NULL)
                {
                    printf("Error Occured!\n");
                    free(sentence);
                    exit(EXIT_FAILURE);
                }
            /////////////////Checking///////////////

        }

    //Copying sentnce(s) to the text file.
    fwrite(sentence,sizeof sentence[0],index,fptr);
    free(sentence);
    free(fptr);
    return 0;
}

【问题讨论】:

  • Nitpick:您不需要在检查代码中释放 NULL ptr..
  • “已停止工作”?什么?也许在调试器中运行,看看它在哪一行崩溃。
  • 另外,最后做close(ptr),因为它是FILE *。无需免费,由fclose处理
  • getchar() 返回 int 而不是 char,这对于成功检测到 EOF 很重要。
  • sentence[0] 是一个char,所以这个sizeof sentence[0] 的大小等于char 的大小,总是1

标签: c file fwrite dynamic-allocation


【解决方案1】:

我想你需要写sentence = realloc(sentence, size)

此外,每次我们需要时将分配的大小加倍会更有效。 然后我们可以再读一遍我们已经读过的,所以我们需要更少的重新分配。那么size 加倍(它是分配缓冲区的大小,而index 跟踪读取的字符。

所以

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    size_t index=0,size=1;
    int ch;
    FILE *fptr;
    fptr=fopen("./bla.txt","w");

    /////////////////Checking///////////////
    if(fptr==NULL)
    {
            fprintf(stderr, "Error occured!\n");
            exit(EXIT_FAILURE);
    }
    /////////////////Checking///////////////

    char *sentence = malloc(size) ;
    puts("Please write, (close with return) :\n");

    while((ch=getchar()) != '\n')
    {
            sentence[index]=(ch & 0xff);
            index++;;
            if (index >= size){
                    sentence = realloc(sentence,2*size);

                    /////////////////Checking///////////////
                    if(sentence==NULL)
                    {
                    fclose(fptr);
                    fprintf(stderr, "Error Occured!\n");
                    free(sentence);
                    exit(EXIT_FAILURE);
                    }
                    /////////////////Checking///////////////
                    size *= 2; /* update size */
            }
    }

    //Copying sentence(s) to the text file.
    fwrite(sentence,1,index,fptr);
    free(sentence);
    fclose(fptr);
    return 0;

}

在我的系统上运行良好。

【讨论】:

  • 我按照你说的做了,但是还是不行。
【解决方案2】:

您只想将标准输入写入您已经打开的文件。那么为什么不去掉 malloc 中间人呢?

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int ch;
    FILE *fptr;
    fptr=fopen("./bla.txt","w");

    /////////////////Checking///////////////
    if(fptr==NULL)
    {
            fprintf(stderr, "Error occured!\n");
            exit(EXIT_FAILURE);
    }
    /////////////////Checking///////////////

    puts("Please write, (close with return) :\n");

    while((ch=getchar()) != '\n')
    {       
            fputc(ch, fptr);
    }
    //if you want the newline too 
    fputc('\n', fptr);
    fclose(fptr);
    return 0;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 2015-07-10
    • 2021-06-24
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    相关资源
    最近更新 更多