Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Program to create csv file and write into it Date: Sun, 11 Dec 2016 01:19:00 -0800 Organization: None to speak of Lines: 64 Message-ID: References: <6b201709-fae0-4a06-9783-af485e4040bf@googlegroups.com> <3a3c9153-6813-4eb1-993a-db94b41272ac@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="d0d5c56ca22fa2e528cedb366e6a556f"; logging-data="5581"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/gr20HF6vZbuJfXDk7WvML" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:yvkl3s34zMYhv3AbCUmui2nnusQ= sha1:ESVZrXE30DBgejFTi2vtzqJSNJo= Xref: csiph.com comp.lang.c:98768 Malcolm McLean writes: > On Sunday, December 11, 2016 at 7:57:06 AM UTC, Alla _ wrote: >> I love this word "just" ) I am sorry, but for me "just write >> out the header doesn't ring a bell at all. And how can I escape >> strings, if I have to have names of fields? It seems I also have >> yet no idea how to write alpha words not in a string variable. >> > We have > > struct employee > { > char *fname; > char *sname; > long payrollid; > float salary; > } > > We want a CSV file that looks like this > > First name, Surname, ID, Salary > "Fred", "Bloggs", 1234567, 32000.00 > "Joe", "Sixpack", 2345678, 29000.00 > "Dilbert", "Smith", 7654321, 45000,00 No, a CSV file normally doesn't have spaces after the commas, unless the spaces are part of the fields. (And you have a typo on the last line.) I'd expect that to look like: First name,Surname,ID,Salary Fred,Bloggs,1234567,32000.00 Joe,Sixpack,2345678,29000.00 Dilbert,Smith,7654321,45000.00 > You have to know what names you will give the columns. > Whilst they will bear a relation to the struct field > names, it's unlikely they will be all identical. > > Then the names have to be wrapped in quotes, and the > salaries formatted so that there are exactly two figures > after the decimal place. Why do the names have to be wrapped in quotes? CSV formats require quotes only for fields that contain special characters, particularly embedded commas. Why do the salaries in the CSV file have to have exactly two digits after the decimal? All that should really matter is that the correct value is represented. > It gets harder if commas or other punctuation is allowed > in names. That's what "escaping the string" means. However > most databases disallow commas in people's names. I find that difficult to believe. Is "John Smith, Junior" just out of luck? A database should make as few assumptions as possible about people's names. You might want to take a look at this: https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"