Solved

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

Posted on 2011-03-15
2
311 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
Comment Utility
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
Comment Utility
Thanks for the help!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

762 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

12 Experts available now in Live!

Get 1:1 Help Now