473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++Need help getting a function to write data from a text file to a vector of structs

5 New Member
I was told to write a function that will read data, in a user defined text file, into a vector of structs. I am not sure where to start with this. Could some one tell me what is a vector of structs, I know vectors and I know structs, but I dont know whats a vector of structs. As well, is there a specific algoritm in c++ to use when reading from a user defined text file, or does cin work? If someone can give me a few pointers to help get started with this I would appreciate it. Thanks.

A sample of one of the text files is formatted like this:

3

Jon Smith 20009 40 32 12 13 14 17 12

Wilma Vohn 76448 20 30 12 12 18 20 14

Mark Matthews 55642 50 52 15 16 16 15 17
Apr 5 '07 #1
4 1770
Ganon11
3,652 Recognized Expert Specialist
A vector of structs is just a vector holding structs:

Expand|Select|Wrap|Line Numbers
  1. vector<myStruct> myVec;
In order to do file input/output, you have to use an ifstream and ofstream object. These are included in the header file fstream (for file stream). ifstream works like cin, and ofstream works like cout.

To use an ifstream, for example, you would use this snippet of code:

Expand|Select|Wrap|Line Numbers
  1. ifstream FIN;
  2. FIN.open("myFile.txt");
  3. FIN >> myVar;
ofstreams are similar, but used for output (again, like cout):

Expand|Select|Wrap|Line Numbers
  1. ofstream FOUT;
  2. FOUT.open("myOutputFile.txt");
  3. FOUT << "This is a test of ofstreams.";
Of course, you'll have to come up with your own code to fit your data files.
Apr 5 '07 #2
engggirl3000
5 New Member
Thanks for your help.

Another question I have, what is the difference between reading a text file to a program and opening a text file in the program?
Apr 5 '07 #3
Extremist
94 New Member
Thanks for your help.

Another question I have, what is the difference between reading a text file to a program and opening a text file in the program?
You already posted it in it's own thread, don't repeat yourself constantly
Apr 5 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
You are going to be much happier if you use a flat file. That is, have a record layout so that the fields in the record are the same size in each record:

Jon Smith 20009 40 32 12 13 14 17 12

Wilma Vohn 76448 20 30 12 12 18 20 14

Mark Matthews 55642 50 52 15 16 16 15 17

If you do this, the name will always be the same length on the disc so you can use getline(). The other data can use the >> operator.

Then you code a Read function takes a struct address as an argument and that reads the next record from the file populating the struct with the data.
Apr 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3058
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
6
1708
by: Affan Syed | last post by:
Hi, I am getting this weird problem. I know what i am doing is strange.. i am using C++ vectors and fopen, but for some reason if i used ofstream in the similar scenario it would give me errors. So here is what i do. I create a new node and insert it in a vecotr as follows: nodeCreator = new cNode(location, skew, offset,Beaconify,nodeId,directory); gNodeVector.push_back(*nodeCreator); //now lets delete the memory we created for the node
4
2208
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
6
2130
by: naknak | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
20
4652
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
10
3014
by: arnaudk | last post by:
Hello, I am relatively new to C++ and have been milling over a container related problem for a while now. I have a ~2Gb ASCII file of time-ordered data with 4 comma-separated columns. The first column is a string identifier, the second and third are date and time, and the last two some numerical data, e.g.: frog, 10/06/2006, 23:56:03, 12.3456, 322.551 frog, 11/06/2006, 22:00:06, 6.12136, 41.1236 fish, 12/06/2006, 09:01:54, 1.3456, 0.3321...
10
1895
by: colin | last post by:
Hi, I profile my code and find its spending a lot of time doing implicit conversions from similar structures. the conversions are mainly things like this class Point { implicit conversion to vector3; //this conversion just returns positon Vector3 position;
9
2639
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the object.The object is triangulated. The rays can undergo multiple reflections, diffractions etc of the same object i.e. a ray hits a surface of the object, undergoes reflection resulting in a reflected ray which can again hit a surface, corner or edge...
1
4931
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category order, Input field and a submit button. The Category name is being fetched from the oracle db along with the corresponding Category order. In the corresponding input field (text box) the user enters a new category order which gets stored in...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7547
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
6788
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
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
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
2925
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.