3 Replies - 4771 Views - Last Post: 07 March 2010 - 09:54 AM Rate Topic: -----

#1 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 109
  • Joined: 03-October 09

Reading Text file into structure

Posted 07 March 2010 - 08:40 AM

this is my structure:
struct data
		{
			string pName;
			string pCat;
			int pBarcode;
			double pPrice;
			string pMan;
			int pStock;
			int pSold;
			data *next;
			data *prev;
		};



This is my text file:

3

Kidney Vetch
Team Sports
157541796
8601.14
Kelloggs
465
100

Kidney Vetch
Food
971324986
1526.55
Fisher Price
845
466

Tea Tree
Breakfast Foods
861165080
2281.0
Purina One
186
803



This is my function:

void Interface::dataTxt(){
	int noItem,i;
	ifstream datas("data-xs");

	datas >> noItem;
		for(i=0;i<noItem;i++){
			cin>>data.ppName;
			cin>>data.pCat;
			cin>>data.pBarcode;
			cin>>data.pPrice;
			cin>>data.pMan;
			cin>>data.pStock;
			cin>>data.pSold;
		}
	infile.close();
	

}



The structure is in my database.h, and the function is in my interface.cpp there is a interface.h too where i #include "database.h"

I am having quite alot errors. May i noe if that is the correct way to read in a text file into array of struture? the first number in the text file is the number of item in the file.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading Text file into structure

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Reading Text file into structure

Posted 07 March 2010 - 08:50 AM

cin>>data.ppName;


What are trying to do with cin? Shouldn't you be reading data from a file? Anyways, for this assignment, you will be needing an array of structures. Also, be careful. The >> operator DOES not read the file line-by-line. It reads a specific word until a space is encountered. In in your input file, you have "Kidney Vetch" as a name. If you want to extract data from the file, Kidney will be assigned to pName, and Vetch to pCat. I suggest you to remove the spaces, otherwise, you cannot do this assignment using >>. So, modifying the input file like this:
3

KidneyVetch
TeamSports
157541796
8601.14
Kelloggs
465
100

KidneyVetch
Food
971324986
1526.55
FisherPrice
845
466

TeaTree
BreakfastFoods
861165080
2281.0
PurinaOne
186
803


and using this:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct data
{
	string pName;
    string pCat;
    int pBarcode;
    double pPrice;
    string pMan;
    int pStock;
    int pSold;
    data *next;
    data *prev;
};

int main()
{
	std::ifstream ifs;
	ifs.open("data.txt");

        if (!ifs)
           exit(1);

	data d[3]; // create an array of structures

	int num;
	ifs >> num; // read the first line, which represents the number of "structured data"

        // Extract individual line and append it to structure, using the >> operator
        // Make sure to not have any spaces on a line, because everything will be messed up
	for (int i = 0; i < num; i++)
		ifs >> d[i].pName >> d[i].pCat >> d[i].pBarcode >> d[i].pPrice >> d[i].pMan >> d[i].pStock >> d[i].pSold;

        // Print the extracted data.
	for (int i = 0; i < num; i++)
		std::cout << d[i].pName << "\n" << d[i].pCat << "\n" << d[i].pBarcode << "\n" << d[i].pPrice << "\n" << d[i].pMan << "\n" << d[i].pStock << "\n" << d[i].pSold << "\n\n";

	ifs.close();
	cin.ignore();
	cin.get();
	return 0;
}


I successfully managed to read the data.

This post has been edited by sarmanu: 07 March 2010 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Reading Text file into structure

Posted 07 March 2010 - 08:51 AM

tnecniv, haven't you been here long enough to know that this:

Quote

I am having quite alot errors.

is completely insufficient information? POST. YOUR. ERRORS!!!!
Was This Post Helpful? 0
  • +
  • -

#4 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 109
  • Joined: 03-October 09

Re: Reading Text file into structure

Posted 07 March 2010 - 09:54 AM

I have edited my code and these are my errors:

error C2061: syntax error : identifier 'data'
error C2065: 'data' : undeclared identifier
error C2228: left of '.pName' must have class/struct/union
error C2784: 'std::basic_istream<_Elem,_Traits>
error C2780: 'std::basic_istream<_Elem,_Traits>

#include "interface.h"
#include<fstream>


void Interface::mainMenu()
{	
	int Nitem,i;
	ifstream inFile("data-xs.txt");
	if (inFile==NULL ){
	cout<<"Error"<<endl;//return error if unable to open file//
	}
	else
	inFile >> Nitem;//read from text file the number of data//

	for(i=0;i<Nitem;i++){//read the data from text file//
		new data;
		getline(inFile,data.pName);
	}
}



#include <iostream>
#include <string>

using namespace std;

class Database
{
	private:
		struct data
		{
			string pName;
			string pCat;
			int pBarcode;
			double pPrice;
			string pMan;
			int pStock;
			int pSold;
			data *next;
			data *prev;
		};


I have only tried to take in the first value and the errors occured.
Please advise how to get line and put it into the struture.

This post has been edited by tnecniv: 08 March 2010 - 05:20 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1