C + + reads the CSV file and divides the comma separator file into n arrays

Source: Internet
Author: User
Tags cdata
#include <stdio.h>
#include <string.h>
#include <windows.h>


#define MAXLINE 256


struct student
{
Char stuname[100];//name
Char f[11][10];


};


Custom data types
typedef struct student record;
Structure Body Array
Record rs[10000] = {0};


void Spit (int max);




Reading files
int main ()
{






int length,i,j,k;//per line data length \ structure subscript \ member variable number \ Loop variable
Char clinestr[255],cdata[80],ctmp[2];//per line file content \ read from file member variable content \ value of single character per row of data


file* FP;


Initializing variables
j = 0;
i = 0;
memset (CDATA, 0, sizeof (CDATA));
memset (clinestr, 0, sizeof (CLINESTR));
memset (&rs, 0, sizeof (record));


Fflush (stdin);


Reading files
fp = fopen ("D:\\exat.csv", "R");
if (fp = NULL)
{
return-1;
}


Reading data from one line in a file into an array of structures
while ((!feof (FP)) && ((Fgets (CLINESTR,MAXLINE,FP))!= NULL)
{
Length = strlen (CLINESTR);
for (k = 0; k < length; k++)
{
memset (cTmp, 0, sizeof (CTMP));
memcpy (CTMP, &clinestr[k],1);
Separated by commas among the members of the file
if (ctmp[0]!= ' \ n ')
{
strcat (CDATA,CTMP);
}
Place the contents of the member variable read from the file into the data file record structure body array
Else
{
if (j = = 0)
{
strcpy (Rs[i].stuname,cdata);
}

memset (CDATA, 0, sizeof (CDATA));
j + +;
if (j = = 1)
{
Break
}
}
}//end for
j = 0;
i++;

}//end while
Fclose (FP);
Returns the data file record structure body array maximum subscript

i--;
Spit (i);
return i;


}


void Spit (int max)
{
int Len;
int m=0,n=0;
int num=0;


for (num=0;num<=max;num++)
{
Len = strlen (rs[num].stuname);


for (int i = 0; I<len i++)
{
if (rs[num].stuname[i]!= ', ')
{
Rs[num].f[m][n] = Rs[num].stuname[i];
n++;
}
Else
{
m++;
n=0;
}
}
m=0;
n=0;
}
}
Related Article

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.