Custom functions enable C ++ to read CSV files

Source: Internet
Author: User
Tags function examples

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.

 
 
  1. int strstr_cnt(const char *string, const char 
    *substring) {  
  2. int i,j,k,count = 0;  
  3. for (i = 0; string[i]; i++){  
  4. for (j = i, k = 0; (string[j] == 
    substring[k] && (j < strlen(string))); j++,k++) {  
  5. if (! substring[k + 1]) {  
  6. count++;  
  7. }  
  8. }  
  9. }  
  10. return count;  

Second function: Calculate the position of a substring in the string.

 
 
  1. int substring_index(const char *s1,const char *s2, int pos){  
  2. int i,j,k;  
  3. for( i = pos ; s1[i] ; i++ ) {  
  4. for( j = i, k = 0 ; s1[j] == s2[k]; j++,k++ ){  
  5. if (! s2[k + 1]) {  
  6. return i;  
  7. }  
  8. }  
  9. }  
  10. return -1;  

Function 3: Read a row of a opened CSV file and process the row into an array.

 
 
  1. char *fgetcsvline(vector<string> &csv_databuf, FILE *fhead) {  
  2. char *ret_stat;  
  3. char data_buf[1024];  
  4. string stringbuf;  
  5. ret_stat = fgets( data_buf, 1024, fhead );  
  6. if (ret_stat != NULL) {  
  7. int len = strstr_cnt(data_buf,"\",\"");  
  8. if (len > 0){  
  9. int pos = substring_index(data_buf,"\",\"",0);  
  10. int startpos = 1;  
  11. string csv_buf;  
  12. while (pos > 0) {  
  13. stringbuf = (string)data_buf;  
  14. csv_buf = stringbuf.substr(startpos,pos - startpos);  
  15. csv_databuf.push_back(csv_buf);  
  16. startpos = pos + 3;  
  17. pos = substring_index(data_buf,"\",\"",pos + 2);  
  18. }  
  19. if ((substring_index(data_buf,"\n",0)) > 0){  
  20. csv_buf = stringbuf.substr(startpos, stringbuf.length() 
    - startpos - 2);  
  21. } else {  
  22. csv_buf = stringbuf.substr(startpos, stringbuf.length() 
    - startpos - 1);  
  23. }  
  24. csv_databuf.push_back(csv_buf);  
  25. }  
  26. }  
  27. 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:

 
 
  1. "sss","ddd","444"  
  2. "ttt","www","ooo"  
  3. "sss","qqq","000" 

The usage is as follows:

 
 
  1. int main(int argc, char* argv[]) {  
  2. FILE *fp_head;  
  3. string csvFileName = "test.csv";  
  4. char *ret_stat;  
  5. vector<string> csv_data;  
  6. fp_head = fopen( csvFileName, "rt" );  
  7. ret_stat = fgetcsvline(csv_data, fp_head);  
  8. while (ret_stat != NULL) {  
  9. //get csv data use csv_data[n]  
  10. ret_stat = fgetcsvline(csv_data, fp_head);  
  11. }  
  12. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.