0

I need to write a function that returns every permute of a word in a given text file.
For some reason the output is wrong and I don't really understand why.
However, if instead of writing a function that should check a presnce of a letter (as seen below the function chars(char,char*))
I write the needed letters for check manually
it works as intended.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100

void permute(FILE *fp, char *perm){

int c,i,perm_size=0;
int flag,chars(char,char *);
char permute[MAX];
i=0;
while(perm[i++])
    perm_size++;

    flag=0;
    i=0;

while(!feof(fp)){
    c=fgetc(fp);
    if(chars(c,perm)==0){/*a function that checks c with each one of the permutes chars*/
        flag++;
        permute[i++]=c;
        if (flag == perm_size){/*if the permute is the word's length*/
            permute[i]='\0';
            printf("%s\n",permute);
            flag=0;
          i=0;
          permute[0]='\0';
        }
    }
    else{
      flag=0;
        i=0;
    }   
  }
}


int chars(char ch, char *str){

    while(*str++)
    {
        if(ch==*str)
            return 0;
    }
    return 1;

}

#include "func.h"
#include <string.h>
int main(int argc,char **argv){

FILE *fp; 
char *input,*perm;
char *prog=argv[0];
void permute(FILE *, char *);

if(argc==1)
{
    fprintf(stderr,"%s error: no arguments\n",prog);
    exit(1);
}
input=argv[1];
perm=argv[2];

if(!(fp=fopen(input,"r")))  
{
    fprintf(stderr,"%s error: cannot open file\n",prog);
    exit(1);
}

  permute(fp,perm);

  fclose(fp);
  return 0;
}

input:
./program text chairs
output: nothing

Milo
  • 131
  • 10
  • 3
    What is the input? The expected output? The actual output? Also, read why `while(!feof(file))` is wrong: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Christian Gibbons Jun 04 '18 at 16:41
  • @ChristianGibbons i added an input and an output – Milo Jun 04 '18 at 16:45
  • @milo: According to your program, `text` is the name of a file which contains some words. Don't you think that the *contents* of that file are relevant to your problem description? But you would be well off to try to debug this problem yourself, since you have all the information needed at your fingertips. For reference, you might want to read Eric Lippert's essay [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – rici Jun 04 '18 at 16:53
  • 1
    As Eric suggests, the key to debugging programs and to writing debuggable programs is to break your problem down into small, independent pieces, each with well-defined interfaces. You seem to be avoiding the use of standard library functions for some reason. Perhaps you are required to implement your own. If so, implement them as functions; don't just inline them everywhere you need them. That way you can test them independently. If you did that, you would find that your implementation of `fgets` has a serious flaw. – rici Jun 04 '18 at 16:56
  • @ChristianGibbons: the text contains only few permutation of the word chair, thanks for the link I will try to do it myself – Milo Jun 04 '18 at 16:56
  • @ChristianGibbons in the end I did debuging myself and made it! the answer is that I incermented wrongly a pointer in the function chars(char,char *) – Milo Jun 04 '18 at 17:47
  • It is rici you should be thanking for the help in trying to debug it yourself. – Christian Gibbons Jun 04 '18 at 18:14
  • @rici thank you very much! helped me a ton! – Milo Jun 04 '18 at 18:16

1 Answers1

0

As told - you should really learn debuging your programs, so I did - I incremented wrongly a value in a function chars(char , char *) a good pointer explanation from other user

Milo
  • 131
  • 10