Thread: I need help with working with files & structures C

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    11

    I need help with working with files & structures C

    Hello,

    Please pardon my low level of knowledge in C, I have had sleepless night for this yet no solution, I decided to seek here. My aim is to read from a file which contains person's data and search the file, then print the data in another file and in the screen orderly based on the person's city of residence.

    The file I'm reading from is like this;

    Peter 00812020933 Maardu Tallinn
    James 899387 Avar Tallinn
    Sam 5789277 Ulikool Tartu
    Ola 4847474747 Seoul Flora
    Pascal 673663 Kaja Tallinn

    The code cannot compile bcus I have errors which I need to eradicate.

    And the code is.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /*
    2.	Program will output to file „F2.txt“ first the residents of Tallinn, then Tartu and finally the rest.
    */
    
    
    typedef struct
    {
        char name[15];
        char telephone[11];
    
    
    }Person;
    
    
    typedef struct
    {
        char street[15];
        char city[15];
        Person person;
    
    
    } Address;
    enum { MAX_ROW = 20};
    void convert(char str[]);
    void input(FILE*, Address* address[MAX_ROW]);
    void output(FILE* ip, FILE* op, Address* address[MAX_ROW]);
    
    
    int main(void)
     {
         FILE *ip, *op;
         Address* address[MAX_ROW];
         op = fopen("F2.txt","w");
         ip = fopen("F1.txt","r");
         if (ip==NULL || op==NULL)
         {
             printf("\n cannot open file\n");
             exit(0);
         }
    
    
        input(ip, address);
        output(ip, op, address);
    
    
        fclose(ip);
        fclose(op);
    
    
         return 0;
     }
    
    
    void input(FILE* ip, Address* address[MAX_ROW])
    {
    
    
      int row = 0;
    
    
      while (row < MAX_ROW && fscanf(ip, "%s, %s, %s, %s",
          address[row]->person.name, address[row]->person.telephone, 
          address[row]->street, address[row]->city ) == 4)
      {
          row++;
      }
    
    
    
    
    }
    
    
    void output(FILE* ip, FILE* op, Address* address[MAX_ROW])
    {
        char P[10] = {"Tallinn"};
        char T[10] = {"Tartu"};
        char total[50];
        char fin[MAX_ROW];
       int i,e,r;
       int count = 0;
    
    
      convert(P);
      convert(T);
      for (i=0; i < MAX_ROW; i++ )
      {
          strcpy(total, address[i]->city);
          convert(total);
    
    
           if (strstr(total, P) != NULL )
            {
                fin[count] = address[i]; //stores the data found in this structure array
                count++;
    		}
    	}
                for (e=0; e < MAX_ROW; i++ )
          {
          fprintf(op, "%s\t, %s\t, %s\t, %s\t", 
              fin[e]->person.name, fin[e]->person.telephone, fin[e]->street, fin[e]->city );
          printf("%s\t, %s\t, %s\t, %s\t", fin[e]->person.name, 
              fin[e]->person.telephone, fin[e]->street, fin[e]->city );
            }
    
    
        for (i=0; i < MAX_ROW; i++ )
      {
    
    
             if (strstr(total, T) != NULL )
            {
                address[count] = address[i]; //stores the data found in this structure array
                count++;
            }
      }
                for (r=0; r < MAX_ROW; r++ ){
                     fprintf(op, "%s\t, %s\t, %s\t, %s\t", fin[r]->person.name, 
                        fin[r]->person.telephone, fin[r]->street, fin[r]->city);
                     printf("%s\t, %s\t, %s\t, %s\t", fin[r]->person.name, 
                        fin[r]->person.telephone, fin[r]->street, fin[r]->city);
    
    
            }
    
    
           for (i=0; i < MAX_ROW; i++ )
      {
             if ((strstr(total, P) == NULL ) && (strstr(total, T) == NULL ))
          {
             fprintf(op,"no result found");
             printf("no result found");
          }
      }
    
    
          for (i=0; i < MAX_ROW; i++ )
      {
            {
            address[count] = address[i]; //stores the data found in this structure array
                count++;
            }
      }
                for (r=0; r < MAX_ROW; r++ ){
                     fprintf(op, "%s\t, %s\t, %s\t, %s\t", fin[r]->person.name, 
                        fin[r]->person.telephone, fin[r]->street, fin[r]->city );
                     printf("%s\t, %s\t, %s\t, %s\t", fin[r]->person.name, 
                        fin[r]->person.telephone, fin[r]->street, fin[r]->city );
    
    
            }
    	}
    
    
    
    
    
    
    void convert(char str[])
    {
        int i;
    
    
        for (i = 0; str[i] != '\0'; i++)
        {
            if (str[i] >= 'A' && str[i] <= 'Z') //checks each character and converts it
            {
                str[i] = str[i] + 32;
            }
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,763
    > Address* address[MAX_ROW]
    Remove all the *'s from all the lines looking like this.

    So
    Code:
    void input(FILE*, Address address[MAX_ROW]);
    void output(FILE* ip, FILE* op, Address address[MAX_ROW]);
     
     
    int main(void)
     {
         FILE *ip, *op;
         Address address[MAX_ROW];
    Then change all those address->member references into address.member
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    11

    I Modified the code

    Quote Originally Posted by Salem View Post
    > Address* address[MAX_ROW]
    Remove all the *'s from all the lines looking like this.

    So
    Code:
    void input(FILE*, Address address[MAX_ROW]);
    void output(FILE* ip, FILE* op, Address address[MAX_ROW]);
     
     
    int main(void)
     {
         FILE *ip, *op;
         Address address[MAX_ROW];
    Then change all those address->member references into address.member
    I have modified the code with you help and it compiled but it prints no result found on the screen but nothing in the file F2. Then there is actually data in the file F1. what could be the cause?

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    
    
    typedef struct
    {
        char name[15];
        char telephone[11];
    
    
    }Person;
    
    
    typedef struct
    {
        char street[15];
        char city[15];
        Person person;
    
    
    } Address;
    enum { MAX_ROW = 20};
    void convert(char str[]);
    void input(FILE*, Address address[MAX_ROW]);
    void output(FILE* ip, FILE* op, Address address[MAX_ROW]);
    
    
    int main(void)
     {
         FILE *ip, *op;
         Address address[MAX_ROW];
         op = fopen("F2.txt","w");
         ip = fopen("F1.txt","r");
         if (ip==NULL || op==NULL)
         {
             printf("\n cannot open file\n");
             exit(0);
         }
    
    
        input(ip, address);
        output(ip, op, address);
    
    
        fclose(ip);
        fclose(op);
    
    
         return 0;
     }
    
    
    void input(FILE* ip, Address address[MAX_ROW])
    {
    
    
      int row = 0;
    
    
      while (row < MAX_ROW && fscanf(ip, "%s, %s, %s, %s",address[row].person.name, address[row].person.telephone, address[row].street, address[row].city ) == 4)
      {
          row++;
      }
    
    
    
    
    } char fin[30];
    
    
    void output(FILE* ip, FILE* op, Address address[MAX_ROW])
    {
        char P[10] = {"Tallinn"};
        char T[10] = {"Tartu"};
        char total[50];
        Address M;
        Address* fin = &M;
        int i;
       int count = 0;
    
    
      convert(P);
      convert(T);
      for (i=0; i < MAX_ROW; i++ )
      {
          strcpy(total, address[i].city);
          convert(total);
    
    
           if (strstr(total, P) != NULL )
            {
                fin[count] = address[i];
                count++;
                
                fprintf(op, "%s\t, %s\t, %s\t, %s\t", fin[i].person.name, fin[i].person.telephone, fin[i].street, fin[i].city );
                printf("%s\t, %s\t, %s\t, %s\t", fin[i].person.name, fin[i].person.telephone, fin[i].street, fin[i].city );
    		}
          
    
    
             else if (strstr(total, T) != NULL )
            {
                fin[count] = address[i]; //stores the data found in this structure array
                count++;
                
                fprintf(op, "%s\t, %s\t, %s\t, %s\t", fin[i].person.name, fin[i].person.telephone, fin[i].street, fin[i].city );
                printf("%s\t, %s\t, %s\t, %s\t", fin[i].person.name, fin[i].person.telephone, fin[i].street, fin[i].city );
            }
      
                
    
    
             else if ((strstr(total, P) == NULL ) && (strstr(total, T) == NULL ))
          {
             fprintf(op,"no result found");
             printf("no result found");
          }
      
          else
    
    
            {
                     fprintf(op, "%s\t, %s\t, %s\t, %s\t", fin[i].person.name, fin[i].person.telephone, fin[i].street, fin[i].city );
                     printf("%s\t, %s\t, %s\t, %s\t", fin[i].person.name, fin[i].person.telephone, fin[i].street, fin[i].city );
    
    
            }
    	}
    }
    
    
    
    
    
    
    void convert(char str[])
    {
        int i;
    
    
        for (i = 0; str[i] != '\0'; i++)
        {
            if (str[i] >= 'A' && str[i] <= 'Z')
            {
                str[i] = str[i] + 32;
            }
        }
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,763
    > Address M;
    > Address* fin = &M;
    ...
    > fin[count] = address[i];
    > count++;
    Treating a thing which isn't an array as if it were an array is a bad idea.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    870
    Here is a clean-up of your code.

    'fin' only has space for one address (and it has a global doppleganger for some reason). So once count is incremented past 0 it will be an out-of-bounds access. I have no idea what 'fin' is for.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
     
    typedef struct
    {
        char name[15];
        char telephone[11];
    } Person;
     
    typedef struct
    {
        char street[15];
        char city[15];
        Person person;
    } Address;
     
    enum { MAX_ROW = 20 };
     
    void convert(char *str);
    void input(FILE *ip, Address address[]);
    void output(FILE *op, Address address[]);
     
    int main()
    {
        Address address[MAX_ROW];
        FILE *op = fopen("F2.txt","w");
        FILE *ip = fopen("F1.txt","r");
     
        if (!ip || !op)
        {
            printf("\n cannot open file\n");
            exit(0);
        }
     
        input(ip, address);
        output(op, address);
     
        fclose(ip);
        fclose(op);
        return 0;
    }
     
    void input(FILE *ip, Address address[])
    {
      int row = 0;
      while (row < MAX_ROW
          && fscanf(ip, "%s, %s, %s, %s",
                 address[row].person.name, address[row].person.telephone,
                 address[row].street, address[row].city ) == 4)
          ++row;
    }
     
    char fin[30];
     
    void output(FILE* op, Address address[])
    {
        char P[10] = {"Tallinn"};
        char T[10] = {"Tartu"};
     
        convert(P);
        convert(T);
     
        char total[50];
        Address M;
        Address* fin = &M;
     
        int count = 0;
      
        for (int i = 0; i < MAX_ROW; ++i)
        {
            strcpy(total, address[i].city);
            convert(total);
     
            if (strstr(total, P))
            {
                fin[count] = address[i];
                count++;
            }
            else if (strstr(total, T))
            {
                fin[count] = address[i];
                count++;
            }
            else
            {
                fprintf(op,"no result found");
                printf("no result found");
                continue; // skip the output
            }
     
            fprintf(op, "%s\t, %s\t, %s\t, %s\t",
                fin[i].person.name, fin[i].person.telephone,
                fin[i].street, fin[i].city );
            printf("%s\t, %s\t, %s\t, %s\t",
                fin[i].person.name, fin[i].person.telephone,
                fin[i].street, fin[i].city );
        }
    }
     
    void convert(char *str)
    {
        for ( ; *str; ++str)
            *str = tolower(*str);
    }
    After thinking about it for a few minutes, maybe you meant something like this:
    Code:
    void output(FILE* op, Address address[])
    {
        char P[10] = {"Tallinn"};
        char T[10] = {"Tartu"};
     
        convert(P);
        convert(T);
     
        char city[50];
        int count = 0;
     
        for (int i = 0; i < MAX_ROW; ++i)
        {
            strcpy(city, address[i].city);
            convert(city);
     
            if (strstr(city, P) || strstr(city, T))
            {
                fprintf(op, "%s\t, %s\t, %s\t, %s\t",
                    address[i].person.name, address[i].person.telephone,
                    address[i].street, address[i].city );
                printf("%s\t, %s\t, %s\t, %s\t",
                    address[i].person.name, address[i].person.telephone,
                    address[i].street, address[i].city );
                ++count;
            }
        }
     
        if (count == 0)
        {
            fprintf(op, "no result found\n");
            printf("no result found\n");
        }
        else
            printf("Found %d matches.\n", count);
    }
    Last edited by john.c; 02-23-2020 at 12:25 PM.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Program Working with Structures
    By mroman in forum C++ Programming
    Replies: 6
    Last Post: 11-16-2015, 09:21 PM
  2. working with structures
    By torquemada in forum C Programming
    Replies: 10
    Last Post: 11-12-2011, 06:35 PM
  3. Help working with array of structures!
    By black_stallion in forum C Programming
    Replies: 16
    Last Post: 09-11-2011, 10:23 AM
  4. working with pointers to structures
    By Delia in forum C Programming
    Replies: 2
    Last Post: 03-21-2010, 05:23 PM
  5. Working with arrays of structures, need advice...
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-14-2002, 08:38 AM

Tags for this Thread