Zenek
Zenek

Reputation: 120

Read a file using "read()" function

i'm studying C, and I need to read a text-file, but I can only use "write, malloc, free, open, read, close".

That's my code:

#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);
}

Now my question is: As you can read here,

 char buffer[MAXCHAR];

i've created static buffer, but i would like create a dynamic buffer which allow me to resize the buffer according to the number of the chars in the text file, but i don't know how... someone have a trick😥😥 ?

Upvotes: 0

Views: 567

Answers (2)

csavvy
csavvy

Reputation: 821

First of all your way of allocating memory is wrong in below line.

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

correct that line as below

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

and as far as your question is concerned, you dont need to reallocate memory in this particular case because you are just reading the bytes to buffer and printing.

a single malloc will suffice.

#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;
}

Upvotes: 1

niek tuytel
niek tuytel

Reputation: 1179

ehm, If you forget to set realloc to it as well, here is some sample code for reallocation (dynamic resizing buffer)

    #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);
    }    

Upvotes: 0

Related Questions