Reputation: 13
I am trying to write a program that could read/write the rolodex info (the file will be named myRolodex by default). But I think the line: strcpy(filename,"myRolodex");
causes Segmentation fault (core dumped) in my GCC compiler.
Any help to fix would be grateful!
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include<stdbool.h>
int main(int argc, char* argv[]){
char filename[250];
if (argv[1]==NULL){
strcpy(filename,"myRolodex");
}
strcpy(filename,argv[1]);
printf("%s",filename);
return 0;
}
Upvotes: 1
Views: 798
Reputation: 696
Correct your code as follows
if (argv[1]==NULL){
strcpy(filename,"myRolodex");
}else{
strcpy(filename,argv[1]);
}
The problem is, even if you check argv[1]==NULL, statment strcpy(filename,argv[1]); will be always be executed since it's not in else block.
Upvotes: 2