Thread: Thanks to OCD, I programed a new line remover for text files. Needs some work still.

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

    Thanks to OCD, I programed a new line remover for text files. Needs some work still.

    So I've replaced '\r' and '\n' with '\0'. This, however, just fills up the file with 0s instead of just removing the contents all together. I would like to just fill the '\n' with nothing, not even zeros. No, the zeros don't show up on an average text editor, however, they do show as ^@ on vim and on a hex editor they show up as binary 0s.

    The reason why I did this is because I've been creating SSH keys to log into my servers. When transferring the public keys via macOS or another *nix system, it would send the key as a one line text file.

    Windows on the other hand did not. This annoyed me and I wanted to make something to remove all the new lines.

    This is what I came up with.

    Here's the source:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(int argc, const char *argv[]) {
        unsigned long long fileSize, programCounter;
        char *fileData;
        FILE *fp;
        
        fileSize = 0;
        fileData = NULL;
        fp = NULL;
        
        if(argc != 2) {
            fprintf(stderr, "Enter only one file, please.\n");
            return 1;
        }
        fp = fopen(argv[1], "r");
        if(fp == NULL) {
            fprintf(stderr, "Could not open file: %s.\n", argv[1]);
            return 1;
        }
        fseek(fp, 0L, SEEK_END);
        fileSize = ftell(fp);
        rewind(fp);
        
        fileData = malloc(fileSize);
        if(fileData == NULL) {
            fprintf(stderr, "Could not allocate memory.\n");
            return 1;
        }
        
        fread(fileData, fileSize, 1, fp);
        
        fclose(fp);
        
        for(programCounter = 0; programCounter < fileSize; programCounter++) {
            if(fileData[programCounter] == '\r')
                fileData[programCounter] = '\0';
            
            if(fileData[programCounter] == '\n')
                fileData[programCounter] = '\0';
        }
        
        fp = fopen(argv[1], "w");
        fwrite(fileData, fileSize, 1, fp);
        fclose(fp);
        
        free(fileData);
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're reading and writing in binary, not text. In this context, '\0' does not behave as a string-terminating character. Hence it is printing the actual binary value of '\0'.

    Incidentally, when using fread/fwrite, it is more appropriate to open files in binary mode ("rb", "wb").

    Reading and writing your files as text (using fgets and fputs instead) might be more appropriate.
    Last edited by Matticus; 12-29-2017 at 11:27 AM.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    As a simple filter:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char line[1000];
        while (fgets(line, sizeof line, stdin) != NULL) {
            char *p;
            if ((p = strchr(line, '\r')) != NULL)
                *p = '\0';
            else if ((p = strchr(line, '\n')) != NULL)
                *p = '\0';
            fputs(line, stdout);
        }
        return 0;
    }
    Shorter:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char line[1000];
        while (fgets(line, sizeof line, stdin) != NULL) {
            line[strcspn(line, "\r\n")] = '\0';
            fputs(line, stdout);
        }
        return 0;
    }
    Last edited by john.c; 12-29-2017 at 11:52 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-11-2011, 10:32 PM
  2. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  3. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  4. Detecting end of LINE in text files
    By kiknewbie in forum C Programming
    Replies: 6
    Last Post: 12-09-2008, 09:34 AM
  5. Reading text files form a certain line???
    By Agent89 in forum C++ Programming
    Replies: 4
    Last Post: 05-24-2002, 11:40 AM

Tags for this Thread