bus error

Dec 21, 2008 at 1:00am
here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>

int main ()
{
	FILE * message;
	FILE * changed;
	int encryptnumber;
	int messagepart;
	int choose;
	
me:
	puts("When your message in in a file named \"message.txt\" press 1 for encrypt or 2 for Decrypt then enter.");
	scanf( "%i" , &choose);
	
	switch (choose) {
// encrypt
		case 1:
			printf("What number do you want to encrypt with? choose any number exept 0 or the encryption will not work:");
			scanf( "%i" , encryptnumber);
			message = fopen("message.txt" , "r");
			changed = fopen("encrypted.txt" , "w");
			for(;;)
			{
				fscanf( message , "%x" , &messagepart);
				messagepart * encryptnumber;
				fprintf( changed, "%x" , messagepart);
			}
			fclose(message);
			fclose(changed);
			break;
// decrypt
		case 2:
			printf("Enter your decrypt number:");
			scanf( "%i" , encryptnumber);
			message = fopen("message.txt" , "r");
			changed = fopen("decrypted.txt" , "w");
			for(;;)
			{
				fscanf( message , "%x" , &messagepart);
				messagepart / encryptnumber;
				fprintf( changed, "%x" , messagepart);
			}
			fclose(message);
			fclose(changed);
			break;
		default:
			puts("Only 1 or 2");
			goto me;
	}
	
}


then when i build and run it gives me a "bus error" anybody no what this is?
im using mac leopard 10.5.4
Last edited on Dec 21, 2008 at 1:03am
Dec 21, 2008 at 1:29am
Your for loops are infinite, so you never stop reading the files.
Dec 23, 2008 at 3:34pm
then shouldn't it at least create "encrypted.txt" ?
would this work?

1
2
3
4
5
6
7
8
9
10
11
for(;;)
{
	fscanf(read , "%x" , &messagepart);
	printf( "%x" , messagepart);
	if(fileinput = EOF)
	{
		break;
	}
	messagepart * encryptnumber;
	fprintf( write , "%x" , messagepart);
}

Last edited on Dec 23, 2008 at 3:45pm
Dec 23, 2008 at 6:08pm
It's possible "message.txt" doesn't exist. when a file is open with "r", it's not created if it doesn't exit.
Dec 24, 2008 at 1:05pm
i made "message.txt" i made shur that wasnt the problem ;)
Dec 24, 2008 at 1:46pm
This bit on line 19 isn't right and is probably the cause of the bus error:
scanf( "%i" , encryptnumber); //error
it should be
scanf( "%i" , &encryptnumber);



Note also:
The following lines don't actually do anything
Line 25: messagepart * encryptnumber; //doesn't accomplish anything

Line 40: messagepart / encryptnumber; //doesn't accomplish anything
Dec 24, 2008 at 5:04pm
He probably meant *= and /=
Dec 24, 2008 at 11:55pm
y dont they do anything? im trying to multiply the encryptnumber variable to the messagepart variable to encrypt it. then the opposite to decrypt it. how can i accomplish this?
Dec 25, 2008 at 12:24am
Yes you are multiplying (or dividing) two values. This will give you a result, but you are not
using that result, therefore 'nothing has been accomplished' - if you see what I mean.

Maybe you meant something like:
messagepart = messagepart * encryptnumber;
or the shorthand version:
messagepart *= encryptnumber;
Dec 25, 2008 at 9:42pm
of course its something simple like that!!!!! but this still dosnt solve my "bus error" problem
Topic archived. No new replies allowed.