DOS text file to unix file

M

Money

I am doing things the right way or does it needs modification?

#include <stdio.h>
#define MS_DOS_EOF 26
#define MS_DOS_CR 13

int main(int argc, char *argv[])
{
int ch;
FILE *in, *out;
in = fopen(argv[1],"rb");
out = fopen(argv[2],"wb");
while (1)
{
ch = fgetc(in);
switch(ch)
{
case EOF:
case MS_DOS_EOF:
fclose(in);
fclose(out);
exit(0);

case MS_DOS_CR:
break;

default:
fputc(ch, out);
break;
}
}
return 0;
}
 
D

David T. Ashley

Money said:
I am doing things the right way or does it needs modification?

#include <stdio.h>
#define MS_DOS_EOF 26
#define MS_DOS_CR 13

int main(int argc, char *argv[])
{
int ch;
FILE *in, *out;
in = fopen(argv[1],"rb");
out = fopen(argv[2],"wb");
while (1)
{
ch = fgetc(in);
switch(ch)
{
case EOF:
case MS_DOS_EOF:
fclose(in);
fclose(out);
exit(0);

case MS_DOS_CR:
break;

default:
fputc(ch, out);
break;
}
}
return 0;
}

Comments:

a)If you're running this on a Unix system, look up the dos2unix utility --
already exists and seems to work fine.

b)Given that the standard Windows line termination is 13-10 and Unix is 10,
your program should work.

c)You might want to look carefully at the last-line-of-the-file cases. I'm
not sure of Unix convention--whether that gets a terminator or not, and
whether this convention is the same for Windows.
 
N

Nick Keighley

David T. Ashley wrote:

a)If you're running this on a Unix system, look up the dos2unix utility --
already exists and seems to work fine.

the version I used annoyingly modified the file protection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top