c - Is it necessary to pass the file pointer to main? -
i quite new c programming , have make program asks user enter file name open , open file , print values sorted.
will need pass file pointer main, or can open file in 1 function , can work file throughout other functions?
int getfile () { char file_name[100]; file* fp; int rc; printf("enter file name: "); rc = scanf("%s", file_name); if (rc != 1) printf ("error"); fp = fopen(file_name, "r"); return 0; } do have pass file pointer here main?
just pass file name argument main function, maybe want. might use int main(int argc, char* argv[]), example:
int main(int argc, char* argv[]) { file* fp; if (argc == 1) printf("usage : a.out filename\n"); else { if (fp = fopen(*++argv, "r") != null) { /*your code here*/ } } return 0; }
Comments
Post a Comment