Reputation: 83
I have to write a C program that somewhat function like dos2unix. Which replace all CR
LF
with only LF
(DOS-Format to Unix-Format).
So this is my way of approach. Everytime I read a line, I search for the end of the data by lookng for \0
and then check if the following are \r\n
.
If yes replace with \n
only. But it seems do not work and the line CRLF here
never been printed out once.
char data[255]; // save the data from in.txt
char *checker;
pf = fopen("in.txt", "r");
pf2 = fopen("out.txt", "w");
while (feof(pf) == 0)
{
fgets(data, 255, pf); // Read input data
checker = data;
while (checker != "\0") // Search for a new line
{
if (checker == "\r\n") // Check if this is CR LF
{
printf("CRLF here");
checker = "\n"; // replace with LF
}
checker++;
}
fputs(data, pf2); // Write to output data
}
Upvotes: 1
Views: 4993
Reputation: 140649
You have a whole bunch of bugs:
in.txt
in "rb"
mode, instead of "r"
mode, to see the CRLF line endings in the first place.out.txt
in "wb"
mode, instead of "w"
mode, to prevent the C library from undoing your work. ==
. You can compare one character of a string to a character literal with ==
, but that's not what you're doing, and it only works for single characters; a CRLF sequence is two characters.memmove
to shift all the characters after the replacement down one.fopen
succeeded, or for any other I/O errors.while (!feof (fp))
is always wrong.A better way to write this program is with a main loop that goes character by character, something like
int c;
while ((c = getc(ifp)) != EOF) {
if (c == '\r') {
putc('\n', ofp);
c = getc(ifp);
if (c == EOF) break;
if (c == '\n') continue;
}
putc(c, ofp);
}
This converts both \r\n
and bare \r
to \n
, because bare \r
is very rare nowadays, but was used as the line terminator on some historical OSes (notably classic MacOS), and there isn't anything else sensible to do with it.
It's important that c
be an int
, not a char
, so that it can hold EOF as well as all possible characters.
Upvotes: 5