Hi everyone, i am a student of IT and i am trying to read a text file and load it into a records of nested structs. I understand the logic how to do it, but i had a few problems.

the txt. file contains :

John Smith
125 2004
Daily News,76
New Mail,159
English news,244
The Mercury,342

<< moderator edit: added code tags: [code][/code] >>

my definition of structs are

struct annualSalesData
{
	int numVendors, year;
	vendorData *vendors;
};

struct vendorData
{
  char name[MAX_VENDOR_NAME_SIZE];
  dailySalesData sales[MAX_DAILY_SALES];
};

struct dailySalesData
{
  int day;
  int papers[NUM_NEWSPAPERS];
};

DailySalesData is nested into the vendorData and VendorData is nested into
annualSalesData ( with a pointer to this struct )

my code was:

/create the stream object

	ifstream myFile;


//connect it with the file

	myFile.open( fileName.c_str(), ios::in );

//check if it works

	if ( !myFile.good() )
	{
		cerr << "ERROR: Unable to open file " << fileName << endl;
		return false;
	}


// case positive read data
//try to read fist entry
    
	myFile.seekg ( sales.vendors[sales.numVendors].name * sizeof (annualSalesData ), ios::end );

// close file

	myFile.close();
    return true;
}  //end of function load text file for Update vendors

i tried to use the stream >> <<

as :

myFile >> sales.vendors[sales.numVendors].name;

... etc

But it does not work either...
if anyone can help me with some directions on how to do load the file into de records struct. Any help will be sincerely appreciate it. Or any examples of how to read data into nested structs would be great. Thanks so much!

regards

This does not try to read, it just moves to someplace in the file:

//try to read fist entry
    
	myFile.seekg ( sales.vendors[sales.numVendors].name * sizeof (annualSalesData ), ios::end );

Is sales.numVendors initialized here?

myFile >> sales.vendors[sales.numVendors].name;

Hi there thanks for help. I just fixed some of the problem. What i've done is when i read the txt. file i copied the details on temporary files. Then i tried to
copy them into the structs and compiled, but when i run the program, i had an error giving some memory adresses and saying that memory couldn't be "written"


//the sales nested struct is initialised before comes to this function
//temp file
char buffer [MAX_VENDOR_NAME_SIZE]; // that the temp file i used to store vendor's name

sales.numVendors++;
strcpy ( sales.vendors[sales.numVendors].name, buffer );

//

Any help would be very appreciate it. Best Regards.

This looks potentially suspect.

//the sales nested struct is initialised before comes to this function
//temp file
char buffer [MAX_VENDOR_NAME_SIZE]; // that the temp file i used to store vendor's name

   sales.numVendors++;
   strcpy ( sales.vendors[sales.numVendors].name, buffer );

It would really help to see more context, however.

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.