Thread: Made a program to find either POSIX new lines or Windows newlines

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    93

    Made a program to find either POSIX new lines or Windows newlines

    I've been playing with Cygwin and C lately. I made this program to find new lines depending on what system they were created on.

    Eventually, I'm wanting to create a converter to convert text files between POSIX and Windows.

    Yes, I know one already exists but I want to try it myself.

    Here's the code:

    Code:
    #include <stdio.h>
    #include <stdint.h>
    #include <string.h>
    
    
    int main(int argc, char **argv) {
        int8_t FileData;
        uint8_t WinNewLine;
        FILE *fp = NULL;
        
        if(argc != 2) {
            fprintf(stderr, "Enter one file.\n");
            return 1;
        }
        
        fp = fopen(argv[1], "r");
        
        if(fp == NULL) {
            fprintf(stderr, "No file found.\n");
            return 1;
        }
        
        FileData = getc(fp);
        
        while(1) {
            WinNewLine = 0;
            
            if(FileData == '\r') {
                FileData = getc(fp);
                
                if(FileData == '\n') {
                    printf("Found a new line!\n");
                    WinNewLine = 1;
                }
            }
            
            if(FileData == '\n' && WinNewLine == 0)
                printf("Found POSIX new line!\n");
            
            FileData = getc(fp);
            
            if(feof (fp))
                break;
            }
        return 0;
        }
    Last edited by ImageJPEG; 11-13-2017 at 09:16 PM.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    In the case of fread() used with fopen() with Windows, unless you open with "rb" (read binary mode), fread() will skip return "\r" characters, but I'm not sure about getc(). I'm not sure about all C libraries though. It won't hurt to have the check for "\r" either, other than the overhead.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    getc() is a macro for a specific call to fread() probably. It would behoove the OP to check his implementation.

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    I'm a little lost.

    Are you guys suggesting to implement fread() instead of getc()?

    I'm a little lost with "...fread() will skip return "\r" characters...". I mean I think I understand what you're saying but not entirely.

    So fread() will just skip over '\r' characters then? How can I determine if it's a new line under POSIX or Windows?

    I'm assuming one of the main differences (and why Notepad doesn't work well with POSIX text files) with how text files are handled are how newlines are implemented in the either system?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    To make it cross platform, you will have to open the file in "rb" mode and specifically check for "\r\n" (windows newline) or just '\n' (posix newline). You won't know the difference when the file is opened in "r" mode. I think windows just does the right thing for windows in that case.

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    If I open it in binary, I would need to search for 0x0D0A for Windows and just 0x0A for POSIX?

    I'm thinking too much into it, aren't I?

    https://www.cisco.com/c/en/us/td/doc...n_r/frf019.pdf

    Use fread for binary mode though, right? getc won't get me anywhere since it wont be read as a text file.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    getc()/fgetc()/getchar() return an 'int', so you should save the return value in an 'int' variable. An added bonus (actually, a very common practice) is that you can check if you reached the end of the file (or encountered an I/O error) by comparing that value with 'EOF'. Then you don't have to call feof() to see if you reached the end of the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 06-22-2011, 07:07 AM
  2. Windows 7: Program unable to find glut/glfw dll?
    By Jake.c in forum Game Programming
    Replies: 1
    Last Post: 10-29-2009, 03:59 PM
  3. POSIX programming in Windows?
    By jw232 in forum Windows Programming
    Replies: 1
    Last Post: 10-25-2009, 09:03 AM
  4. Windows return error when input is made
    By newbie1234 in forum C Programming
    Replies: 8
    Last Post: 05-25-2006, 06:16 AM
  5. POSIX on windows anyone?
    By Lynux-Penguin in forum Linux Programming
    Replies: 1
    Last Post: 08-27-2003, 12:56 AM

Tags for this Thread