C ++ is a flexible programming language for applications. We can compile a function suitable for our own development to implement some specific functions based on our own needs. For example, we will introduce a user-defined function to implement the function of reading CSV files in C ++.
- Analysis of concepts related to C ++ static Constructor
- C ++ Traits skills
- Interpretation of c ++ virtual function examples
- What is the relationship between the C ++ function pointer and the C # Delegate?
- The C ++ explicit keyword is applied to the constructor.
Today, we just need a function for C ++ to read CSV files.
No ready-made functions are found, so I wrote one myself.
The objective is to implement C ++ to read CSV files and read each row into an array.
A little unfamiliar, the string processing functions do not know what is ready-made, so they also write two auxiliary string processing functions.
First function: calculate the number of substrings in a string.
- int strstr_cnt(const char *string, const char
*substring) {
- int i,j,k,count = 0;
- for (i = 0; string[i]; i++){
- for (j = i, k = 0; (string[j] ==
substring[k] && (j < strlen(string))); j++,k++) {
- if (! substring[k + 1]) {
- count++;
- }
- }
- }
- return count;
- }
Second function: Calculate the position of a substring in the string.
- int substring_index(const char *s1,const char *s2, int pos){
- int i,j,k;
- for( i = pos ; s1[i] ; i++ ) {
- for( j = i, k = 0 ; s1[j] == s2[k]; j++,k++ ){
- if (! s2[k + 1]) {
- return i;
- }
- }
- }
- return -1;
- }
Function 3: Read a row of a opened CSV file and process the row into an array.
- char *fgetcsvline(vector<string> &csv_databuf, FILE *fhead) {
- char *ret_stat;
- char data_buf[1024];
- string stringbuf;
- ret_stat = fgets( data_buf, 1024, fhead );
- if (ret_stat != NULL) {
- int len = strstr_cnt(data_buf,"\",\"");
- if (len > 0){
- int pos = substring_index(data_buf,"\",\"",0);
- int startpos = 1;
- string csv_buf;
- while (pos > 0) {
- stringbuf = (string)data_buf;
- csv_buf = stringbuf.substr(startpos,pos - startpos);
- csv_databuf.push_back(csv_buf);
- startpos = pos + 3;
- pos = substring_index(data_buf,"\",\"",pos + 2);
- }
- if ((substring_index(data_buf,"\n",0)) > 0){
- csv_buf = stringbuf.substr(startpos, stringbuf.length()
- startpos - 2);
- } else {
- csv_buf = stringbuf.substr(startpos, stringbuf.length()
- startpos - 1);
- }
- csv_databuf.push_back(csv_buf);
- }
- }
- return ret_stat;
- }
This function uses the above two functions to process strings.
In addition, the CSV file to be processed by this function is in double quotation marks format:
- "sss","ddd","444"
- "ttt","www","ooo"
- "sss","qqq","000"
The usage is as follows:
- int main(int argc, char* argv[]) {
- FILE *fp_head;
- string csvFileName = "test.csv";
- char *ret_stat;
- vector<string> csv_data;
- fp_head = fopen( csvFileName, "rt" );
- ret_stat = fgetcsvline(csv_data, fp_head);
- while (ret_stat != NULL) {
- //get csv data use csv_data[n]
- ret_stat = fgetcsvline(csv_data, fp_head);
- }
- return 0;
- }
The above code may require a little debugging. You can also slightly modify it to read csv files in other formats.
The above is how C ++ reads CSV files.