R
Ram Laxman
Iam new bie to C++ programming..
I want to write a program which will read the Comma separated
values(CSV) file column wise.
For example:
In a.txt:
"TicketNumber","Phone","CarNumber"
10,20,30
40,45,80
60,70,34
I want to read the values like the following:
"TicketNumber",
10
40
60
and want to store in the values {10,40,60 } in an array.
When Iam compiling the below program in VC++ 6.0 i get the following
error:
d:\vs6\vc98\include\vector(48) : warning C4786:
'??0?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QAE@IABV?$basic_string@DU?$char_traits@D@s
C:\test.cpp(269) : error C2059: syntax error : 'constant'
C:\test.cpp(271) : error C2065: 'status' : undeclared identifier
C:\test.cpp(288) : fatal error C1020: unexpected #endif
Error executing cl.exe.
test.obj - 12 error(s), 6 warning(s)
Could anyone help?
Regards
Ram Laxman
#include<fstream>
#include<ios>
#include <iostream>
#include<string>
#include<iomanip>
#include<sstream>
#include<vector>
#include<algorithm>
#include<stdio.h>
#include <fcntl.h>
#include <io.h>
#include <cstdlib>
#include <cstring>
using namespace std;
const char *filename ="C:\\result.txt";
#define TRUE 1
#define FALSE 0
class test
{
public:
test(ifstream& fin = ifs) :
fin(ifs) {}
// test(ifstream& fin = filename) :
// fin(filename) { }
string read_file_header(string &str1, ifstream ifs);
int checkofcomma(char c);
int checknextquote(string &line,string &fld,int i);
int checkofeol(char c,ifstream &ifs);
int commacount(char c,ifstream &ifs);
int getline(ifstream &ifs,string &line);
bool parseline(string &str,string &line);
/* string getfield(int n);
int getnfield() const { return nfield; } */
private:
ifstream& ifs; // input file pointer
string line; // input line
vector<string> field; // field strings
// int nfield; // number of fields
// string fieldsep; // separator characters
};
/* Read the Column Header from the file */
string test:: read_column_header(string &str1, ifstream ifs)
{
char *pdest;
int pos;
char lineone[256];
if (!ifs.eof())
{
ifs.getline(lineone,sizeof(lineone),'\n');
pdest =strstr(lineone,str1);
while (pdest == 0)
{
ifs.getline(lineone,sizeof(lineone),'\n');
pdest = strstr(lineone,str1);
}
pos = pdest - lineone + str1.length();//end of Test:
string s = lineone;
string s1 = s.substr(0,pos); /* Return the SubString if the
matches found */
return s1;
}
else
{
return "EOF";
}
}
/* Check for \r or the \n in the line which was get by stream */
int test:: checkofeol(char c,ifstream &ifs)
{
int eol;
eol= (c=='\r' || c== '\n'|| c== ',') ;
if(c=='\t')
{
// continue; /* ignore tab spaces in the line */
}
if(c == '\r')
{
ifs.get(c);
if(!ifs.eof() && c != '\n')
ifs.putback(c);
}
return eol;
}
/* Count the number of comma in the line */
int test:: commacount(char c,ifstream &ifs)
{
int count=0;
for(line == "";ifs.get(c) && !checkofeol(c,ifs)
{
if(c==',')
{
count++;
}
}
return count;
}
/* Get Next Quote and return the index of next quote */
int test:: checknextquote(string &line,string &fld,int i)
{
int j;
fld ="";
for(j=i;j<line.length();j++)
{
if(line[j] == '"' && line[++j] != '"')
{
int k =line.find_first_of('"',j);
if(k>line.length())
k=line.length();
/* Store the String in an array */
for(k=k-j;k-- >0
fld=fld+line[j++];
break;
}
fld=fld+line[j];
}
return j;
}
/* This function return the position of the plain text or integer
which doesnot have quote */
int test:: checkplain(string &line,string &fld,int i)
{
int j;
j=line.find_first_of(',',i); /* Look for separator */
if(j>line.length())
j=line.length();
fld=string(s,i,j-i);
return j;
}
/* Get one line and tokenize based on the delimiter */
int test:: getline(ifstream &ifs,string &line)
{
char c;
for(line == "";ifs.get(c) && !checkofcomma(c)
{
line =line+c;
}
//if(
str1=line;
return !ifs.eof();
}
/* Split each line of the field with the delimeter */
/* String line peakmemory,etc needs to be passed here */
bool test:: parseline(string &str,string &line)
{
int position=1;
/* int saveposition; */
string fld;
int i,j;
int nfield=0;
if(line.length()==0)
return 0;
i=0;
do
{
if(i< line.length() && line == '"')
{ /* get next position of quote */
j=checknextquote(line,fld, ++i);
if(line[++j] == ',') /* If there is comma
after the position of quote
then go to new line */
{
position = position-line[j] +str.length();
/* Go to next line */
cout << "\n";
position=1; /* Go to First Position again */
}
}
else
{
j=checkplain(line,fld,i); /* In this function if there is no
quote then it will be integer
of prefixlength */
if(line[++j] == ',')
{
/* Update the position */
position =position -line[j]+str.length();
if(nfield >=field.size())
{
field.push_back(fld);
}
else
{
field[nfield]=fld;
}
}
/* Got to next line */
cout << "\n";
/* Reset the position */
position=1;
}
nfield ++;
i=j+1;
} while (j<line.length());
/*return nfield; */
return TRUE;
}
int main()
{
/* Create an object of test class */
int count;
char c;
bool success =FALSE;
const char *strprint;
int i;
//string tok1;
int result;
string s1,s2("ticketno"),s3("phonenumber"),s4("carno");
if 0
int status;
status = _open(filename,_O_RDONLY);
if(status == -1)
{
printf("Couldnot able to Open file ");
}
else
{
printf("Opening of file Successful\n");
}
ifstream ifs(filename); // Open for reading
if(ifs.fail())
{
cout << "Couldnot Open the stream";
return EXIT_FAILURE;
}
#endif
ifstream ifs(filename);
test test1;
while(test1.getline(ifs, s)) // Discards newline char(Read line by
line)
{
s += line + "\n";
strprint= s.data();
// cout << strprint << endl;
count=commacount(c,ifs);
for(i=0;i<=count;count++)
{
string str3= test1.read_column_header(s2,ifs);
result=str3.compare(s2);
if (result == 0)
{
string str = str3;
}
string str4=test1.read_column_header(s3,ifs);
result =str4.compare(s3);
if(result == 0)
{
string str =str4;
}
string str5 =test1.read_column_header(s4,ifs);
result=str5.compare(s4);
if(result == 0)
{
string str =str5;
}
/* Parse Each line */
success = test1.parseline(str,line);
if(success)
printf("Parsing Successful");
}
if(!ifs && !ifs.eof())
cout << "Error in Reading Input file";
ifs.close();
// out.close();
return 0;
}
I want to write a program which will read the Comma separated
values(CSV) file column wise.
For example:
In a.txt:
"TicketNumber","Phone","CarNumber"
10,20,30
40,45,80
60,70,34
I want to read the values like the following:
"TicketNumber",
10
40
60
and want to store in the values {10,40,60 } in an array.
When Iam compiling the below program in VC++ 6.0 i get the following
error:
d:\vs6\vc98\include\vector(48) : warning C4786:
'??0?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QAE@IABV?$basic_string@DU?$char_traits@D@s
C:\test.cpp(269) : error C2059: syntax error : 'constant'
C:\test.cpp(271) : error C2065: 'status' : undeclared identifier
C:\test.cpp(288) : fatal error C1020: unexpected #endif
Error executing cl.exe.
test.obj - 12 error(s), 6 warning(s)
Could anyone help?
Regards
Ram Laxman
#include<fstream>
#include<ios>
#include <iostream>
#include<string>
#include<iomanip>
#include<sstream>
#include<vector>
#include<algorithm>
#include<stdio.h>
#include <fcntl.h>
#include <io.h>
#include <cstdlib>
#include <cstring>
using namespace std;
const char *filename ="C:\\result.txt";
#define TRUE 1
#define FALSE 0
class test
{
public:
test(ifstream& fin = ifs) :
fin(ifs) {}
// test(ifstream& fin = filename) :
// fin(filename) { }
string read_file_header(string &str1, ifstream ifs);
int checkofcomma(char c);
int checknextquote(string &line,string &fld,int i);
int checkofeol(char c,ifstream &ifs);
int commacount(char c,ifstream &ifs);
int getline(ifstream &ifs,string &line);
bool parseline(string &str,string &line);
/* string getfield(int n);
int getnfield() const { return nfield; } */
private:
ifstream& ifs; // input file pointer
string line; // input line
vector<string> field; // field strings
// int nfield; // number of fields
// string fieldsep; // separator characters
};
/* Read the Column Header from the file */
string test:: read_column_header(string &str1, ifstream ifs)
{
char *pdest;
int pos;
char lineone[256];
if (!ifs.eof())
{
ifs.getline(lineone,sizeof(lineone),'\n');
pdest =strstr(lineone,str1);
while (pdest == 0)
{
ifs.getline(lineone,sizeof(lineone),'\n');
pdest = strstr(lineone,str1);
}
pos = pdest - lineone + str1.length();//end of Test:
string s = lineone;
string s1 = s.substr(0,pos); /* Return the SubString if the
matches found */
return s1;
}
else
{
return "EOF";
}
}
/* Check for \r or the \n in the line which was get by stream */
int test:: checkofeol(char c,ifstream &ifs)
{
int eol;
eol= (c=='\r' || c== '\n'|| c== ',') ;
if(c=='\t')
{
// continue; /* ignore tab spaces in the line */
}
if(c == '\r')
{
ifs.get(c);
if(!ifs.eof() && c != '\n')
ifs.putback(c);
}
return eol;
}
/* Count the number of comma in the line */
int test:: commacount(char c,ifstream &ifs)
{
int count=0;
for(line == "";ifs.get(c) && !checkofeol(c,ifs)
{
if(c==',')
{
count++;
}
}
return count;
}
/* Get Next Quote and return the index of next quote */
int test:: checknextquote(string &line,string &fld,int i)
{
int j;
fld ="";
for(j=i;j<line.length();j++)
{
if(line[j] == '"' && line[++j] != '"')
{
int k =line.find_first_of('"',j);
if(k>line.length())
k=line.length();
/* Store the String in an array */
for(k=k-j;k-- >0
fld=fld+line[j++];
break;
}
fld=fld+line[j];
}
return j;
}
/* This function return the position of the plain text or integer
which doesnot have quote */
int test:: checkplain(string &line,string &fld,int i)
{
int j;
j=line.find_first_of(',',i); /* Look for separator */
if(j>line.length())
j=line.length();
fld=string(s,i,j-i);
return j;
}
/* Get one line and tokenize based on the delimiter */
int test:: getline(ifstream &ifs,string &line)
{
char c;
for(line == "";ifs.get(c) && !checkofcomma(c)
{
line =line+c;
}
//if(
str1=line;
return !ifs.eof();
}
/* Split each line of the field with the delimeter */
/* String line peakmemory,etc needs to be passed here */
bool test:: parseline(string &str,string &line)
{
int position=1;
/* int saveposition; */
string fld;
int i,j;
int nfield=0;
if(line.length()==0)
return 0;
i=0;
do
{
if(i< line.length() && line == '"')
{ /* get next position of quote */
j=checknextquote(line,fld, ++i);
if(line[++j] == ',') /* If there is comma
after the position of quote
then go to new line */
{
position = position-line[j] +str.length();
/* Go to next line */
cout << "\n";
position=1; /* Go to First Position again */
}
}
else
{
j=checkplain(line,fld,i); /* In this function if there is no
quote then it will be integer
of prefixlength */
if(line[++j] == ',')
{
/* Update the position */
position =position -line[j]+str.length();
if(nfield >=field.size())
{
field.push_back(fld);
}
else
{
field[nfield]=fld;
}
}
/* Got to next line */
cout << "\n";
/* Reset the position */
position=1;
}
nfield ++;
i=j+1;
} while (j<line.length());
/*return nfield; */
return TRUE;
}
int main()
{
/* Create an object of test class */
int count;
char c;
bool success =FALSE;
const char *strprint;
int i;
//string tok1;
int result;
string s1,s2("ticketno"),s3("phonenumber"),s4("carno");
if 0
int status;
status = _open(filename,_O_RDONLY);
if(status == -1)
{
printf("Couldnot able to Open file ");
}
else
{
printf("Opening of file Successful\n");
}
ifstream ifs(filename); // Open for reading
if(ifs.fail())
{
cout << "Couldnot Open the stream";
return EXIT_FAILURE;
}
#endif
ifstream ifs(filename);
test test1;
while(test1.getline(ifs, s)) // Discards newline char(Read line by
line)
{
s += line + "\n";
strprint= s.data();
// cout << strprint << endl;
count=commacount(c,ifs);
for(i=0;i<=count;count++)
{
string str3= test1.read_column_header(s2,ifs);
result=str3.compare(s2);
if (result == 0)
{
string str = str3;
}
string str4=test1.read_column_header(s3,ifs);
result =str4.compare(s3);
if(result == 0)
{
string str =str4;
}
string str5 =test1.read_column_header(s4,ifs);
result=str5.compare(s4);
if(result == 0)
{
string str =str5;
}
/* Parse Each line */
success = test1.parseline(str,line);
if(success)
printf("Parsing Successful");
}
if(!ifs && !ifs.eof())
cout << "Error in Reading Input file";
ifs.close();
// out.close();
return 0;
}