Thread: EOL characters

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    EOL characters

    Is there any way to get the number of characters that are used to terminate the line when using fputs (for example)?

    Though I'd except this is possible, I could find anything in a C reference or on Google.

    An alternative is using fgetpos(). Would this cause a performance hit?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by exareth View Post
    Is there any way to get the number of characters that are used to terminate the line when using fputs (for example)?

    Though I'd except this is possible, I could find anything in a C reference or on Google.

    An alternative is using fgetpos(). Would this cause a performance hit?
    open file in text mode for writing, fputs the empty string
    close file - open it in the binary mode for reading - check how many chars you can read from it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        int eol_len = 0;
        FILE *fp;
        if((fp = fopen("/tmp/eolchartest", "wt")) == NULL) ; /* should print an error and exit here . . . use a random name as well */
        fputs("\n", fp);
        fclose(fp);
        if((fp = fopen("/tmp/eolchartest", "rb")) == NULL) ; /* should print an error and exit here . . . use the above random name as well */
        while(fgetc(fp) != EOF) eol_len ++;
        fclose(fp);
        printf("eol_len: %i\n", eol_len);
        return 0;
    }
    This works. Gives 1 for UNIX-style EOL chars, 2 for DOS-style EOLs.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM