Hi, I m facing difficulties in replacing this C code with C++?
For e.g : Can we use Fopen in C++? Please suggest me changes here to make it c++ equivalent! How to write this in C++ --> void fileRead(FILE *ofp);

    void fileScanner(FILE * ifp,int i);
    void fileRead(FILE *ofp);
    int  findString(char s[],char u[]);
    void StoreIndexInFile(FILE *);
    void fileDisplay(FILE *ifp,FILE *ofp);
    typedef struct index
    {
      int id;
      char word[20];
      int  count;
    }indexs;

     indexs index1[100];
     int ic=0;
     char *stop[]={"is","are","as","was","and","this","that","i","where","at","am","a","the","of","on","there","or","which","by","who","to"};
     char *suffix[]={"alize","tional","able","age","aholic","al","wise"};
     char *replace[]={"al","tion"};
     char *in=NULL,*fn[]={NULL};
    void main()
    {
     FILE *ip,*fp;
     int n=0,choice=0,i;
     clrscr();
     do
        {
         printf("\t\t Conflation Algorithm using Files \n\n"
            "\t\t 1.specify the input documents \n"
            "\t\t 2.perfom Conflation Algorithm \n"
            "\t\t 4.show index file content \n"
            "\t\t 0.exit\n"
            "\t\t Enter the choice.....");
          scanf("%d",&choice);
     switch(choice)
     {
      case 1: printf("\n Enter the index file name  \n");
          scanf("%s",in);
          if((ip=fopen(in,"a"))==NULL)
          {
           printf("\n cannot craete inxdex file %s \n",in);exit(1);
          }
         printf("\n Ente how many documents to scan \n");
         scanf("%d",&n);
         for(i=0;i<n;i++)
         {
           printf("\n Enter the file name to scan \n");
           scanf("%s",fn[i]);
         } break;
        case 2:
         for(i=0;i<n;i++)
         {
           if((fp=fopen(fn[i],"r+"))==NULL)
            {
             printf("\n cannot open file %s \n",fn[i]);exit(1);
            }
            else
            {
             fileScanner(fp,i);
            }
         }
      //clrscr();
      StoreIndexInFile(ip);
      break;
      case 4:
      //clrscr();
      fileRead(stdout);break;
      case 0:exit(0);break;
      default : printf("\n Invalid option please enter option 0-4 only\n");
      }
     }while(choice!=0);
     getch();
    }

    void fileRead(FILE *ofp)
    {
    int m;
    printf("\n id  word  count \n");
        for(m=0;m<ic;m++)
       {
        fprintf(ofp,"%d %-20s %d \n",index1[m].id,index1[m].word,index1[m].count);
       }
    }

    void fileDisplay(FILE * ifp ,FILE *ofp)
    {
    int c;
    while((c=fgetc(ifp))!=EOF)
       {
         fputc(c,ofp);
       }
    }

    void fileScanner(FILE * ifp,int did)
    {
     char c,s[20];
     int e=-1,k=-1,l,i=0;
     while((c=getc(ifp))!=EOF)
         {
           if(c!=' ')
          {
            s[i]=c;
            i++;
          }
         else
          {
           s[i]='\0';
           for(l=0;stop[l]!='\0';l++)
        {
         if(strcmp(s,stop[l])==0)
           {
            s[0]='\0'; break;
           }
        }
         if(s[0]!='\0')
         {
           for(l=0;suffix[l]!='\0';l++)
        {
          if((k=findString(s,suffix[l]))!=-1)
            {
              s[k-1]='\0';  break;
             }
        }
             if((replace[l]!=NULL)&&l<3)
            strcat(s,replace[l]);
             if((e=searchEntry(index1,s,ic))!=-1)
             {
               index1[e].count+=1;
               //strcpy(index1[e].id,(char)did);
            //    index1[e].id[index1[e].count-1]=did;
             //   index1[e].id[index1[e].count]='\0';
             }
              else
             {
              index1[ic].id=did;
               //     index1[ic].id[0]=did;index1[ic].id[1]='\0';
              strcpy(index1[ic].word,s);
              index1[ic].count=1;
              ic++;
              }
             }
        i=0;
          }
         }
    }
    void StoreIndexInFile(FILE *ofp)
    {
    int m;
    fprintf(ofp,"\nid word             count \n");
        for(m=0;m<ic;m++)
       {
        fprintf(ofp,"%d %-20s %d \n",index1[m].id,index1[m].word,index1[m].count);
       }
    }

    int findString(char s[],char u[])
     {
      /* using naive string matching algorithm:
         Time complexity :0((n-m+1)m)
         where m.n are length of u and s */
      int i,j,ls,lu;
      // finding the lenth of both scanned string and substring
      for(ls=0;s[ls]!='\0';ls++);
      for(lu=0;u[lu]!='\0';lu++);
     for(i=0;i<ls-lu+1;i++)
       {
          for(j=0;s[i+j]==u[j]&&u[j]!='\0';j++);
             if(u[j]=='\0') return i+1;//substring found
       }
     return -1;  //if substring is not present
     }

    int searchEntry(indexs ind[],char key[],int n)
    {
    indexs *i;
    for(i=ind;i<ind+n;i++)
     {
     if((strcmp(i->word,key)==0))
        {
         return(i-ind);
        }
     }
    return -1;
    }

    void storeIndexInFile(FILE *ifp,indexs ind[],int n)
    {
    indexs *i;
       fprintf(ifp,"\nid word(root)     frequency\n\n");
     for(i=ind;i<ind+n;i++)
       {
        fprintf(ifp,"%d %-20s %d \n",i->id,i->word,i->count);
       }
    }

What's the specific errors? I can see a lot of problems within this code, but, I don't have the time to physically sit down and re-write this, sorry.

what will be fuction prototype of this fuction?

int searchEntry(indexs ind[],char key[],int n)
{
indexs *i;
for(i=ind;i<ind+n;i++)
{
if((strcmp(i->word,key)==0))
{
return(i-ind);
}
}
return -1;
}

I saved this code as .cpp, and i m getting this error!

You're getting what error?

Function prototypes just tell the compiler about the function's return values and parameters. An easy way to create a prototype is to copy the function header and add a semicolon at the end.

int searchEntry(indexs ind[],char key[],int n);

Can we use Fopen in C++?

Yes, but you should probably change all functions/structures in stdio.h to fstream declared in <fstream> header file. If you don't know how to use fstream then search for tutorials -- there are lots of them. Here is one.

for function prototype,just write the data types without the variable names and end the line with semicolon (;)

int searchEntry(indexs[],char[],int);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.