Error in a loop ) plz help =X

aux=word[0];
for(i=0;i<155;i++)
{
word[i]=word[i+1];
word[i+1]=aux;

}


Im running this loop but i cant seem too change the values in the array .... Its an array of chars ...

for(i=0;i<155;i++)
{
word[i]=fgetc(fpin);
}

I know some people bring codes so you can make them their homework but i cant really make this program work ...

Im pulling chars from a file to the array ... scrambling the array and then putting into another file the result ... like a scrambling program ...



fputs(word,fpout);
Last edited on
Hi

I think you dont want to do some thing like

1
2
3
4
5
6
7
aux=word[0];
for(i=0;i<155;i++) 
{
word[i]=word[i+1];
word[i+1]=aux;

}


You may want your code to do some thing like following


1
2
3
4
5
6
for(i=0;i<155;i++) 
{
      aux=word[i];
      word[i]=word[i+1];
       word[i+1]=aux;
}


Or you may want your code to do

1
2
3
4
5
6
7
8
aux1=word[0];
for(i=0;i<154;i++) 
{
      aux=word[i];
      word[i]=word[i+1];
       word[i+1]=aux;
}
 word[154] = aux1;


And if the size of word is 155, you should change the loop as for(i=0;i<154;i++)
Last edited on
even if i make the changes the output of the fpout file is still like the fpin... I just want to copy file to an array and then send it remixed ... like you write "hello world " in fpin file and fpout file will be like "Ollew orldw"
Could you post all your code and please put them in code syntax ?

main()

{
int i;
char word[155];
FILE *fpin;
FILE *fpout;
char aux;

fpout = fopen("mistura.txt", "w");
fpin = fopen ("frases.txt", "r");

if (fpin == NULL)
{
printf("Erro ao abrir o ficheiro.");
exit(1);
}


if(fpout == NULL)
{
printf("Erro ao abrir o ficheiro.");
exit(1);
}


for(i=0;i<155;i++)
{
word[i]=fgetc(fpin);
}


aux=word[0];
for(i=0;i<154;i++)
{
aux=word[i];
word[i]=word[i+1];
word[i+1]=aux;
}



fputs(word,fpout);



printf("OK\n");
fclose(fpin);
fclose(fpout);
system("PAUSE");
}
i dont know what code syntax means :X im sorry ...
i only know the basics of c# programming but i know its a rather simple code ... But that type of guy if you get stuck in something for like 6 hours your not going anywhere ...


I just need to copy chars from file to an array (thats why i have 155 in array size ) its the total chars i want to copy ... But the for cicle doesnt do anithyng ... Doesnt do what i want it to do ...
change the loop, the one where you read the file into
1
2
3
4
5
6
7
8
int size= 0;
while (!feof(fpin) )
{
        if(size >= 155)
              break;
	word[size]=fgetc(fpin);
	size++;
}


change your loop, the one which tries to mix up the word, into

1
2
3
4
5
6
7
8
9

for(int i = 0; i < size; i++){
	    int l1 = rand() % size;
	   int  l2 = rand() % size;
	    aux = word[l1];
	    word[l1] = word[l2];
	    word[l2] = aux;
}
word[size]='\n';



include #include <stdlib.h> #include <time.h>

and add srand(time(NULL)); some where on the top of your code
it should work now
Last edited on
thanks
Topic archived. No new replies allowed.