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 03-11-2004, 04:09 AM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Rep: Reputation: 30
fgets() problems in C


Hey all

I'm trying to use fgets in my apps now instead of scanf (so much easier to write to strings when your using spaces in them) but when I use them in pwdDiary (my app i'm working on) it just skips past them to the next function....

If i do something like this in pwdDiary:

Code:
char name[64];
char nameAgain[64];

printf("Enter your name: ");
fgets(name, sizeof(name), stdin);
name[strlen(name)-1] = '\0';

printf("Enter it again: ");
fgets(nameAgain, sizeof(nameAgain), stdin);
nameAgain[strlen(nameAgain)-1] = '\0';
This would skip to the second fgets but if i create a new program and use this it works fine....

Anyone run into this before?
 
Old 03-11-2004, 05:47 AM   #2
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
That seems strange, nothing wrong with your code here.

If could however look like its skipping the first fgets if the line is longer then the specified read buffer?
like 65spaces+newline ? .. First reads 63 spaces (plus adds a \0), next reads 2 spaces and the newline (plus adds a \0) ??

Maybe your files contains alot of trailing spaces? I nothis that sometimes I get them when Im coding.. Mostly when copy and pasting code over an over again while doing debugging.

Hope you solve it, and let us know =)
 
Old 03-11-2004, 10:06 AM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
fgets() as you use it is fine, but you need to know that (as worldmagic says) there can be junk like spaces or newlines in the stdin buffer.

If you use fgetc() on stdin you can get around those problems. It reads every character out there, until you tell it to stop. Hack ther code below into something that filters out the crud for you, if that's the problem.


Code:
void get_stdin(char *result, int maxlen){
  int ch=0;
  int chars_we_read=0;
  memset(result,0x00,(size_t)maxlen);
  while(ch=fgetc(stdin)!=EOF && chars_we_read < maxlen){
           *(result+ chars_we_read)=(char)ch;
           chars_we_read++;
           if(ch=='\n') break;
  }
}
 
Old 03-11-2004, 05:23 PM   #4
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
K I just thrw a getchar() in before the fgets and it worked perfectly - apparently fgets() treats srdin differently to functions like scanf()...

Thanks for the help guys
 
Old 03-12-2004, 04:39 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
try using
ddd

to have a look at the problem.

billy
 
  


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
php fgets really slow pasholy2001 Programming 0 04-22-2005 02:17 AM
reading in string with fgets bostonbanana Programming 4 12-12-2004 02:10 PM
Is fgets functions secure? mullog Programming 2 10-02-2004 01:22 PM
fgets h/w Programming 23 12-21-2003 06:20 PM
fgets vs gets cxel91a Programming 2 12-01-2003 12:36 PM

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

All times are GMT -5. The time now is 04:31 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