files and linked list.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • inboxforsumit
    New Member
    • Feb 2008
    • 1

    files and linked list.

    Hello friends...
    I have to submit an asginment on the following topic.
    Write a program to scan a text file for unique words and store these words alphabetically in a linked list. You have to take care of all the puncutation marks . For example "my name is Sumit. What is your name? " here we have to scan the file till we find space,a comma,question mark or some other punctuation mark and all the characters before that forms a word . I have written the following code but the problem is that my code also modifies the text file. Please help me to modify my code so that it is used to scan the text file and store the unique words in a linked list in sorted order without modifying the text file....... Please help........

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<alloc.h>
    #include<process.h>
    #include<dos.h>
    
    struct node
    {
    char value[15];
    struct node * next;
    };
    
    typedef struct node * nodeptr;
    void addnode(nodeptr *,char []);
    void myinfo();
    
    void main()
    {
    
    nodeptr front=NULL,temp,p,q;
    FILE *outfile;
    int c,count=1; char ch;
    char *filename;
    char *type; char str[15],tmp[15];
    filename=(char *)malloc(sizeof(char)*30);
    type=(char *)malloc(sizeof(char)*10);
    myinfo();
    clrscr();
    puts("ENTER FILENAME ");
    puts("--------------");
    gets(filename);
    puts("\nENTER MODE OF WRITING ");
    puts("---------------------");
    gets(type);
    
    if((outfile=fopen(filename,type))==NULL)
    {
    puts("ERROR");
    exit(1);
    }
    
    puts("\nENTER TEXT TO THE FILE: USE ^Z TO END");
    puts("-------------------------------------");
    do
    {
    c=fgetc(stdin);
    if(c=='.'||c==','||c=='?'||c==';'||c=='"')
    {
    c=' ';
    fputc(c,outfile);
    }
    else
    fputc(c,outfile);
    }while(c!=EOF);
    
    rewind(outfile);
    outfile=fopen(filename,"r");
    while(!feof(outfile))
    {
    fscanf(outfile,"%s",str);
    
    
    temp=front;
    
    if(temp==NULL)
    {
    addnode(&front,str);
    }
    else
    {
    while(temp!=NULL)
    {
    if(strcmp(str,temp->value)==0)
    {
    count=0;
    break;
    
    }
    temp=temp->next;
    }
    
    if(count==1)
    {
    addnode(&front,str);
    }
    else count=1;
    }
    
    }
    
    /* SORTING PART */
    
    for(p=front;p->next!=NULL;p=p->next)
    {
    for(q=p->next;q!=NULL;q=q->next)
    {
    if(strcmp(p->value,q->value)>0)
    {
    strcpy(tmp,q->value);
    strcpy(q->value,p->value);
    strcpy(p->value,tmp);
    }
    }
    }
    
    
    /* DISPLAYING SORTED LIST */
    
    puts("\nUNIQUE ELEMENTS IN ALPHABETICAL/ASCENDING ORDER");
    puts("-----------------------------------------------");
    p=front;
    while(p!=NULL)
    {
    printf("%s\t",p->value);
    p=p->next;
    
    }
    
    
    
    
    fclose(outfile);
    getch();
    
    }
    
    
    void addnode(nodeptr *q,char val[])
    {
    nodeptr p=NULL,r;
    p=(nodeptr)malloc(sizeof(struct node));
    strcpy(p->value,val);
    
    if(*q==NULL)
    {
    p->next=NULL;
    *q=p;
    }
    else
    {
    r=*q;
    while(r->next!=NULL)
    r=r->next;
    p->next=NULL;
    r->next=p;
    }
    
    }
    
    void myinfo()
    {
    
    int i;
    char info1[55]="A PROGRAM TO READ A TEXT FILE AND TO STORE UNIQUE WORDS";
    char info2[48]="IN A LINKED LIST IN ALPHABETICAL/ASCENDING ORDER";
    char info3[42]="DESIGNED BY SUMIT GAUR. MCA-I ROLL NO 4019";
    clrscr();gotoxy(12,10);
    for(i=0;i<55;i++)
    {
    printf("%c",info1[i]);
    delay(100);
    }
    gotoxy(15,11);
    for(i=0;i<48;i++)
    {
    printf("%c",info2[i]);
    delay(100);
    }
    
    gotoxy(17,13);
    for(i=0;i<42;i++)
    {
    printf("%c",info3[i]);
    delay(100);
    }
    delay(5000);
    
    }
    Last edited by RedSon; Feb 28 '08, 03:30 PM.
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Please enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [code] tags in future.

    MODERATOR

    Comment

    Working...