Thread: help w/structure array and opening reading file

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    24

    help w/structure array and opening reading file

    I'm working on an assignment and am having some problems. The guy taught the 4 chapters in a weak over the core of this assignment when we had done the first 7 chapters in the first 9 weeks.

    I've attached the .pdf file if someone wants to read the assignment. I don't have to sort the data, he changed it.

    I'm on the first part, creating the open file function, Here is what I have in main and I have another file that has the functions in them. Yes, I do have a header file, but I won't include that b/c it's not necessary to troubleshoot at the moment.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX = 20
    
    typedef struct person
    {
        char fname [100];
        char lname [100];
        char gender;
        int age;
        char color [100];
        char hobby [100];
    } person;
    
    
    
    int main(int argc, char *argv[])
    {
        person information [12];
    
    int x;
    int total;
    FILE* fp = NULL;
    
    //struct person array[MAX];
    
    fp = openfile();
    total = readtotal(fp);
    for( x = 0; x <total; x++)
    array[x] = readPerson(fp);
    findMatches(array, total);
    fclose(fp)
    
    
      system("PAUSE");
      return 0;
    }

    Code:
    #include "functions.h"
    
    FILE* openfile()
    {
        FILE *fp = NULL;
    
        fp = fopen("matches.txt", "r");
    
        if (fp == NULL) printf("There is no input file, please create an input file 'matches.txt' and try again");
    
    return fp;
    }
    I understand that there is a TON wrong w/the code. In the main, I have taken a skeleton from notes really and just pasted it in there. I have only edited down to *fp = openfile();

    The rest I haven't even taken a look at. It's just there so I know what to do. I told it to compile to see if I could get past the part I have actually done and it didn't get past *fp = openfile();

    Here is the error I'm getting:

    [Warning] assignment makes pointer from integer without a cast

    Thanks for any help.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Assuming the first paste is your exact program, the problem is that you don't have a declaration for your openfile() function. If you don't declare a function before you use it, the compiler assumes that it returns an int. In this case, you're trying then to convert an int into a FILE*, which requires a diagnostic from your compiler (which it gave you). In your case, you probably just want to #include your functions.h file, but since you didn't paste that file, I can't be certain it has the declaration you need.

    It sounds like you're using gcc. If so, always build with at least the -Wall option. This turns on a number of useful warnings, including telling you when you forget to declare a function. The implicit declaration of functions is a feature of C that's included for backward compatibility and should not be used.

    On another note, in C, if you want to declare a function as taking no arguments, you use (void) for the parameter list, not (). That is:
    Code:
    FILE *openfile(void); /* This is good. */
    FILE *openfile(); /* This is not. */

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    thanks for the help!!! I have been working on this program nonstop for about 7 hours or so.

    Here's what I have in my main.c file, .h file, and another .c that contains my functions, Here they are in that order.

    Here's the problem now. I am trying to read in a file called "matchmaker.txt" and it will NOT read it in. Even if I put the file in directly I always get a return for it not being read "file can not be found" from my program.


    matchmaker.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "matchmaker.h"
    
    int main(int argc, char *argv[])
    {
        int total, x;
        FILE* fp = NULL;
        person* array = NULL;
    
        fp = openfile();
        total = readtotal(fp);
        array = readfile(fp, total);
        fclose(fp);
        findMatch(array, total);
    
        for(x = 0; x < total; x++)
        {
            free(array[x].first);
            array[x].first=NULL;
            free(array[x].last);
            array[x].last=NULL;
            free(array[x].color);
            array[x].color=NULL;
            free(array[x].hobby);
            array[x].hobby=NULL;
        }
        free(array);
        array = NULL;
    
      system("PAUSE");
      return 0;
    }
    header file - "matchmaker.h"

    Code:
    #ifndef matchmaker_H
    #define matchmaker_H
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Person
    {
        char* first;
        char* last;
        char sex;
        int age;
        char* color;
        char* hobby;
    
    };
    
    typedef struct Person person;
    
    
    FILE* openfile();
    
    int readtotal(FILE* fp);
    
    person * readfile(FILE * fp, int total);
    
    void findMatch(person *array, int total);
    
    #endif
    functions file "matchmakerFunc.c"
    Code:
    #include "matchmaker.h"
    
    FILE* openfile(void)
    {
        FILE* fp = NULL;
    
        char filename[100];
        int len = 0;
    
    
        //printf("Enter the name of the file: ");
        fgets(filename, 100, stdin);
        //scanf("%s", filename); //i tried this thinking maybe it could be the fgets causing the problem, but it's not
       
        len = strlen(filename);
       
        if(filename[len-1] = '\n')
            filename[len-1] = '\0';
    
        fp = fopen(filename, "r");
        if(fp == NULL)
        {
            printf("That file does not exist!");
            //exit(-1);
        }
        return fp;
    }
    
    
    int readtotal(FILE* fp)
    {
        int total;
        fscanf(fp, "%i", &total);
        getc(fp);
        return total;
    }
    
    
    person * readfile(FILE* fp, int total)
    {
    
        int x;
        char temp[100];
        person * array = (person*)malloc(total*sizeof(person));
        if(array=NULL)
            exit (-1);
    
        for(x = 0; x < total; x++)
        {
            fgets(temp, 100, fp);
            array[x].first =  (char *)malloc(strlen(temp)*sizeof(char));
            if(array[x].first == NULL)
                exit(-1);
            strncpy(array[x].first, temp, strlen(temp)-1);
    
            fgets(temp, 100, fp);
            array[x].last = (char *)malloc(strlen(temp)*sizeof(char));
            if(array[x].last == NULL)
                exit(-1);
            strncpy(array[x].last, temp, strlen(temp)-1);
    
            fscanf(fp, "%c", &array[x].sex);
            getc(fp);
    
            fscanf(fp, "%i", &array[x].age);
            getc(fp);
    
            fgets(temp, 100, fp);
            array[x].color = (char *)malloc(strlen(temp)*sizeof(char));
            if(array[x].color == NULL)
                exit(-1);
            strncpy(array[x].color, temp, strlen(temp)-1);
    
            fgets(temp, 100, fp);
            array[x].hobby = (char*)malloc(strlen(temp)*sizeof(char));
            if(array[x].hobby == NULL)
                exit(-1);
            strncpy(array[x].hobby, temp, strlen(temp)-1);
    
        }
        return array;
    
    }
    
    
    void findMatch(person * array, int total)
    {
       int outer;
       int inner;
       int found, count1 = 0, count2 = 0;
    
      for(outer=0; outer < total; outer++)
    {
        count1 = 0;
        count2 = 0;
    
        for(inner = 0; inner < total; inner++)
        {
            found = 0;
    
            if(outer != inner)
            {
                if(array[outer].sex != array[inner].sex)
                {
                if(array[outer].age - array[inner].age < 6  || array[outer].age - array[inner].age > -6 ) found++;
                if(strcmp(array[outer].color, array[inner].color) == 0) found++;
                if(strcmp(array[outer].hobby, array[inner].hobby) == 0) found++;
                }
            }
            if(count1 = 0)
            {
            printf("First Name: %s", array[outer].first);
            printf("Last Name: %s", array[outer].last);
            printf("Gender: %i", array[outer].sex);
            printf("Age: %c", array[outer].age);
            printf("Color: %s", array[outer].color);
            printf("Hobby: %s", array[outer].hobby);
    
            printf("\n\nPROSPECTIVE MATCHES\n----------------------------\n\n");
            count1++;
            }
            if(found > 1)
            {
            printf("First Name: %s", array[inner].first);
            printf("\nLast Name: %s", array[inner].last);
            printf("\nGender: %i", array[inner].sex);
            printf("\nAge: %c", array[inner].age);
            printf("\nColor: %s", array[inner].color);
            printf("\nHobby: %s", array[inner].hobby);
            }
        }
    }
    }
    Last edited by live4soccer7; 03-15-2009 at 07:52 PM.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    24

    Talking

    I've even tried something as simple as this and it won't work. I put the .txt file that i'm trying to read in into the directory where my c code is contained. I have tried wxdev and code::blocks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
    
      FILE *pRead;
      char name[10];
      
      pRead = fopen("matches.txt", "r");
    
      if(pRead) printf("\n\nFile cannot be found\n\n");
    
      else printf("\n\nFile has been found and read\n\n");
    
    fscanf(pRead, %s:, name)
    
    while ( !feof(pRead))
    {
        printf("%s\n", name);
        fsacnf(pRead, "%s", name);
      system("PAUSE");
      return 0;
    }
    any help would be super appreciated, I have only a number of hours left before I must submit this assignment. Please.... lol

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    new updated code for simple example, it reads in the file, but it the program crashes and burns w/the window's error report box.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
    
      FILE *pRead;
      char name[10];
    
      pRead = fopen("matches.txt", "r");
    
      if(pRead) printf("\n\nFile cannot be found\n\n");
    
      else printf("\n\nFile has been found and read\n\n");
    
    fscanf(pRead, "%s", name);
    
    while ( !feof(pRead))
    {
        printf("%s\n", name);
        fscanf(pRead, "%s", name);
    }
    
    system("PAUSE");
    return 0;
    
    }

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    In your main program (the large one), I noticed that you're using = (a single equals sign) for comparison, but that doesn't work in C. To compare values, you use == (two equals signs). = is assignment, even in a conditional. I see at least three places you're using = to compare. This will cause problems. Honestly, though, I can't see why your large program won't open a file that exists, because even though you have the = bug there, it won't manifest itself most times. At any rate, I recommend fixing those comparisons, at least; and including string.h wherever you use string functions. As a side note, your strncpy() usage isn't safe, as it won't create a string. I'm not sure what you're trying to achieve (cutting the last character off of the string?) with your code, so I won't make a recommendation on how to fix it at this point, but bear in mind that strncpy() needs to be handled carefully.

    As for your crashing program (the last posted), there are two issues I see. One, you're printing "File not found" if the file opens successfully; your logic is swapped. Plus you don't exit if the file isn't found, so that could cause a crash when you try to read from a null FILE*.

    Next, you're only providing 10 bytes for a string (which means 9 characters plus a null character). If any line of your file is longer than that (and don't forget the newlines), you've got undefined behavior, which might mean a crash. scanf() with %s is as dangerous as gets(). But then, a lot of C is dangerous.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    thanks for the help. I have gone a head and changed the == problem. Just a few typos from the code.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    i have changed the file not found part also

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having troubles reading in file to array
    By belkins in forum C Programming
    Replies: 3
    Last Post: 11-10-2008, 09:58 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. reading from file to an array - help needed!
    By timeforheroes in forum C Programming
    Replies: 2
    Last Post: 04-28-2005, 12:16 PM
  5. Text file to 2D Array PLEASE HELP!
    By lostboy101 in forum C Programming
    Replies: 0
    Last Post: 03-26-2002, 10:51 AM