473,699 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

read data from file into structure ?

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,lin e)){
istringstream anyname(line);

anyname>>studen t.name>>student .midterm>>stude nt.quiz>>studen t.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

Nov 22 '05 #1
2 2072
use a class or a function to do it

Nov 22 '05 #2
tv****@hotmail. com 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,lin e)){
istringstream anyname(line);

anyname>>studen t.name>>student .midterm>>stude nt.quiz>>studen t.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,lin e))
{
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
Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4607
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential options. I can get this to worksuccessfully, but only when I'm NOT dealing with arrays in thestructure definition. For example: In C, you are able to simply read a binary file intoa structure as follows: ...
5
16651
by: Vasilis Serghi | last post by:
Hi all, I am fairly new to C programming and I have come to a stand still. I am trying to create a program that will accept a user input and give the user an output depending on the contents of a csv file. So far I have successfully managed to capture the user input, open the csv file, create a structure that matches the file data, but I cannot get the data into the structure to do a search on. This is some of what I have done so far:- ...
5
3335
by: Alejandro Lapeyre | last post by:
I am writing a class to wrap a RIFF file, so i have to read and decode some structured data on the file. For example, in the begining of file is a header: Public Structure RiffHeader Public id As Int32 ' (a FOURCC actually) Public fileSize As Int32 Public fileType As Int32 End Structure
3
4008
by: Dave Coate | last post by:
Hello again, I am going to re-post a question. I got some excellent suggestions from Rob and Mattias on this but their ideas did not solve the problem. Here is the original post: ***************************************************** I am looking for a way to 'override' file security and read the Owner of a file to which I have no access. I am a system administrator, as such I have administrative rights to all the computers in the...
2
3160
by: Eric | last post by:
Hi, I need to send data from a file into a structure. In Vb6 this was done like so. public Type Struct1 v1 as string v2 as string end type
8
23903
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
2633
by: Jens | last post by:
Hello, I have been looking for some C-code which listens on a user-defined port for incoming data traffic. When data is received, the data is written to a file. I found some C-code (server) that almost does the job. It listens on a user-defined port and responds to incoming data by writing how many times somebody has tried to connect to the server.
11
3345
by: waffle.horn | last post by:
Hi, if this makes sense i want to create a function that can be called so that it reads a single line from a file, then after using the information destroys it. Such that when the function is called that line of info does not exist anymore in the file. for example i have created a short example of where i am at, but I dont know how to alter what im doing so that i just read in a single line
9
2255
by: Hollywood | last post by:
Hello members of the comp.lang.c++, My log file is made of a set of 1000 following lines kind: 21/09/07 13:49:56,MW.SET.D_IGLS,2.000000 21/09/07 13:49:56,MW.SET.GNP_NT,7.000000 ..... commas ',' are used as field separators (data, name, value).
6
342
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have following data.bin (3 Data appended to 2 Data): 2, data0, data1, 3, data0, data1, data2
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.