Solved

File operations

Posted on 2006-11-18
6
189 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:wkellya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:numansiddique
ID: 17973986
could you please post the code snippet where u r having tht problem
0
 

Author Comment

by:wkellya
ID: 17974213
heres the code:

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

            if(fp==NULL)
                  {
                  printf("Cannot open file.\n");/* Jumps right to here without waiting for input*/
                 return EXIT_FAILURE;
                        system("pause");
                  }
            else
0
 

Author Comment

by:wkellya
ID: 17974436
heres the loop condition:
      }/*end else*/
            printf("\n\n");
            printf("Enter Q to quit, C to continue >");
            scanf(" %c", &quit);

      }while ((quit != 'Q') && (quit != 'q'));  
  }while(1);/*Everything above is true*/
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:numansiddique
ID: 17975094
Hello wkellya
i would suggest you to do the following modifications.
declare quit as int

int quit;
int quit2;

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

these following modifications worked with me. i dont know the real problem why u r getting the problem.
0
 

Author Comment

by:wkellya
ID: 17975863
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

do{
 do{
 
   }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.
0
 
LVL 84

Accepted Solution

by:
ozo earned 75 total points
ID: 17976786
while(strcmp(fname,"Quit")||strcmp(fname,"quit"))
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question