How can I make
Code:
infile.getline(element[i].symbol, 3, ','); //get data until new line
infile.getline(element[i].name, 15, ',');
infile.getline(dummybuffer, 5, ',');
element[i].numb = atoi(dummybuffer);
infile.getline(dummybuffer, 10, ',');
element[i].mass = atof(dummybuffer);
Go character by character?
Code:
for (i=0; (!infile.eof()) && (i <= 112); i++)
{
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
infile >> ch;
element[i].symbol[0] = ch;
infile >> ch;
element[i].symbol[1] = ch;
}
}
Here's my entire code:
Code:
#include <fstream.h> //header for cout and cin
#include <string.h> // header for strings
#include <windows.h> // required for system("cls")
struct chem
{
char symbol[3];
char name[15];
int numb;
double mass;
};
main()
{
ifstream infile;
int i = 0;
int j = 0;
char dummybuffer[26];
char ch;
chem element[113];
infile.open("CHEM.DAT",ios::in);
if (!infile) {cout << "ERROR" << endl << endl; return 1;}
for (i=0; (!infile.eof()) && (i <= 112); i++)
{
infile >> ch;
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
element[i].symbol[0] = ch;
infile >> ch;
element[i].symbol[1] = ch;
infile >> ch;
}
j=0;
infile >> ch;
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
element[i].name[j] = ch;
infile >> ch;
j++;
}
j=0;
infile >> ch;
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
dummybuffer[j] = ch;
infile >> ch;
j++;
}
element[i].numb = atoi(dummybuffer);
j=0;
infile >> ch;
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
dummybuffer[j] = ch;
infile >> ch;
j++;
}
element[i].mass = atof(dummybuffer);
/*infile.getline(element[i].symbol, 3, ','); //get data until new line
infile.getline(element[i].name, 15, ',');
infile.getline(dummybuffer, 5, ',');
element[i].numb = atoi(dummybuffer);
infile.getline(dummybuffer, 10, ',');
element[i].mass = atof(dummybuffer); */
}
infile.close();
for (i=0; i<112; i++)
{
cout << element[i].symbol << "," << element[i].name << "," << element[i].numb << "," << element[i].mass << endl;
}
return 0;
}
}
I'm not getting the output I want