• C

File operations

I have a loop that determines whether to continue or quit based on user input when the continue option is executed the program starts over but it no longer waits for the user to input a filename. I goes straight to the error message as if the file==NULL when I printed out the value for the file name it was indeed equalled to NULL this only happens the second time the loop executes can any one tell me why this is happening?
Who is Participating?
ozoConnect With a Mentor Commented:
could you please post the code snippet where u r having tht problem
wkellyaAuthor Commented:
heres the code:

printf("Enter the name and path of a .TXT file e.g. C:\\<filename>.txt:\n");
                  fgets(fname, 128, stdin);
       if((s = strchr(fname,'\n')) != NULL) *s = '\0';/* remove newline char */

                  printf("Cannot open file.\n");/* Jumps right to here without waiting for input*/
                 return EXIT_FAILURE;
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

wkellyaAuthor Commented:
heres the loop condition:
      }/*end else*/
            printf("Enter Q to quit, C to continue >");
            scanf(" %c", &quit);

      }while ((quit != 'Q') && (quit != 'q'));  
  }while(1);/*Everything above is true*/
Hello wkellya
i would suggest you to do the following modifications.
declare quit as int

int quit;
int quit2;

in the else do this
 printf("Enter 1 to quit, 2 to continue>");
quit = quit2;
}while(quit != 1);

these following modifications worked with me. i dont know the real problem why u r getting the problem.
wkellyaAuthor Commented:
nope it still isn't alowing me to leave the loop what I should have said is that I have another while loop that allows the user to start over if the file stats are invalid. I wanted to wrap another do while around the whole thing

   }while(1)/*Everything done in the if is true*/
}while((fname!="Quit")||(fname !="quit"))

but when i type quit in for a filename it gets accepted and goes to "Cannot find file" I just cant get out of the loop.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.