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
}