954,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Basic file handing

I am still working on the lovely trie project, now I am working on file input that will be passed into the trie. I receive the following errors when I compile this code.

$ make
gcc -Wall -g   -c -o spellback.o spellback.c
spellback.c: In function ‘main’:
spellback.c:36: warning: implicit declaration of function ‘open’
spellback.c:36: warning: assignment makes pointer from integer without a cast
spellback.c:16: warning: unused variable ‘line’
gcc -Wall -g -o simplespell node.o spellback.o


I would like to be able to remove these warnings but the more pressing issue is this bus error:

$ ./simplespell -w filer.c
Opening default file
Closed default file
Opening selected file
Bus error


And of course here is the source code for this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include "node.h"

#define MAXSIZE 2048

int main(int argc, char *argv[]){
	int ck;
	ck = argc;
	//printf("%d\n", ck);
	
	FILE *fp;
	char line [MAXSIZE];
	
	if(ck == 1){
		printf("Opening default file\n");
		fp = fopen(".WORDS", "r");
		fclose(fp);
		printf("Closed default file\n");
		
	}
	
	if(ck == 2){
		printf("Invalid Input\n");
	}
	
	if(ck == 3){
		printf("Opening default file\n");
		fp = fopen(".WORDS", "r");
		fclose(fp);
		//point fp to next file
		printf("Closed default file\n");
		fp = open(argv[2], "r");
		printf("Opening selected file\n");
		fclose(fp);
		printf("Closed selected file\n");
	}
	
	return 0;
}


Any idea what is causing this bus error? Also the c file does exist and I do have read permissions. As does .WORDS .

thanatosys
Newbie Poster
15 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

on 36. line you have fp = open(argv[2], "r"); it should be fp = fopen(argv[2], "r");
Else you have declared variable char line[MAXSIZE]; but you don't use it in your code.

vl4kn0
Light Poster
26 posts since Mar 2008
Reputation Points: 27
Solved Threads: 4
 

All good, I realized the one about the line, it will get used later so I am ok with that warning for now. Thanks again for pointing out a typeo :) , I need to be a bit more careful it seems.

thanatosys
Newbie Poster
15 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You