C function to export text from a ListView into a text file with separators

Introduction

You can find plenty of C++ and/or MFC code to deal with your ListView controls, but maybe not enough pure C and API (SDK) code. This code snippet consists of only one C function (fExportListView) that outputs the text content of a ListView control into a newly created text file (with fields separated). The output text file can be imported into a spreadsheet software such as MS Excel, or into a database. Any reader interested in thorough explanations (only SDK, no MFC) can read the excellent article by Bengi (titled 'Using ListView control under Win32 API').

This is a first attempt at proposing C and API code - maybe completely useless? However, if you are interested in simple 'insert ListView item' or 'insert ListView column' functions, just let me know and I will upload those too, if they are needed.

Background

You should be familiar with C language and C program compiling, including Win32 API. Whether you use MFC or not, you should know about API. Therefore, reading Charles Petzold's book "Programming Windows" (published by Microsoft Press) is quite mandatory. By the way, thank you so much for your explanations and inspiring examples, Mr. Petzold.

Using the code

I assume you have declared, created, (re)sized and filled (with text) a ListView control (i.e., a window of class WC_LISTVIEW, with style, e.g., WS_BORDER | WS_CHILD | WS_VISIBLE | LVS_REPORT). See reference on CreateWindowEx in MSDN, and/or include the ListView in your Visual Studio project.

Bear in mind that in a ListView, you deal with columns, items and subitems. One of the columns contains only items (usually, the leftmost column), all the other columns contain only subitems. So, for a given 'line' in your ListView, the LVM_GETITEMTEXT message will copy text from:

an item when the iSubItem member of the LVITEMstruct equals zero, or

from a subitem when iSubItem is greater than zero.

See the code below and try it out.

The function is really simple, and comes with embedded comments explaining what it does step by step. However, here are some elements:

1. Import your text file with separators into strings (e.g. fgetc's into char*** with the corresponding calloc's or using a 'string' class). Well, that part of my code toolbox is quite obfuscated and out of CodeProject's scope, so I probably won't upload it anytime soon...
2. Importing strings (char* that is) into the ListView is relatively easy. I am currently preparing (translating comments and variable names into English) fInsertListViewColumn and fInsertListViewItem (item or subitem) and will upload them next week if I can find some time .