I'm coming from the PHP world, and it's nice to see the similarities between C & PHP. But I have so many questions! :)

This is a simple fopen() script, easily found online.
What I am wondering about is the buffer variable. I think I understand that it's only looking for 256 characters on a given line.

#include < stdio.h >

int main(void)
{
    [B]char buffer[256];[/B]
    FILE * myfile;
    
    myfile = fopen("textfile.txt","r");
    
    while (!feof(myfile))
    {
        fgets(buffer,256,myfile);
        printf("%s",buffer);
    }
    
    fclose(myfile);
    
    return 0;
}

So, what happens if a text file has an unknown character count per line? Is there an unlimited buffer count option?

No. In C, you must allocate all arrays (buffers) before using them. There is no unlimited array. In the above program, when the length of the line exceeds 255 bytes, it stores the first 255 bytes followed by a null byte in the array. A subsequent call to fgets will store the remainder of the line in the array (or if the remainder exceeds 255 bytes, repeat).

The thing you are forgetting is that the concept of lines appear only in case of display to the terminal or in general just the display.While storing there is nothing called a separate line,it is just signified by the '\n' character which tells the display program to start printing in next line.
So it doesn't really matters whether you read the whole line (as how you see it ) or just 256 characters ( limiting to the buffer size ) because until the order of characters doesn't change and until there is the presence of '\n' character your alignment wont change. :)

And ya the unlimited buffer,there is nothing of that sort.You need to allocate the memory and then use it.

Don't use feof() to control a loop

Use while ( fgets(buffer,sizeof(buffer),myfile) != NULL )

Hi Salem,

I completely agree with your code snippet for checking EOF as C input functions return values that can be used to test for this condition.

However, you could still use the feof() function to control a loop if you use a priming read and move the input function call to the bottom of the loop. (Sorry, I know you know this - this is just an information "tidbit" for the OP).

fgets(buf, BUFSIZ, fp);
while(!feof(fp)) {
    fputs(buf, stdout);
    fgets(buf, BUFSIZ, fp);
}

I wouldn't do it like this in C for the reason I stated previously, but I have used this style in other languages where getting a hold of the return value from the input function was a tad "cumbersome" and coding the loop (especially when calling the input function at the top of the loop) became messy.

Cheers,
JD

commented: no -4

@yellowSnow
And if fgets() returns NULL because of an error (and not because of end of file), then what?

commented: Yup! +18

@yellowSnow
And if fgets() returns NULL because of an error (and not because of end of file), then what?

My oversight ... my apologies.

Cheers.

commented: N/P :) +36

You could, of course, roll your own function that provides that feature of unlimited buffer length

#include <stdlib.h>
#include <string.h>
#pragma warning(disable: 4996)

#define TRUE 1
#define FALSE 0

int getline(char** buf, FILE* fp)
{
    const int BLOCKSIZE = 10;
    int c;
    int size = 0;
    int maxsize = 0;
    if( *buf != NULL)
    {
        free(*buf);
        *buf = NULL;
    }

    maxsize = BLOCKSIZE;
    *buf = realloc(*buf, maxsize);
    memset(*buf, 0, BLOCKSIZE);
    do
    {
        c = fgetc(fp);
        if( c != EOF)
        {
            // check of we need to allocate more memory
            if( size == (maxsize-1) )
            {
                maxsize += BLOCKSIZE;
                *buf = realloc(*buf, maxsize);
            }
            (*buf)[size] = c;
            ++size;
        }
    }while( c != '\n' && c != EOF);
   (*buf)[size] = 0; // null terminate the string
   return  (c == EOF) ? FALSE : TRUE;


}

int main()
{
    char* buf = 0;
    FILE* fp = fopen("test2.c", "r");
    while( getline(&buf, fp) )
        fputs(buf, stdout);
    fclose(fp);
    free(buf);

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.