Thread: Why is it not working

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    Why is it not working

    It show a errror on the void read function with the { as the error. What do i nedd to do to fix it?

    Code:
    #include <stdio.h>
    #include <stdlib.h>            //malloc is usually in here
    #include <string.h>
    
    int list_length = 0;           //keeps track of the number of names
    
    void read_file(char **nameslist);
    void save(char **list);
    void sort_list(char**list,int n);
    
    int main() {
        char *list[60];             //creates a list of 60 char pointers
        char choice;
        read_file(list);            //reads the names from the file
    
       FILE*f1;
       f1=fopen("nameslist.txt","w");
       if(f1== NULL){
           printf("Can't open nameslist.txt");
           exit(0);
    }
    
    /*****************************************************
     function: reads names in from a file
     parameters: list is an array of char pointers
     returns: none
     comments:Reads the names in from the file then it
              arranges the names and put it in a file.
    ******************************************************/
    
    void read_file(char **list){
        int i;
        char fname[60], mname[60], lname[60];
        for(i = 0; fscanf(p,"%s %s %s", &fname, &mname, &lname) == 3; i++) { //reads in the names as first,middle,and last
            list[1] = malloc(strlen(fname) + strlen(mname)+ strlen(lname) + 3);
            strcpy(list[1],fname);                    //arranges the names as "first middle last"
            strcat(list[1]," ");
            strcat(list[1],mname);
            strcat(list[1]," ");
            strcat(list[1],lname);
        }
    
            mname[0]
            mname[1]='.'
            mname[2]='\0'
    
        list[2] = malloc(strlen(lname)  + strlen(mname) + srtlen(fname) + 3);
            strcpy(list[2],lname);
            strcat(list[2]," ");
            strcat(list[2],mname);
            strcat(list[2]," ");
            strcat(list[2],fname);
    
        FILE *fp=fopen("nameslist.txt","r+");
        FILE *fp2=fopen("phonebook.txt","w");
        if((fp == NULL)||(fp2 == NULL)){
            printf("\nError opening files.\n");
            exit(0);
        }
    
    }
    
    
    }
    /********************************************************
     function: arranges the names in list so that the last
               name is replaced by its initial
     parameters: list is an array of char pointers
     returns: none
     comments: This functions sorts the names alphabetically
               and then saves them in a file.
    ********************************************************/
    
    void sort_list(char**list,int n){
        int i,j;
        char temp[30];
        for(i=1;i<n;i++){
            strcpy(temp,list[i]);
            for(j=i;(j>0)&&(strcmp(list[j-1],temp)>=0);j--){
                strcpy(list[j],list[j-1]);
            }
            strcpy(list[j],temp);
        }
    
          FILE *fp=fopen("nameslist.txt","r+");
          FILE *fp2=fopen("alphabet.txt","w");
           if((fp == NULL)||(fp2 == NULL)){
            printf("\nError opening files.\n");
            exit(0);
        }
    
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    This is in error:
    Code:
           mname[0]
            mname[1]='.'
            mname[2]='\0'
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM