By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,122 Members | 1,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,122 IT Pros & Developers. It's quick & easy.

Strange fgets() behavior

P: 3
Hello everyone!
While trying to write a simple program for my laboratory exercises I encountered a problem I can't resolve, nor understand.

I want to make a simple function that gets the name of a file, and if it is correct - return it as a pointer; if not - redo the whole loop, so that user can type it once more. The code is:
Expand|Select|Wrap|Line Numbers
  1.     while(1) {
  2.         printf(welcome_string);
  3.         printf("\n");
  4.         fgets(file_name_str, FNAME_BUFFER_SIZE, stdin);
  5.         file_ptr = fopen(file_name_str, file_mode_ptr);
  6.  
  7.         if (file_ptr == NULL) {
  8.             printf("\n");
  9.             printf(BAD_FILE_MSG);
  10.             printf("\n");
  11.  
  12.             if ((temp_char = getc(stdin)) == 'q') {
  13.                 exit(1);
  14.             } else {
  15.                 system("clear");
  16.                 continue;
  17.             }
  18.         }
  19.         return file_ptr;
  20.  
This is the main part, the rest is not important.

The problem is that when I enter any character, instead of a sole enter, the program 'skips' fgets() function and goes to if conditional right away.

I use Windows XP with Cygwin. GCC version is 3.4.4.

Thanks for any tips.
Dec 28 '06 #1
Share this Question
Share on Google+
6 Replies


100+
P: 1,646
Hello everyone!
While trying to write a simple program for my laboratory exercises I encountered a problem I can't resolve, nor understand.

I want to make a simple function that gets the name of a file, and if it is correct - return it as a pointer; if not - redo the whole loop, so that user can type it once more. The code is:
Expand|Select|Wrap|Line Numbers
  1.     while(1) {
  2.         printf(welcome_string);
  3.         printf("\n");
  4.         fgets(file_name_str, FNAME_BUFFER_SIZE, stdin);
  5.         file_ptr = fopen(file_name_str, file_mode_ptr);
  6.  
  7.         if (file_ptr == NULL) {
  8.             printf("\n");
  9.             printf(BAD_FILE_MSG);
  10.             printf("\n");
  11.  
  12.             if ((temp_char = getc(stdin)) == 'q') {
  13.                 exit(1);
  14.             } else {
  15.                 system("clear");
  16.                 continue;
  17.             }
  18.         }
  19.         return file_ptr;
  20.  
This is the main part, the rest is not important.

The problem is that when I enter any character, instead of a sole enter, the program 'skips' fgets() function and goes to if conditional right away.

I use Windows XP with Cygwin. GCC version is 3.4.4.

Thanks for any tips.
Hi. A couple of things. Even though you are convinced that the rest of the code is not important, in this case at least it is vital.
There are two parameters in your fgets function that are not explained by the code posted
FNAME_BUFFER_SIZE and stdin
Given that these 2 values are crucial to the problem could you please amend your code to show us their values? The closing curly brace for the while loop is also missing from your posted code.
Dec 28 '06 #2

P: 3
The bracket is missing, as it was cut out. FNAME_BUFFER_SIZE is 256.
Dec 29 '06 #3

100+
P: 1,646
The bracket is missing, as it was cut out. FNAME_BUFFER_SIZE is 256.
and stdin?
Dec 29 '06 #4

100+
P: 1,646
The bracket is missing, as it was cut out. FNAME_BUFFER_SIZE is 256.
This runs under vc++
Expand|Select|Wrap|Line Numbers
  1. char welcome_string[50];
  2.    char file_name_str[256];
  3.    FILE *file_ptr = NULL;
  4.    char temp_char = 0;
  5.    int FNAME_BUFFER_SIZE = 256
  6.  
  7.    sprintf(welcome_string, "Enter the name of a file:");
  8.    while(1) {
  9.         printf(welcome_string);
  10.         printf("\n");
  11.         fgets(file_name_str, FNAME_BUFFER_SIZE, stdin);
  12.         file_ptr = fopen(file_name_str, "r");
  13.  
  14.         if (file_ptr == NULL) {
  15.             printf("\n");
  16.             printf("Bad file try again");
  17.             printf("\n");
  18.  
  19.             if ((temp_char = getc(stdin)) == 'q') {
  20.                 exit(1);
  21.             } else {
  22.                 system("CLS");
  23.                 continue;
  24.             }
  25.         }
  26.    }
  27.    return file_ptr;
  28.  
Dec 29 '06 #5

P: 3
I will try it, and write how it worked. Thanks.
Dec 30 '06 #6

P: 8
Hi ,

I have modified the program a bit and it worked well, Modified lines are tagged with /<******>/.

Its a problem with fgets , which appends one extra character at the end.
I have tested it on MS v studio.


Thanks,




char welcome_string[50];
char file_name_str[256],file_name_actual[256];
FILE *file_ptr = NULL;
char temp_char = 0;
int FNAME_BUFFER_SIZE = 256;



FILE * f1()
{
sprintf(welcome_string, "Enter the name of a file:");
while(1) {
printf(welcome_string);
printf("\n");
fgets(file_name_str, FNAME_BUFFER_SIZE, stdin);
strncpy(file_name_actual,file_name_str,(strlen(fil e_name_str)-1)); /<*******>/
file_ptr = fopen(file_name_actual, "r");

if (file_ptr == NULL) {
printf("\n");
printf("Bad file try again");
printf("\n");
printf("want to quit? press q\n"); /<********************************>/
if ((temp_char = getc(stdin)) == 'q') {
exit(1);
} else {
system("CLS");
continue;
}
}
else
printf("found the file\n"); /<*************************>/
}
return file_ptr;
}

void main ()
{FILE *fp= f1();
}
Jan 2 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.