1

I'm starting to do things in C, because it gets a lot of attention this language. I did a word generator with numbers and letters mixed. The words are of length 8 and contain specific characters.

I ask for help because I have done the code that generates the words correctly, and every 20,000 words generated saves the last word in txt, and when you start it, it detects that word and displays it in console.

What I like to do is get that last word and send it to cycle to cycle, if any word, start of the word, not the beginning.

Here you have the code and thank you in advance to anyone who can offer help.

#include <stdio.h>
#include <stdlib.h>
int main(int main, char *argv[]){
int a,b,c,d,e,f,g,h;
char var0[] = {'1','A','2','B','3','C','4','D','5','E','6'};
char var1[] = {'1','A','2','B','3','C','4','D','5','E','6'};
char var2[] = {'1','A','2','B','3','C','4','D','5','E','6'};
char var3[] = {'1','A','2','B','3','C','4','D','5','E','6'};
char var4[] = {'1','A','2','B','3','C','4','D','5','E','6'};
char var5[] = {'1','A','2','B','3','C','4','D','5','E','6'};
char var6[] = {'1','A','2','B','3','C','4','D','5','E','6'};
char var7[] = {'1','A','2','B','3','C','4','D','5','E','6'};
char caracteres[9];
FILE *fp, *sesion;
sesion = fopen("Save_Sesion.txt","r");
if (sesion == NULL){
printf("\nNo Sesion.\n");
}
else{
printf("\nLast key was:\n\n");
  while (feof(sesion) == 0){
  fgets(caracteres,9,sesion);
  printf("%s",caracteres);
  }
}
printf("Press any key to continue\n");
getchar();
for( a=0; a <= 10; a++){
  for( b=0; b <= 10; b++){
    for( c=0; c <= 10; c++){
      for( d=0; d <= 10; d++){
        for( e=0; e <= 10; e++){
          for( f=0; f <= 10; f++){
            for( g=0; g <= 10; g++){
              for( h=0; h <= 10; h++){
                 printf("%c%c%c%c%c%c%c%c\n",var0[a],var1[b],var2[c],var3[d],var4[e],var5[f],var6[g],var7[h]);
              } 
            }
          } 
        }
        fp = fopen("Save_Sesion.txt","w");
        fprintf(fp, "%c%c%c%c%c%c%c%c\n",var0[a],var1[b],var2[c],var3[d],var4[e],var5[f],var6[g],var7[h]);
        fclose(fp);
      } 
    } 
  } 
}
return 0;
}
Mat
  • 202,337
  • 40
  • 393
  • 406
Canna
  • 29
  • 1
  • 3
  • "I'm starting to do things in C, because it gets a lot of attention this language." - Really? I'd have thought there were many more 'modern' languages getting more attention. – Mitch Wheat Sep 14 '13 at 12:02
  • @MitchWheat Oh I see what you did there with those quotes... :P –  Sep 14 '13 at 12:07
  • Why are you using feof() as a condition for the while loop. It's not good practise. Check this link http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wronc – Vivek S Sep 14 '13 at 13:39
  • Also, can you be more specific as to what you wish to accomplish ? explain "What I like to do is get that last word and send it to cycle to cycle, if any word, start of the word, not the beginning." – Vivek S Sep 14 '13 at 14:03

1 Answers1

1

Couple of problem I notices with your code..

You are writing to the file Save_sesion.txt after 14641 iterations which is exactly 11^4. The for loops iterate from 0 to 10, i.e 11 iterations.

When you write to the file, loop vairables, e, f, g and h all would have become greater than 10 and hence you would be indexing at the 11th position into arrays var4, var5, var6 and var7 which results in array index out of bounds.

Vivek S
  • 1,251
  • 10
  • 12
  • Thanks for answering and sorry for my bad English. Thanks for the advice feof, I removed and it is better. I have reviewed the generation of words and do it properly. I know the word which is stored in Save_Sesion.txt restart their last four variables to 1, but I do not mind this issue while the generation is done correctly. I'm content to re-start of the word that is stored in Save_Sesion.txt Thanks for responding – Canna Sep 14 '13 at 14:30