473,654 Members | 3,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unix and Windows style CR LF in C program

18 New Member
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??
May 21 '10 #1
7 10652
Banfa
9,065 Recognized Expert Moderator Expert
Now if I run this on linux it creates linux format CR / LF file.
CR / LF is Windows format. Linux format is LF

http://en.wikipedia.org/wiki/Newline
May 21 '10 #2
gskbond
18 New Member
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
May 21 '10 #3
code green
1,726 Recognized Expert Top Contributor
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
Expand|Select|Wrap|Line Numbers
  1. if(unix) {endline = chr(10)}
  2. elseif(wind) {endline = chr(13)+chr(10)}
I have found in Windows you can usually get away with CR alone.
May 21 '10 #4
Banfa
9,065 Recognized Expert Moderator Expert
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?
May 21 '10 #5
gskbond
18 New Member
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.
May 21 '10 #6
Banfa
9,065 Recognized Expert Moderator Expert
Are you sure have you tried it on Windows with Linux output?
May 21 '10 #7
gskbond
18 New Member
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()).
May 21 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2408
by: nao921 | last post by:
Hi everyone, I am currently involved in a project that involves a windows client program written in delphi and a web application written in php. I have made several php pages for the delphi program to do requests via the http component from INDY. the problems I am having are: 1) the first request from delphi app to php web app is the authenication. In which user sends username + password to the php app. If authenication is valid, a...
4
5730
by: KB | last post by:
Hi, I want to write a Python script that controls and automates a Windows GUI computation program. My problem is that I do not know how to quit the Windows GUI program gracefully with a command (program's or Python) in Cygwin. 'kill' or CTRL-C command in Cygwin does not finish it gracefully, meaning that some outputs do not come out normally. The only thing I know is to click 'File-Exit' menu, but
1
2079
by: vck_82 | last post by:
HI Does any body know how to convert windows style path to posix style path. I need this because i am developing a windows application which actually starts an exe which is developed in unix. The exe only supports unix style of paths. So, i need to convert the path before sending the path to the exe. please help me if anybody know about it. thanks and regards
0
1235
by: Neil F | last post by:
Whould anyone pls like to help out a noobie to vb. I am looking for some sample code for a windows style calculator, as part of my teaching degree. Anyone know a good place to find this, on a students budget :/ Ta, Synchro
6
4030
by: Alice | last post by:
Hi -- I am trying to code a "mad libs" style program, in which the user puts in nouns, adjectives, etc., into text boxes. Then the user clicks on a button which opens a message box. Inside the message box is a story with blanks filled in by what the user entered into the text boxes. Only when my message box opens, it does not plug in the user's words. Can anyone tell me what I'm doing wrong, or does anyone
1
3012
by: MAL | last post by:
Hello, I have 2 classes that work great as a windows app to retrieve and process data from an Oracle9i db. When I implement them in a Service program running as Local System, it fails on the .open. I am using OLEDB for Oracle 9.2.0.4. I have verified the Oracle Client install, Local Sys' path variable, and followed MSKB article 259959(Debugging connectivity issues w/ Oracle). Here is the error:
0
3603
by: Rainer Lehrig | last post by:
Hello, we have build a library for our Open Source SCADA project http://pvbrowser.org The library supports Linux/Unix, Windows and OpenVMS. The doxygen documentation of the library can be found on: http://pvbrowser.de/pvbrowser/sf/manual/rllib/html/classes.html
4
2816
by: driplet | last post by:
Hi I have some exe files compiled from FORTRAN source codes and need to run them from a C++ windows application program so that I can monitor them and choose some parameters for them. Could any body please tell me how to make such a C++ program to run these exe files as simple as I just click a button? Thanks in advance!
2
1572
shek124
by: shek124 | last post by:
I want to run a windows startup program. here my code: Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, ByVal phkResult As Long) As Long Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String) As Long...
3
3117
by: sekhar.mekala | last post by:
Hello, I am a DB2 DBA on IBM Mainframe environment. I recently started learning DB2 on Unix/Windows environment. Can any one answer the following questions: 1. What is the qualifier that is used to identify tablespaces in DB2 UDB on Linux/Unix/Windows environmet? On z/OS, it is the database name that qualifies the tablespace names. 2. Is the DPF (Database Partitioning Feature) is just a paper based
0
8376
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.