Solved

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

Posted on 2011-03-15
2
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

749 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