473,507 Members | 11,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading file into string array

Hi,

I have a project that's supposed to create a program for a "Dating
Service". The first part of the program is to read a textfile of
profiles which include names, age, etc...into a string array, and be
able to add,edit,remove to the textfile of profiles during runtime.
What would be the most efficient way to do this to make it easiest as
possible to make changes to the textfile during time and access
elements of the array?

john

Mar 6 '06 #1
2 3012
no**********@gmail.com wrote:
Hi,

I have a project that's supposed to create a program for a "Dating
Service". The first part of the program is to read a textfile of
profiles which include names, age, etc...into a string array, and be
able to add,edit,remove to the textfile of profiles during runtime.
What would be the most efficient way to do this to make it easiest as
possible to make changes to the textfile during time and access
elements of the array?

john


First, don't use an array or a raw buffer; use a std::vector of
std::strings (see
http://www.parashift.com/c++-faq-lit...html#faq-34.1). If you
do this, use the getline function, something like this:

ifstream file( "dating.txt" );
vector<string> v;
string line;
while( getline( file, line ) )
{
v.push_back( line );
}
// Now process the vector of strings

Alternately, you might consider implementing an extraction operator for
your data structure. Something like:

struct Person
{
string first, last;
unsigned int age;
// ...
};

istream& operator>>( istream& is, Person& p )
{
is >> p.first >> p.last >> p.age;
return is;
}

Then you could use it like this:

vector<Person> v;
Person p;
while( file >> p )
{
v.push_back( p );
}
// Now process the vector of People

Note that you can't generally modify data in the middle of a file (e.g.
by seeking to a certain point and trying to overwrite the data), so you
may need to write the entire data file each time.

Cheers! --M

Mar 6 '06 #2
thanks!!!

Mar 6 '06 #3

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

Similar topics

3
9496
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
6
43977
by: Neil Patel | last post by:
I have a log file that puts the most recent record at the bottom of the file. Each line is delimited by a \r\n Does anyone know how to seek to the end of the file and start reading backwards?
29
10371
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and...
18
9021
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
2
1730
by: GeoUK | last post by:
Hi All, New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have. As part of a course I am doing at University I had...
10
8333
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
7
11767
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
0
2174
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
21
3015
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
1
4701
by: stoogots2 | last post by:
I have written a Windows App in C# that needs to read a text file over the network, starting from the end of the file and reading backwards toward the beginning (looking for the last occurrence of a...
0
7221
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
7372
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...
1
7029
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...
0
7481
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...
0
5619
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,...
1
5039
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
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...

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.