0

I am trying to write a program where i want to create a file using the id given by the user but is the file already exists then i want my program not to overwrite it but show some message like already exists. how can i do that.

#include<stdio.h>
#include<stdlib.h>
struct student_info{
    char fName[10],lName[10],depart[10],student_id[10];
    long borrowed_books[5];

};

int main(){
   struct student_info student;
   FILE *fpt;
   printf("enter your id:\n");
   scanf("%s",student.student_id);
   printf("enter your first name:\n");
   scanf("%s",student.fName);
   printf("enter your last name:\n");
   scanf("%s",student.lName);
   printf("enter your department:\n");
   scanf("%s",student.depart);

   fpt=fopen(student.student_id,"w");


fclose(fpt);
}
Sudarshan
  • 96
  • 6
  • https://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c – yano Mar 02 '20 at 16:57
  • recommend checking if the file exists ASAP before continuing on and letting the user enter the remaining information ... that's a waste of time if the info will simply be discarded. – yano Mar 02 '20 at 16:58
  • 1
    @yano Yeah.. I hate these websites where they are asking for tons of information only to reject it for some stupid reason after you hit "submit"... – Eugene Sh. Mar 02 '20 at 17:02
  • Does this answer your question? [What's the best way to check if a file exists in C?](https://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c) – Václav Mar 02 '20 at 17:04

0 Answers0