icable

This is a discussion on icable within the C Programming forums, part of the General Programming Boards category; does anyone tell me what's wrong with the program? Code: #include<stdio.h> #include<stdlib.h> #include<time.h> #include<ctype.h> #include<string.h> struct llist { char word[20]; ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    3

    Unhappy icable

    does anyone tell me what's wrong with the program?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<ctype.h>
    #include<string.h>
    
    struct llist                      
    {
                        
       char word[20];                
       struct llist *next;            
    };
    typedef struct llist node;      
    typedef node *llink;
     
    llink insertnode(llink head,llink ptr,char str[20])
    {
    	llink newnode;
    	newnode = (llink)malloc(sizeof(node));
    		if(!newnode)
    			return NULL;
    		newnode->word=str;
    		newnode->next=NULL;
    		newnode->next=ptr->next;
    		ptr->next=newnode;
    		return head;
    }
    
    llink deletenode(llink head,llink ptr)
    {
    	llink previous;
    	previous=head;
    	while(previous->next!=ptr)
    		previous=previous->next;
    	previous->next=ptr->next;
    	free(ptr);
    		return head;
    }
    llink findnode(llink head,char str[20])
    {
    	llink ptr;
    	ptr=head;
    	do
    	{
    		if (ptr->word==str)
    			return ptr;
    		ptr->next;
    	}while (head!=ptr && head!=head->next);
    	return NULL;
    }
    int main(void)
    {
      
       FILE *infile  = fopen("wap.txt","r");
       FILE *outfile = fopen("wordlist.txt","w");
       FILE *infile1 = fopen("wordlist.txt","r");
       FILE *outfile1 = fopen("wordlist1.txt","w");
      
       llink head;                    
       llink ptr;
         
       int i=0,count=0;
    
       if ( infile != NULL && outfile != NULL )
       {
          int terminate = 0;
          for ( ;; )
          {
             int ch = fgetc(infile);
             if ( ch == EOF )
             {
                break;
             }
             if ( isalpha(ch) )
             {
                fputc(ch, outfile);
                terminate = 1; /* okay to add newline on next space */
             }
             else if ( isspace(ch) && terminate )
             {
                fputc('\n', outfile);
                terminate = 0;
             }
          }
          
          fclose(infile);
          fclose(outfile);
       head = ( llink ) malloc(sizeof(node)); 
       head->next = NULL;            
       ptr = head;     
       while(!feof(infile1))  
       {
           
           fscanf(infile1,"%s",ptr->word);     
           ptr->next = ( llink ) malloc(sizeof(node));
           ptr->next->next = NULL;   
           ptr = ptr->next;
    	   count++;
       }
      
       ptr = head;                   
       while(i<count-1)
       {
         fprintf(outfile1,"%s\n",ptr->word);
         ptr = ptr->next; 
    	 i++;
       }
       
      fclose(infile1);
      fclose(outfile1);
     
       }
       return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>anyone tell me what's wrong with the program?
    Be a bit more descriptive.. pleeeeaassee!
    What do you think is wrong with it, what are the symptoms etc.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    if (ptr->word==str)
    You can't compare strings this way unless it's technically the same string, which I doubt it will be in your program. Use strcmp(char*, char*). It returns 0 if both strings are equal.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Also, you should check if fopen() succeeds or fails before doing anything.
    Another thing, is it really good to have both a write-file and a read-file pointer to the same file at the same time? I highly doubt it!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    3

    Thx

    thx for your help!!!!
    now i know how to do

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21