Bus Error Fopen
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Bus error in simple C program up vote 1 down vote favorite I have the following simple program. And I am running "program file.txt" from the command line and getting "Bus error" with no output. I don't know what I'm doing wrong here. #include int main(char *arg[]) { FILE *file = fopen(arg[0], "r"); if (file == NULL) { printf("Cannot open file.\n"); exit(1); } return 1; } c bus-error share|improve this question asked Jun 5 '12 at 2:51 Jack Kobil 82 add a comment| 4 Answers 4 active oldest votes up vote 2 down vote accepted could you print out the value of arg[0], suppose the type of main is int main(int argc, char* argv[]) and the argv[0] is the name of process, the argv[1] is first argument. share|improve this answer answered Jun 5 '12 at 2:56 RolandXu 1,6771717 No, I also had trouble printing out arg[0]. –Jack Kobil Jun 5 '12 at 2:57 2 so, change you main type to int main(int argc, char* argv[]) –RolandXu Jun 5 '12 at 2:59 Okay, thank you! Worked perfectly! Changed to your suggestion and used argv[1] and it worked. –Jack Kobil Jun 5 '12 at 3:00 add a comment| up vote 3 down vote The prototype for the c entry function is int main(int argc, char *arg[]); So with your prototype you are actually trying to dereference an int to passing to fopen Try this instead #include int main(int argc, char *argv[]) { FILE *file = fopen(argv[1], "r"); if (file == NULL) { printf("Cannot open file.\n"); exit(1); } return 1; } share|improve this answer edited Jun 5 '12 at 3:07 answered Jun 5
Programming Boards C Programming Bus error when reading input from file Getting started with C or C++ | C Tutorial | C++ Tutorial | C and C++ FAQ | Get a compiler | Fixes for common problems Thread: Bus error when reading input from file Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 03-27-2008 #1 gkoenig View Profile View Forum Posts Registered User Join Date Feb 2008 Posts 77 Bus http://stackoverflow.com/questions/10890963/bus-error-in-simple-c-program error when reading input from file Hello to all, I am receiving a bus error when I run the following code: Code: #include #include #include #include typedef struct { char *name ; char *data ; int length ; int type ; } sequence ; sequence loadedSequences[1000] ; int nSequences = 0 ; int main() { char buffer[1000] ; char *command http://cboard.cprogramming.com/c-programming/100942-bus-error-when-reading-input-file.html ; char *filename, *list_num ; for(;;) { printf("SeqTool> ") ; fpurge(stdin) ; fgets(buffer, 1000, stdin) ; fpurge(stdin) ; // Eliminating the newline character char *p = strchr(buffer, '\n') ; if (p) { *p = '\0' ; } command = strtok( buffer, " \t\n" ) ; ////////////////// Load sequence in FASTA format ////////////////////////// if( (strcmp ( command, "read" )) == 0 ) { char *fileName ; fileName = strtok(NULL, " \t\n" ) ; FILE *input = fopen(filename, "r") ; fgets( buffer, 1000, input) ; char *seqName ; seqName = &buffer[1] ; // After the > in FASTA format loadedSequences[nSequences].name = (char *) malloc( ( strlen(seqName) + 1 ) * sizeof( char ) ) ; strcpy( loadedSequences[nSequences].name , seqName ) ; ++nSequences ; } What I am trying to do is read in from a standardized format (FASTA) and collect the data so that I can manipulate it later if needed. The syntax for the command should be: SeqTool> read Any help would be great. 03-27-2008 #2 matsp View Profile View Forum Posts Kernel hacker Join Date Jul 2007 Location Farncombe, Surrey, England Posts 15,677 What if "command" after