Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bus error with getchar

Status
Not open for further replies.

Kenshi

Technical User
Jan 12, 2002
3
US
I was trying to create a simple C program to take out all the ^M's that appear when you view a Windows text file in Unix. (I'm using FreeBSD by the way.) So far, I've just created enough that it reads character by character and stores anything that's not equal to 0x0D in a buffer. I'll worry about saving it after I get this error fixed. The problem is that when it gets to the line "buffer[counter] = getchar(filep);" it gives me a SIGBUS error. Through much experimentation, I've narrowed it down further. Gdb (a debugger) gave me the exact location in memory where it's faulting. I disassembled it and the instruction at that exact point is "mov %al,(%eax)" or something like that. (I'm not keen on AT&T syntax.) So I would say it's during the assignment that it's getting a bus error. But anyway, here's the code so hopefully you can point out my mistake.

Code:
#include <stdio.h>

int main(int argc,char *argv[])
{
	char *buffer;
	int counter = 0;
	FILE *filep;
	if(argc != 2)
	{
		printf(&quot;You must specify a file to convert.\n&quot;);
		exit(0);
	}
	if(!(filep = fopen(argv[1],&quot;r&quot;)))
	{
		printf(&quot;Couldn't open file.\n&quot;);
		exit(0);
	}
	while(!feof(filep))
	{
		buffer[counter] = (char)fgetc(filep);
		counter++;
		if(buffer[counter-1] = 0x0D)
			counter--;
	}
}
 
Oops, you have defined buffer as a pointer, but have not allocated any space... you need to do something like this:

char *buffer = (char*) new char[5000];

Otherwise, if buffer is just a character buffer, do something like this:

char buffer[5000];


hope this helps.
 
Ok, this is about my tenth time to post this. Hopefully it'll work now that I registered. Yes, that was indeed the problem. I thought when you declared a char pointer, it worked as a dynamic array. That was apparently wrong. Is there an easy way to create a dynamic array? Thanks.
 
Well, in C you can use one of these:

1) dynamically add/remove elements to a linked list
2) malloc and realloc to change the size of the array
3) create a &quot;temporary&quot; larger array, copy the original array to the &quot;temporary&quot; array, delete the original array, assign the &quot;temporary&quot; array to the original array pointer.

There's a performance hit especially for the third option though, so it's not the one to choose in most cases.

In VC++ with MFC, you can use CObArray or CObList which support dynamically increasing or decreasing array or linked list sizes.
 
Thanks. I may look into malloc. For now, I'll just leave it static and just recompile it if I ever need to convert a huge document. Ok, I have one last question. If I can fix this, this baby will work perfectly. I have worked with this for days but can't get it to do right. Since the file actually shrinks, I had to use the ftruncate function to omit the tail of the previous version of the file. (If someone knows a way to set the end of the file to where the stream cursor is, let me know.) But it always leaves a single 0xff byte at the end of the file. I cannot figure out why. Here's my code. It's not commented in the real source since it's very short and I know exactly what does what, so I'll stick some C++ style comments in the relevant parts when I post here.

Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MAX_FILE_SIZE 50000

int main(int argc,char *argv[])
{
	char buffer[MAX_FILE_SIZE];
	unsigned long counter = 0;
	FILE *filep;
	unsigned long new_length;
	if(argc != 2)
	{
		printf(&quot;You must specify a file to convert.\n&quot;);
		exit(0);
	}
	if(!(filep = fopen(argv[1],&quot;r+&quot;)))
	{
		printf(&quot;Couldn't open file.\n&quot;);
		exit(0);
	}
	while(!feof(filep))
	{
		buffer[counter] = (char)fgetc(filep);
		if(buffer[counter] != 0x0D)
			counter++;
	}
	rewind(filep); \\ go back to beginning of file
	fprintf(filep,&quot;%s&quot;,buffer); \\ write new version of file
	new_length = ftell(filep) - 1; \\ file size is cursor minus 1
	ftruncate(fileno(filep),new_length); \\ set eof
	fclose(filep);
	printf(&quot;Done.\n&quot;);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top