The code (pasted below) is supposed to read the input file and then parse the data into the proper place in the structure.
IF the file being read has no blank spaces the data is read properly. But once the data has blank lines then the file gets stuck in a infinite loop.
I have been reading about skipping blank lines/ empty lines.
I try using the "if (buffer[0] == '\n')"
then read another line. but it isn't working at all!
Does some one know the code that will allow ( i was using a while loop in case there were more than one blank line.) me to skip the blank lines and allow the code to parse all the data.
The file being read would look like this
"
<student>
<first>
1FRED
</first>
<mi>
J
</mi>
<last>
JOHNSON
</last>
<ssn>
123456788
</ssn>
</student>
<STUDENT>
<FIRST>
2SUSIE
</FIRST>
<MI>
Q
</MI>
<LAST>
WATSON
</LAST>
<SSN>
234567899
</SSN>
</STUDENT>
"
Here's the code:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <stdio.h>
#include <cstring>
#include <strings.h>
using namespace std;
struct record
{
char first [20];
char mi [1];
char last [20];
int ssn;
};
void filename (char ifname [], struct record *student[]);
void structfill (fstream & infile, struct record *student[]);
int main ()
{
system ("clear");
fstream infile;
char ifname [256];
struct record * student [50];
filename (ifname, student);
return 0;
}
/*******************************************************************/
void filename (char ifname [],record *student [])
{
fstream infile;
cout << "Enter name of file to read from: ";
cin.getline (ifname, 256);
cout << endl;
infile.open (ifname);
if (!infile.is_open ())
{
cerr << "FILELOOP!: Unable to open input file " << ifname
<< endl;
exit (1);
}
structfill (infile, student);
}
/*******************************************************************/
void structfill (fstream & infile, record *student [])
{
char buffer [81];
int buffernumber [81];
int n=0;
int f=0;
infile.getline (buffer,81);
while (!infile.eof ())
{
if (strncasecmp (buffer, "<student>",9)==0)
{
student[n] = new record;
while ((strncasecmp (buffer, "</student>",10) != 0))
{
infile.getline (buffer, 81);
if (strncasecmp (buffer, "<first>",7)==0)
{
infile.getline (buffer, 81);
if (buffer[0] == '\n')
{
infile.getline (buffer, 81);
cout << "-----";
}
strcpy (student[n]->first, buffer);
}
if (strncasecmp (buffer, "<mi>",4)==0)
{
infile.getline (buffer,81);
if (buffer[0] == '\n')
infile.getline (buffer, 81);
strcpy (student[n]->mi, buffer);
}
if (strncasecmp (buffer, "<last>",4)==0)
{
infile.getline (buffer, 81);
if (buffer[0] == '\n')
infile.getline (buffer, 81);
strcpy (student[n]->last, buffer);
}
if (strncasecmp (buffer, "<ssn>",4)==0)
{
infile.getline (buffer, 81);
if (buffer[0] == '\n')
infile.getline (buffer, 81);
}
} n++;
infile.getline (buffer,81);
}
}
for (int a =0; a < n; a++){
cout << student[a]->first << " " << student[a]->mi << " " << student[a]->last << a << endl;
}
}