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;
}