使用 "read()" 函数读取文件

Read a file using "read()" function

我正在学习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];

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

嗯,如果你也忘记设置 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,  Address = %u\n", str, str);
    
       free(str);
       
       return(0);
    }    

首先你分配内存的方式在下一行是错误的。

//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 [=11=] 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;
}