Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > read data from file into structure ?

Reply
Thread Tools

read data from file into structure ?

 
 
tvn007@hotmail.com
Guest
Posts: n/a
 
      11-19-2005
I am using the following code to read data from input file into
structure.
Not sure it is the best way. Please comment or you have better way.
please let me kmow.
//////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

struct Record_info {
string name;
double midterm;
double quiz;
double final;
}student ;
int main (void){

double grade;

ifstream in ("test2.txt");
string line,word;
while (getline(in,line)){
istringstream anyname(line);

anyname>>student.name>>student.midterm>>student.qu iz>>student.final;
grade = student.quiz+ 10;
cout << student.name <<endl;
cout << student.midterm <<endl;
cout << student.quiz <<endl;
cout << student.final <<endl;
cout << "GRADE: "<<grade <<endl;
}
return 0;
}
/////////////////////// test2.txt
///////////////////////////////////////
Tony 90.0 -15.2 98.2
Michael 95.0 17.2 92.2

 
Reply With Quote
 
 
 
 
vire
Guest
Posts: n/a
 
      11-19-2005
use a class or a function to do it

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      11-20-2005
wrote:
> I am using the following code to read data from input file into
> structure.
> Not sure it is the best way. Please comment or you have better way.
> please let me kmow.
> //////////////////////////////////////////////////////////////////////////
> #include <iostream>
> #include <string>
> #include <fstream>
> #include <sstream>
> using namespace std;
>
> struct Record_info {
> string name;
> double midterm;
> double quiz;
> double final;
> }student ;
> int main (void){
>
> double grade;
>
> ifstream in ("test2.txt");
> string line,word;
> while (getline(in,line)){
> istringstream anyname(line);
>
> anyname>>student.name>>student.midterm>>student.qu iz>>student.final;
> grade = student.quiz+ 10;
> cout << student.name <<endl;
> cout << student.midterm <<endl;
> cout << student.quiz <<endl;
> cout << student.final <<endl;
> cout << "GRADE: "<<grade <<endl;
> }
> return 0;
> }
> /////////////////////// test2.txt
> ///////////////////////////////////////
> Tony 90.0 -15.2 98.2
> Michael 95.0 17.2 92.2
>


Looks fine to me, apart from the gratuitous use of a global variable.
Try this

struct Record_info {
string name;
double midterm;
double quiz;
double final;
};

int main()
{
...
while (getline(in,line))
{
istringstream anyname(line);
Record_info student;
anyname >> student.name >> student.midterm >>
student.quiz >> student.final;
...
}
...
}

Declare variables locally where you need them, not globally.

john
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read a Structure from a matlab file to a structure in Vc++ 2003 meisterbartsch C++ 2 06-12-2007 08:47 PM
read data from file into structure in C++ ? tvn007@hotmail.com C++ 3 11-11-2005 09:18 AM
initialize data structure or read text file Joan Java 11 08-31-2005 09:53 PM
Need to concatenate all files in a dir together into one file and read the first 225 characters from each file into another file. Tony Perl Misc 5 04-19-2004 03:28 PM
How do I read a csv file into a structure? Vasilis Serghi C Programming 5 01-23-2004 08:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57