Solved

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

Posted on 2011-03-15
2
315 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

792 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