Need short C++ I/O Routine

Dear Forum:
I am trying to write a C++ I/O routine for reading the material below. The code should ignore the comment; i.e. anything including and to the right of %. The code should also skip the line with //. I have tried to use an fstream variable and I'm stuck. Many thanks in advance. Kuuku

*** start ***
3
connectivity_matrix_of_system
molecule_1& % methane
4
C & H Harmonic % two strings
1.0 0.5 % [k,ro]
C & H Harmonic
1.0 0.5 % [k,ro]
C & H Harmonic
1.0 0.5 % [k,ro]
C & H Harmonic
1.0 0.5 % [k,ro]
//
molecule_2& % methane
4
C & H Harmonic
1.0 0.5 % [k,ro]
C & H Harmonic
1.0 0.5 % [k,ro]
C & H Harmonic
1.0 0.5 % [k,ro]
C & H Harmonic
1.0 0.5 % [k,ro]
//
molecule_3& % carbon dioxide
2
C & O Harmonic
0.4 0.6 % [k,ro]
C & O Harmonic
0.4 0.6 % [k,ro]
*** end ***
I think for reading the file you need to specify the structure or the data has to be structured.
You cant use here fread() nor getline() functions which can parse your data and store in a buffer/file.

Is there any possibility to format data?

Thanks and Regards,
Aj
cat text | sed 's/%.*//' | sed 's/^\/\/$//'

what is the problem? reading or parsing?
show some code
Last edited on
Thanks to all:

Here is the code.

*** start **


#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

class Molecule{
private:

public:
int numBonds;
char moleculeName[20];
double param[4][2];
void* bondType[4];
void* bondName[4];

Molecule(){;};
~Molecule(){delete bondType; delete bondName;};
}; // Molecule



int main () {
fstream infile;
char line[80];
Molecule* cluster;

int i,j;
int numMolecules, numMolecules1;

numMolecules = 3;
cluster = new Molecule[numMolecules];

size_t chars_read = 0;
char myTest;

infile.open ("connectivity.in", ios::in);
infile >> numMolecules1;
infile >> line; // comment line read but not used
for (i=0; i < numMolecules; ++i)
{
if(!infile.eof()){infile >> line;}
myTest = ' ';

while ((myTest != '&'))
{myTest = line[chars_read];
cluster[i].moleculeName[chars_read] = myTest;
++chars_read;
}
cluster[i].moleculeName[chars_read] = '\0';
infile >> cluster[i].numBonds;
for (j=0; j < cluster[i].numBonds; ++j)
{cluster[i].bondType[j] = new char[4]; cluster[i].bondName[j] = new char[20];
infile >> cluster[i].bondType[j]>> cluster[i].bondName[j]; // example: C&H Harmonic
infile >> cluster[i].param[j][0]>> cluster[i].param[j][1]; // example: 1.0 0.5
} // j
if (i < (numMolecules-1))infile >> line; // comment line read but not used
} // i

infile.close();
return 0;
}

** end **
The code I posted earlier doesn't work with the data I also posted earlier. Can someone please help me fix the problem? I'll sure appreciate this. Many thanks. To recap, here are the code & data

CODE:

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

class Molecule{
private:

public:
int numBonds;
char moleculeName[20];
double param[4][2];
void* bondType[4];
void* bondName[4];

Molecule(){;};
~Molecule(){delete bondType; delete bondName;};
}; // Molecule



int main () {
fstream infile;
char line[80];
Molecule* cluster;

int i,j;
int numMolecules, numMolecules1;

numMolecules = 3;
cluster = new Molecule[numMolecules];

size_t chars_read = 0;
char myTest;

infile.open ("connectivity.in", ios::in);
infile >> numMolecules1;
infile >> line; // comment line read but not used
for (i=0; i < numMolecules; ++i)
{
if(!infile.eof()){infile >> line;}
myTest = ' ';

while ((myTest != '&'))
{myTest = line[chars_read];
cluster[i].moleculeName[chars_read] = myTest;
++chars_read;
}
cluster[i].moleculeName[chars_read] = '\0';
infile >> cluster[i].numBonds;
for (j=0; j < cluster[i].numBonds; ++j)
{cluster[i].bondType[j] = new char[4]; cluster[i].bondName[j] = new char[20];
infile >> cluster[i].bondType[j]>> cluster[i].bondName[j]; // example: C&H Harmonic
infile >> cluster[i].param[j][0]>> cluster[i].param[j][1]; // example: 1.0 0.5
} // j
if (i < (numMolecules-1))infile >> line; // comment line read but not used
} // i

infile.close();
return 0;
}




DATA

3
connectivity_matrix_of_system
molecule_1& % methane
4
C & H Harmonic % two strings
1.0 0.5 % [k,ro]
C & H Harmonic
1.0 0.5 % [k,ro]
C & H Harmonic
1.0 0.5 % [k,ro]
C & H Harmonic
1.0 0.5 % [k,ro]
//
molecule_2& % methane
4
C & H Harmonic
1.0 0.5 % [k,ro]
C & H Harmonic
1.0 0.5 % [k,ro]
C & H Harmonic
1.0 0.5 % [k,ro]
C & H Harmonic
1.0 0.5 % [k,ro]
//
molecule_3& % carbon dioxide
2
C & O Harmonic
0.4 0.6 % [k,ro]
C & O Harmonic
0.4 0.6 % [k,ro]
[code]Your code goes here[/code]
Could you be a little more specific about the problem?
Make a program that generates the output without the comments. Then work in the cleaned input.

1
2
3
//void* bondType[4];
cluster[i].bondType[j] = new char[4]; 
infile >> cluster[i].bondType[j];
I'm not sure what happen when you try to read a void *, but why don't you just made bondtype a char * (or better, a string).
Also what will happen if numBounds>4.
If at all your file is formated or you willing to format it.
Please see this code on the post.
http://www.cplusplus.com/forum/general/32476/


Thanks and Regards,
AJ
Topic archived. No new replies allowed.