Solved

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

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

Independent Software Vendors: 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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

695 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