-
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
-
1 Attachment(s)
CHEM.DAT
Here's my chem.dat... it might help
Rename it to CHEM.DAT
-
You could use infile.get() to read in one character. Otherwise the newline gets skipped, which is messes you up.
Code:
for (i=0; (!infile.eof()) && (i <= 112); i++)
{
j=0;
ch = infile.get();
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
element[i].symbol[j++] = ch;
ch = infile.get();
}
element[i].symbol[j] = '\0'; //string terminator
j=0;
ch = infile.get();
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
element[i].name[j++] = ch;
ch = infile.get();
}
element[i].name[j] = '\0'; //string terminator
j=0;
ch = infile.get();
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
dummybuffer[j++] = ch;
ch = infile.get();
}
dummybuffer[j] = '\0'; //string terminator
element[i].numb = atoi(dummybuffer);
j=0;
ch = infile.get();
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
dummybuffer[j++] = ch;
ch = infile.get();
}
dummybuffer[j] = '\0'; //string terminator
element[i].mass = atof(dummybuffer);
-
Or you could put a comma at the end of each line. Your choice.
-
And if you want to get fancy, make a function.
Code:
for (i=0; (!infile.eof()) && (i <= 112); i++)
{
read_token(infile,element[i].symbol);
read_token(infile,element[i].name);
element[i].numb = atoi(read_token(infile,dummybuffer));
element[i].mass = atof(read_token(infile,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;
}
char *read_token(ifstream &infile, char *token)
{
int j = 0;
char ch;
ch = infile.get();
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
token[j++] = ch;
ch = infile.get();
}
token[j] = '\0';
return token;
}
-
Your adding null terminators didn't do the trick. I get lots of garbage with either of our code
-
Did you cut and paste? It ran without any problem for me. Printed out all 112 elements. I changed quite a few things (for example, that first while-loop).
Maybe you better paste your latest code to be sure it matches mine. You might also try shortening your loops from 112 to 5 and see what happens.
-
Let me try your function again... yeah, it doesn't work, even with the function or reducing what it outputs.
-
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++)
{
j=0;
ch = infile.get();
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
element[i].symbol[j++] = ch;
ch = infile.get();
}
element[i].symbol[j] = '\0'; //string terminator
j=0;
ch = infile.get();
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
element[i].name[j++] = ch;
ch = infile.get();
}
element[i].name[j] = '\0'; //string terminator
j=0;
ch = infile.get();
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
dummybuffer[j++] = ch;
ch = infile.get();
}
dummybuffer[j] = '\0'; //string terminator
element[i].numb = atoi(dummybuffer);
j=0;
ch = infile.get();
while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
{
dummybuffer[j++] = ch;
ch = infile.get();
}
dummybuffer[j] = '\0'; //string terminator
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;
}
What code are you using, exactly?
-
I just cut and pasted your code, compiled it, and it ran perfectly on my machine!
Try moving this:
chem element[113];
Above main:
chem element[113];
int main()
.
.
-
It almost has the feel like your CHEM.DAT file changed. I used the one you posted.
-
Very peculiar... now it works thanks :D
-
It works without changing it, too... weird.
Apparently my other computer is not compiling properly.
-
It sounds like your compiler doesn't realize you've changed the source file. Maybe look to see if there's a "build all" option to make sure it recompiles everything. It all depends on the compiler. Every compiler/IDE has different options.
-
yeah, I have that. Thanks!