hello,

I am working on a little database program in C++ that writes to a text file, saves, and then reads the text file into array's, but i'm not real sure how to go about converting the information in the text file back into arrays.

the text file looks like this

john,15,fl,clear
bob,300.5,overthere,blue
...

name, location and color are strings, weight is a float.

id like to read them into 4 array's
name[], weight[], location[], color[]
but i'm a little stuck in how to do this, if anyone wants to point me the right direction it would be greatly appreciated

thank you.

Recommended Answers

All 9 Replies

Here is one way to do it -- uses a vector of structures instead of individual arrays.

#include<iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

struct person
{
    string name;
    float weight;
    string location;
    string color;
};

//inside main
int main(void)
{
    vector<person> people;
    string wt;
    person p;
    ifstream in("data.txt");
    while( getline(in,p.name,',') )
    {
        getline(in,wt,',');
        p.weight = (float)atof(wt.c_str());
        getline(in,p.location,',');
        getline(in,p.color);
        people.push_back(p);
    }
//
// now just display all the data in the vector
//
    vector<person>::iterator it = people.begin();
    for(; it != people.end(); it++)
    {
        cout << (*it).name << " "
            << (*it).weight << " "
            << (*it).location << " "
            << (*it).color << "\n";
            
    }

    return 0;
}

I had this exact same kind of assignment a couple of weeks ago. The hardest thing for me was; make sure the input text file is saved in ANSI & not UNIcode format! Otherwise the input text will be jibberish

After then its easy especially if you use a struct concerning the arrays.

/*

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

struct personal_details
{
	string name;
	int age;
	string favourite_colour;
};

int main()
{
	ifstream infile;

	infile.open("text.txt");
	if (!infile) {
		cout << "Unable to open file";
		exit(1); // terminate with error
	}

	while (infile) {
		for(int i = 0; i < 900; i++) {  // NOTE for i < 900 this is the size of the array you are making so alter it to whatever size you desire

			getline(infile, personal_details.name[i], ',');
			getline(infile, personal_details.age[i], ',');
			getline(infile, personal_details. favourite_colour[i], ',');
		}
	}

	infile.close();

	// display contents of each array
	
	for(int i = 0; i < 900; i++) {
		cout << personal_details.name[i] << " ";
	}

	cout << endl;

	for(int i = 0; i < 900; i++) {
		cout << personal_details.age[i] << " ";
	}

	cout << endl;

	for(int i = 0; i < 900; i++) {
		cout << personal_details. favourite_colour[i] <<  " ";
	}

	return 0;
}

>> while (infile) {
>> for(int i = 0; i < 900; i++) { // NOTE for i < 900 this is

Don't you see a problem with that code? What will happen if the file only contains 10 lines? Or 901 lines? There is no need for that for loop. Instead, just use variable i as a counter to index into arrays.

@gretty : Ancient Dragon is right its not efficient.

The above code works right ??? Then whats the problem ? Why isn't this thread marked solved ?

The above code works right ??? Then whats the problem ? Why isn't this thread marked solved ?

Because the OP has not posted any code or made any other comments.

@Ancient Dragon; Thanks your right :) I've put a better way of doing it below, my first code had other problems also.

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>


using namespace std;

struct personal_details
{
	string name[];
	int age[];
	string favourite_colour[];
};

int main()
{
	personal_details pd;
	ifstream infile;

	int i = 0; // counter to use determine the size of the array input

	infile.open("music.txt.windows");
	if (!infile) {
		cout << "Unable to open file";
		exit(1); // terminate with error
	}

	while (infile) {
		getline(infile, pd.name[i], ',');
		infile >> pd.age[i];
		getline(infile, pd.favourite_colour[i], ',');
		i++;

	}


	infile.close();

	// display contents of each array

	for(int j = 0; j < i; j++) {
		cout << pd.name[j] << " ";
	}

	cout << endl;

	for(int j = 0; j < i; j++) {
		cout << pd.age[j] << " ";
	}

	cout << endl;

	for(int j = o; j < i; j++) {
		cout << pd.favourite_colour[j] <<  " ";
	}

	return 0;
}

I think you didn't understand what Ancient Dragon said.You are printing the array till 900 but the inputs may end at 10 itself right?
So take a variable say count inside while loop as:

count=0;
while (infile) {
	getline(infile, pd.name[i], ',');
	infile >> pd.age[i];
	getline(infile, pd.favourite_colour[i], ',');
        count++;
	i++;
}

And then print array only till count as:

for(int i = 0; i < count; i++){}

Thanks very much for all the help guys

I got exactly what I needed.

Here is the first problem your code has:

struct personal_details
{
	string name[];  // Error : Zero sized array
	int age[]; // Error : Zero sized array
	string favourite_colour[]; // Error : Zero sized array
};

To solve this problem:
1. Find out the total number of record a file has.
2. Use dynamic allocation technique.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.