Its me
Its me

Reputation: 121

fgets that doesn't stop at spaces or linebreaks

I'm writing a user space program and I want to send a text file content to the kernel module. The text file isnt to long so instead of sending line by line I rather send all the text in one string.

FILE *rules_text_file;
char *rules = (char *)malloc(sizeof(50*256*sizeof(char))); 
....
if (fgets(rules, 100, rules_text_file) == NULL) {
    return -1;
}
printf("%s\n",rules);  
          
fclose(rule_dev_file);
fclose(rules_text_file);
free(rules);
return 0;

I noticed that fget break at space and line break. Is there another function that reads text file and save it in a string I can use?

The text file len is below 50*256 char.

Upvotes: 1

Views: 145

Answers (2)

mcendu
mcendu

Reputation: 439

Obviously you are not regarding any line breaks or other breaks, so as comment says:

If you don't care about lines, use fread() rather than fgets().

The code is as follows:

char *rules = malloc(sizeof(50*256*sizeof(char))); 
/* ... */
memset(rules, 0, 50 * 256);
errno = 0;
/* read one byte less to make string functions happy */
size_t len = fread(rules, 1, 50*256 - 1, rules_text_file)
if (errno) {
    return -1;
}

Upvotes: 2

greg spears
greg spears

Reputation: 1394

The best fgets() is one you write yourself. I too grew weary of fgets() limits and such. For example, at one time I was reading lots of files and always had to add code to remove the newline ( '\n' ) character from the buffer after each read.

Finally, I wrote this code which stops reading at the newline ( like std lib fgets() ) -- but does not store the newline char into the buffer.

If you don't want this code to break/stop reading at the newline character, then just comment out the line below done_reading = 1; which appears at switch() case '\n':. Of course, you can edit this code endlessly to make it behave however you wish -- and that's the beauty of C and coding in general.

Best wishes and happy coding.

/*-------------------------------------------------------------------------
char *myfgets(buffer, count, stream) - input string from a stream

Purpose: 
    Read a string from the input stream argument and store it in
    buffer. myfgets() reads characters from the current stream position to 
    and including the first newline character ('\n'), to the end of the 
    stream, or until the number of characters read is equal to count - 1, 
    whichever comes first. 

    The result stored in buffer is appended with a NUL ('\0') character. 
    The newline character('\n'), if read, is NOT included in the buffer.
    The carriage return('\r'), if read, is also NOT included in the buffer.

Entry:
       char *buffer- pointer to place to store bytes
       int count - max characters to place in buffer
       FILE *fp - stream to read from

Exit:
       returns buffer with text read from file.
       if count <= 0 return NULL
       returns NULL if error or end-of-file found immediately

*------------------------------------------------------------------------*/
char *myfgets (char *buff, int count, FILE *fp )
{
char *ptr = buff;
int ch, done_reading = 0; 

    if(!buff || !fp || count < 1)
        return NULL;

    while (count-- > 1 && !done_reading)
    {
        ch = fgetc(fp);

        switch(ch)
        {
            /* Carriage return: 0x0D   Linefeed: 0x0A
            ** In MSDOS/Windows each line ends with sequence 0D,0A (\r,\n); */
            case '\n':      /* Never accumulate linefeed into the buffer ... */
                done_reading = 1;  /* Comment out this line and reading will not stop here! */
            case '\r':      /* Never accumulate carriage return into the buffer.*/
            break;

            case EOF:
                if (ptr == buff)
                    return NULL;
                done_reading = 1;

            default:
               *ptr++ = (char) ch;
            break;
        }
    }
    *ptr = '\0'; /* cap it */
    return(buff);
}

Upvotes: 1

Related Questions