943,948 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 733
  • C RSS
Oct 27th, 2008
0

Basic file handing

Expand Post »
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.
  1. $ make
  2. gcc -Wall -g -c -o spellback.o spellback.c
  3. spellback.c: In function ‘main’:
  4. spellback.c:36: warning: implicit declaration of function ‘open’
  5. spellback.c:36: warning: assignment makes pointer from integer without a cast
  6. spellback.c:16: warning: unused variable ‘line’
  7. 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:
  1. $ ./simplespell -w filer.c
  2. Opening default file
  3. Closed default file
  4. Opening selected file
  5. Bus error

And of course here is the source code for this:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdbool.h>
  6. #include "node.h"
  7.  
  8. #define MAXSIZE 2048
  9.  
  10. int main(int argc, char *argv[]){
  11. int ck;
  12. ck = argc;
  13. //printf("%d\n", ck);
  14.  
  15. FILE *fp;
  16. char line [MAXSIZE];
  17.  
  18. if(ck == 1){
  19. printf("Opening default file\n");
  20. fp = fopen(".WORDS", "r");
  21. fclose(fp);
  22. printf("Closed default file\n");
  23.  
  24. }
  25.  
  26. if(ck == 2){
  27. printf("Invalid Input\n");
  28. }
  29.  
  30. if(ck == 3){
  31. printf("Opening default file\n");
  32. fp = fopen(".WORDS", "r");
  33. fclose(fp);
  34. //point fp to next file
  35. printf("Closed default file\n");
  36. fp = open(argv[2], "r");
  37. printf("Opening selected file\n");
  38. fclose(fp);
  39. printf("Closed selected file\n");
  40. }
  41.  
  42. return 0;
  43. }

Any idea what is causing this bus error? Also the c file does exist and I do have read permissions. As does .WORDS .
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thanatosys is offline Offline
15 posts
since Oct 2008
Oct 27th, 2008
1

Re: Basic file handing

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.
Reputation Points: 27
Solved Threads: 4
Light Poster
vl4kn0 is offline Offline
26 posts
since Mar 2008
Oct 27th, 2008
0

Re: Basic file handing

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thanatosys is offline Offline
15 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Segmentation fault C
Next Thread in C Forum Timeline: program to read directories in UNIX help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC