Unix and Windows style CR LF in C program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gskbond
    New Member
    • Oct 2009
    • 18

    Unix and Windows style CR LF in C program

    I have a c program create_row which creates a database row using rand function.

    I am redirecting its output to csv file

    (./create_row param1 param2 param3 param4 > row.csv )

    I have a printf inside program:

    printf("%d,%s,% s,%s,,%s,%s,%05 d,%s,%1d,%s,%10 .0f,%1d,%16.0f, %s,%s,%s,%d,%d, %s\n",
    customerid, firstname, lastname, address, city, state, zip, country, region, email, phone, creditcard_type ,
    creditcard, creditcard_exp, username, password, age, income, gender);

    Now if I run this on linux it creates linux format CR / LF file.

    I do not want to use utilities in linux like dos2unix, unix2dos, perl, sed ,awk since I have to explicitly generate files first and then convert them to DOS format.

    I want to achieve this in C program only.

    Is there a way to replace \n by other character so that I get required format.

    ( My idea is to add other command argument to C program to indicate system_type (linux / win) and this printf will append correct characters acc to system and will generate files in correct format so that they need not be converted.

    Do anybody have any idea how this can be achieved??
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Now if I run this on linux it creates linux format CR / LF file.
    CR / LF is Windows format. Linux format is LF

    Comment

    • gskbond
      New Member
      • Oct 2009
      • 18

      #3
      Ohh yes sorry my mistake....But the problem is I do not want to use any other utility to convert....

      I want my C program to handle it

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        CR and LF both have ASCII codes (13) AND (10)
        If you can check the OS as you say, then append to the string depending on the OS
        Code:
        if(unix) {endline = chr(10)}
        elseif(wind) {endline = chr(13)+chr(10)}
        I have found in Windows you can usually get away with CR alone.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Not notepad :D

          Lots of Linux apps handle Windows CR / LF OK too.

          You could output "\r\n" on Linux, that wont might not work on Windows though, or you might have to explicitly write to the file, if your printfing presumably you are piping to a file at the command line?

          Comment

          • gskbond
            New Member
            • Oct 2009
            • 18

            #6
            Hello all,

            Thanks I just passed a new parameter to C program (0/1 ) indicating system is either win / linux

            If system is win

            In printf I use \r\n

            For Linux I use \n only in printf

            Now regardless of whichever system I run C program on and pipe data to files I am getting correct files in correct format as that of target system.

            We can simply avoid using perl or dos2unix commands and use this simple way to avoid creating and converting huge files to required formats.

            Thanks all for your suggestions.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Are you sure have you tried it on Windows with Linux output?

              Comment

              • gskbond
                New Member
                • Oct 2009
                • 18

                #8
                Ok here is what I am doing

                I have written a perl script which takes input from user about on which system is the database server is installed as a parameter Sys_Type

                I m executing the compiled C binary by passing this parameter to C program and redirecting its output to csv file

                So if user is executing perl script on windows system and gives linux as sys_type (since he prb has mysql DB on linux), it will be fine since C code will execute printf for linux with (\n only) and this output will be redirected to csv file.

                So in any scenario, correct CSV file will be output for a particular system on which bulk loading of database will be done using these files.

                I have checked this for following scenarios:

                1. Execute perl script on linux, pass sys_type = windows and copy paste files in windows and bulk load database(SQL Server) on windows

                2. Execute perl script on linux, pass sys_type = linux and bulk load database (MySQL) on MySQL.

                Both bulk loads are executing correctly.

                Let me know if I am missing any other scenario.

                ( I know I need to check for one scenario: Execute perl script on windows and vary parameter sys_type....I will be doing it soon ...Since I would have to figure out how to call a compiled .exe file (executable in windows) from perl script....actua lly I m getting some error in executing exe file through perl using system()).

                Comment

                Working...