[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Trouble filling a structure from a file...receiving error message incompatible types in assignment

Posted on 2004-12-01
2
Medium Priority
?
287 Views
Last Modified: 2010-04-15
I am trying to fill the structure STUREC and am experiencing problems with my fscanf statement.  Upon compilation I receive the following message: proj6.c:83: incompatible types in assignment.  Can someone help me determine the source of my error?

My code is as follows:

//Name...
//...
//...
//...
//This program is designed to

#include <stdio.h>
#include <string.h>
#define STR_SIZE 25
#define ARR_SIZE 10
#define TRUE 1
#define FALSE 0

typedef struct
{
   char *month;
   int date;
   int year;
} DATE;

typedef struct
{
   char fName[STR_SIZE];
   char lName[STR_SIZE];
   int gradeLevel;
   char ssn[STR_SIZE];
   DATE birthDate;
}STUREC;

typedef enum {false, true} BOOLEAN;

BOOLEAN openFile(int argc, char *argv[], FILE **fpIn);
int fillArray(FILE *fpIn, STUREC arr[]);
int getMonth(char *temp);
void sortArray(STUREC arr[], int numEls);
void swap(STUREC *stu1, STUREC *stu2);
void printArray(STUREC arr[], int numEls);
void closeFile(FILE *fpIn);

// do not change this function
int main(int argc, char *argv[])
{
   int numEls;
   STUREC arr[ARR_SIZE];
   FILE *fpIn;

   if (openFile(argc, argv, &fpIn))
   {
      numEls = fillArray(fpIn, arr);
      printArray(arr, numEls);
      sortArray(arr, numEls);
      printArray(arr, numEls);
      closeFile(fpIn);
   }

   return 0;
}

BOOLEAN openFile(int argc, char *argv[], FILE **fpIn)
{
   BOOLEAN success=false;
   if (argc >= 2)
   {
      *fpIn = fopen(argv[1], "r");
      if (*fpIn != NULL)
         success = true;
      else
      {
         printf("\nERROR: Unable to open file for reading.\n");
      }
   }
   else
      printf("\nERROR: Not enough command line arguments.\n\n");
   return success;
}

int fillArray (FILE *fpIn, STUREC arr [])
{
   int i;
   STUREC stu;
   for (i=0; i < ARR_SIZE; i++)
   arr [i] = fscanf (fpIn, "%s%s%d%s", stu.fName, stu.lName,
                     &stu.gradeLevel, stu.ssn);
}
0
Comment
Question by:csunnypl2
2 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 200 total points
ID: 12721710
the problem is not fscanf
fscanf returns an integer representing the numbers of fields it has correctly parsed, in this case could be 4
You are trying to assign 4 to arr[i]
I guess you want to copy contents of 'stu' to arr[i], so correct code would be:

int fillArray (FILE *fpIn, STUREC arr [])
{
   int i;
   STUREC stu;
   for (i=0; i < ARR_SIZE; i++)
   fscanf (fpIn, "%s%s%d%s", stu.fName, stu.lName,  &stu.gradeLevel, stu.ssn);
   arr[i] = stu;
/* alternative to previous line:
   memcpy(&arr[i], &stu, sizeof(stu));
*/
}

0
 
LVL 3

Expert Comment

by:baboo_
ID: 12721742
Your problem's here:

arr [i] = fscanf (fpIn, "%s%s%d%s", stu.fName, stu.lName, &stu.gradeLevel, stu.ssn);

fscanf returns an int equal to the number of items it's read.  This line tries to assign that int value to an element in your STUREC array, arr.  This is what you want to do, I think:

fscanf (fpIn, "%s%s%d%s", arr[i].fName, arr[i].lName, &(arr[i].gradeLevel), arr[i].ssn);

And remove STUREC stu from your fillArray method (you don't use it anymore).

baboo_
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 and use pointers 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.
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 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