Solved

How do I load strings from a file into an array using structures?

Posted on 2011-03-15
2
312 Views
Last Modified: 2012-05-11
I'm having a little trouble loading a file into an array using a structure. I keep getting the error C2440: '=' : cannot convert from 'char []' to 'char [3]'. I'm not sure what I'm doing wrong. I have attached my program along with the question.

struct customer_r
{
	int cust_id;
	char cust_name[19];
	char state[3];
	char discount;
	double balance_due;
	int order_out;
};

struct database
{
	customer_r rec[10];
};
#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include <string.h>
using namespace std;

//functions go here
void openFile(FILE *);
void loadFile(FILE *, struct database *, char[]);


int main()
{	char buffer[50];
	int counter = 0;



	database table, *point_table;
	point_table = &table;

	FILE *fp;

	openFile(fp);
	loadFile(fp, point_table, buffer);

}

void openFile(FILE *fp)
{
	if((fp = fopen("../../ASSIGNV1.DAT", "r")) == NULL)
	{
		printf("Cannot open the file!\n");
	}
}

void loadFile(FILE *fp, struct database table, char buffer[])
{
	int counter = 0;
	while(feof(fp))
	{
		fgets(buffer, 20, fp);
		table.rec[counter].cust_id = atoi(buffer);
		fgets(buffer, 20, fp);
		table.rec[counter].cust_name = buffer;
		fgets(buffer, 20, fp);
		table.rec[counter].state = buffer;
		fgets(buffer, 20, fp);
		table.rec[counter].discount = buffer[0];
		fgets(buffer, 20, fp);
		table.rec[counter].balance_due = atof(buffer);
		fgets(buffer, 20, fp);
		table.rec[counter].order_out = atoi(buffer);
		++counter;
	}
	fclose(fp);
}

Open in new window

0
Comment
Question by:reesee324
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35142379
You should use the strcpy function to copy the string from buffer to table.rec[counter].state.  Something like this:

strcpy(table.rec[counter].state, buffer);

Open in new window


BTW, you should do the same for cust_name.

I hope this helps.
0
 

Author Closing Comment

by:reesee324
ID: 35143336
Thanks for the help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now