DOS text file to unix file

Discussion in 'C Programming' started by Money, Nov 23, 2006.

  1. Money

    Money Guest

    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;
    }
    Money, Nov 23, 2006
    #1
    1. Advertising

  2. "Money" <> wrote in message
    news:...
    >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.
    David T. Ashley, Nov 23, 2006
    #2
    1. Advertising

  3. David T. Ashley wrote:

    <snip>

    > 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.


    --
    Nick Keighley
    Nick Keighley, Nov 23, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Moore

    Dos vs Unix style text files

    Dave Moore, Feb 10, 2005, in forum: C++
    Replies:
    8
    Views:
    6,665
    Ron Natalie
    Feb 12, 2005
  2. Aki Niimura
    Replies:
    12
    Views:
    605
    Nick Coghlan
    Jan 15, 2005
  3. walterbyrd
    Replies:
    13
    Views:
    1,297
    walterbyrd
    May 13, 2009
  4. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    277
    Michele Dondi
    Jan 22, 2004
  5. January Weiner

    Reading Mac / Unix / DOS text files

    January Weiner, Feb 21, 2006, in forum: Perl Misc
    Replies:
    23
    Views:
    309
    January Weiner
    Feb 27, 2006
Loading...

Share This Page