LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2005, 06:34 PM   #1
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
"for" loop acting strange...


Well, this is probably something totally obvious, but I just can't see it...
I have some code that loops through some command-line arguments and prompts
to remove them if a flag is set (think rm -i ):
Code:
if (in == 1) {
        for (; optind < argc; optind++) {

                printf("rm: remove %s ('y' or 'n')? ", argv[optind]);
                fgets(&response, 2, stdin);
                if (response == 'y' || response == 'Y') 
                       rm(argv[optind]);
        }
}
else
        for (; optind < argc; optind++) {
                printf("removing %s\n", argv[optind]); // printf for debug
                rm(argv[optind]);
        }
The block on the bottom (without "-i" flag set), works as expected, printing the filename then deleting it. But the top block is acting strangely., as it is not stopping to prompt for every second file it loops over. Here is sample output:
Code:
$ ./rm -i foo bar baz
rm: remove foo ('y' or 'n')? y
rm: remove bar ('y' or 'n')? rm: remove baz ('y' or 'n')? y
As you can see, it prompts for first argument fine, then prints the second, but does not prompt, then prompts for the third. It always does this no matter how many arguments. If I pass 2 arguments it prompts for the first, then prints the second prompt but does nothing (ie: the program exits...).

The files that do get prompted are being deleted or not according to the y or n as they should.

Originally I was using scanf instead of fgets, but behavior was exactly the same.

Any ideas? thanks...
 
Old 10-21-2005, 07:05 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
This line is obviously the reason.
Code:
fgets(&response, 2, stdin);
Try replacing it with:
Code:
response=getchar();
getchar(); /* skip Enter */
 
Old 10-22-2005, 02:29 PM   #3
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760

Original Poster
Blog Entries: 4

Rep: Reputation: 78
Thanks jlliagre,

I did not get mail notification of your response, so I ended up figuring it out myself. Indeed, the line-feed was being passed immediately to the next fgets in the loop...

This is working for me now...
Code:
do {              
            response = getchar();
} while (response == '\n');
Thanks again...

I was using fgets, because the manpage says:
"fgets() reads in at most one less than size characters from stream"
as I am passing "2" I thought it would give me a single character 'y' or 'n'.
 
Old 10-23-2005, 03:14 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
There was another issue with your first code, you were passing a pointer to a single character to fgets, but fgets was writing a null to the memory just after this location, overwriting whatever other variable that may be there.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Install stops at "Calibrating delay loop" eam Linux - General 0 07-23-2004 07:39 PM
PHP "for.. loop" to insert into mysql Boby Programming 10 05-29-2004 10:07 AM
What does "loop-mount the disk image" mean ? karans Linux - Distributions 1 10-29-2003 08:21 AM
Site acting "funny" with certain images/links MasterC LQ Suggestions & Feedback 5 09-28-2003 10:24 AM
$LFS "less" acting funny cbates55 Linux - Distributions 1 01-17-2002 08:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration