473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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 1748
Ganon11
3,652 Expert 2GB
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
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
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 Expert Mod 8TB
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
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...
6
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....
4
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...
6
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...
20
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
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...
10
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...
9
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.