Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read Seq File

Status
Not open for further replies.

OOzy

Programmer
Jul 24, 2000
135
SA
Hi Guys

I have a seq file that has three lines eash line has the name and id of student such as

FN1,LN1,ID1
FN2,LN2,ID2
FN3,LN3,ID3

How can I read this file and print it in the screen. I started with the following code, can you help.

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


void PrintStruct(const char *, const char *, int);

// Student Structure
struct Student
{
char FirstName[10];
char LastName[10];
int ID;
};


// Main Function
void main()
{
Student aStudent;
ifstream StuData(&quot;data.txt&quot;,ios::eek:ut);

if (!StuData)
{
cerr << &quot;File could not be opened\n&quot;;
exit(1);
}


cout << setiosflags(ios::left) << setw(20) << &quot;First Name&quot; << setw(20) << &quot;Last Name&quot; << &quot;ID\n&quot;;


while (StuData >> aStudent.FirstName >> aStudent.LastName >> aStudent.ID)
PrintStruct(aStudent.FirstName, aStudent.LastName, aStudent.ID);

}


void PrintStruct(const char *fn, const char *ln, int id)
{
cout << fn <<&quot;,&quot;<<ln<<&quot;,&quot;<<id;
}


 
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
struct Student;

void PrintStruct(Student);

// Student Structure
struct Student
{
char FirstName[10];
char LastName[10];
int ID;
};


// Main Function
void main()
{
Student aStudent;
ifstream StuData(&quot;data.txt&quot;);
if (!StuData)
{
cerr << &quot;File could not be opened\n&quot;;
exit(1);
}

cout<< setiosflags(ios::left) << setw(20) << &quot;First Name&quot;
<< setw(20) << &quot;Last Name&quot; << &quot;ID\n&quot;;


while (StuData)
{
StuData>> aStudent.FirstName;//there will work when
//words are distincted by spaces, not comma or point
//if so, you sould put StuData.ignore() or read commas
//in some variables
StuData>> aStudent.LastName;
StuData>> aStudent.ID;
}
PrintStruct(aStudent);

}


void PrintStruct(Student)
{
cout << Student.fn <<&quot;,&quot;<<Student.ln<<&quot;,&quot;<<Student.id;
}

John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top