使用“ read”函数读取文件

问题描述

我正在学习C,我需要阅读一个文本文件,但是我只能使用“ write,malloc,free,open,read,close”。

那是我的代码

#define MAXCHAR 10000

int open_fp(int check)
{
    char *int_vector;
    int fp,len;
    int i,j;
    char buffer[MAXCHAR]; 

    if(check == 0) //standard list
    {
        if((fp = open("file.txt",O_RDONLY)) != -1) //check if the fp is opened. -1 = error
        {
            printf("\n%d\n",fp); // DEBUG FUNCTION
            sleep(1);

            if (!(int_vector = (char*)malloc(sizeof(char) * sizeof(char))))
            {
                printf("\nWrong allocation\n!"); // DEBUG FUNCTION
                return(0);
            }
            len = read(fp,int_vector,MAXCHAR);

            for(i=0;i<len;i++)
            {
                printf("%c",int_vector[i]);
            } 
        }
        else
        {
            printf("File error!");
            return (0);
        }
        
    }
    return(0);
}

现在我的问题是:您可以在这里阅读

 char buffer[MAXCHAR];

我已经创建了静态缓冲区,但是我想创建一个动态缓冲区,该缓冲区允许我根据文本文件中字符的数量来调整缓冲区的大小,但是我不知道如何...有人将???

解决方法

首先,您在下面一行中分配内存的方式是错误的。

//This allocates only 2 bytes of memory,but you are trying to read 10000
if (!(int_vector = (char*)malloc(sizeof(char) * sizeof(char))))

如下更正该行

//better to add one byte extra and store \0 at the end,useful in case of string operations
if (!(int_vector = malloc(MAXCHAR+1)))

就您的问题而言,在这种特殊情况下,您不需要重新分配内存,因为您只是读取要缓冲的字节并进行打印。

一个malloc就足够了。

#include<stdlib.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define MAXCHAR 100

int open_fp(int check)
{
    char *int_vector;
    int fp,len;
    int i,j;
    char buffer[MAXCHAR]; 

    if(check == 0) //standard list
    {
        if((fp = open("file.txt",O_RDONLY)) != -1) //check if the fp is opened. -1 = error
        {
            printf("\n%d\n",fp); // DEBUG FUNCTION
            sleep(1);

            if (!(int_vector = (char*)malloc(MAXCHAR)))
            {
                printf("\nWrong allocation\n!"); // DEBUG FUNCTION
                return(0);
            }
            //not doing memset on purpose because only limited bytes are accessed.
            while(len = read(fp,int_vector,MAXCHAR))
            {
                printf("\n **number of bytes read is %d **\n",len);
                for(i=0;i<len;i++)
                {
                    printf("%c",int_vector[i]);
                } 
            }
            printf(" At last LEN = %d\n",len);

            //free the memory at the end
            free(int_vector);
            int_vector = NULL;
            close(fp);// better to as fd
        }
        else
        {
            printf("File error!\n");
            return (0);
        }
        
    }
    return(0);
}

int main()
{
    open_fp(0);
    return 0;
}
,

ehm,如果您也忘记设置realloc,下面是一些用于重新分配(动态调整大小缓冲区)的示例代码

    #include <stdio.h>
    #include <stdlib.h>
    
    int main () {
       char *str;
    
       /* Initial memory allocation */
       str = (char *) malloc(15);
       strcpy(str,"tutorialspoint");
       printf("String = %s,Address = %u\n",str,str);
    
       /* Reallocating memory */
       str = (char *) realloc(str,25);
       strcat(str,".com");
       printf("String = %s,str);
    
       free(str);
       
       return(0);
    }