LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 08-04-2012, 05:29 PM   #1
ad_alta
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Rep: Reputation: Disabled
printing with fgets function


Can someone tell me what is wrong here. I want to loop the array and print each student data on a single line, but instead I get each member data on a separate line.


Enter first name for student 1: James
Enter last name for student 1: Wilson
Enter age of student: 25
Enter first name for student 2: Jamie
Enter last name for student 2: Wilson
Enter age of student: 22
You entered:
James
Wilson
25
Jamie
Wilson
22

Code:
#include <stdio.h>

int main()
{
 struct person {
        char firstName[11];
        char lastName[16];
        int age;
 };

 int count=2,i,ch;


 

 struct person student[count];

 for( i=0; i < count; i++)
   {
     printf("Enter first name for student %d: ", i + 1);
     fgets(student[i].firstName,10,stdin);
     printf("Enter last name for student %d: ", i +1);
     fgets(student[i].lastName,15,stdin);
     printf("Enter age of student: ");
     fscanf(stdin," %d", &student[i].age);
     ch=getchar();
   }

 printf("You entered: \n");

 for( i=0; i < count; i++)
 {
     printf("%s %s %d\n", student[i].firstName, student[i].lastName,student[i].age);
}
return 0;

}

Last edited by ad_alta; 08-04-2012 at 05:53 PM.
 
Old 08-05-2012, 12:15 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
After fgets you might want to remove the tailing LineFeed (and perhaps check for problems)

Code:
    buff= '\0';
    fgets (buff, max, stdin);
    len= strlen (buff);
    if (len==0) /* error/EOF on input? */
    else if (buff[len-1]=='\n')
        buff [len--]= '\0';
    else if (len==max-1) /* line truncated */
 
1 members found this post helpful.
Old 08-06-2012, 02:36 AM   #3
ad_alta
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you for the response NevemTeve. Why do you check for the '\n' char at buff[len-1] when strlen returns the number of characters in the string? I'm only assuming here, but shouldn't it be buff[len+1] for '\0' and buff[len+2] for '\n'.

Last edited by ad_alta; 08-06-2012 at 03:26 AM.
 
Old 08-08-2012, 04:17 AM   #4
segmentation_fault
Member
 
Registered: Sep 2008
Location: Ioannina, Greece
Distribution: Gentoo
Posts: 332

Rep: Reputation: 55
You shouldn't mix using fgets and fscanf/scanf, because they handle whitespace characters differently, so some newline characters (\n) are get with fgets.

As for strlen, yes, it returns the length of the string without \0. So, on a string (lets name it buf) containing "James" and a newline character you have:
Code:
 J a m e s \n \0
 0 1 2 3 4  5  6
strlen(buf) will return 6, but you want to get rid of buf[5] which is buf[strlen(buf) - 1]
 
1 members found this post helpful.
  


Reply



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
Slack 13.37 printing to Canon MF4270 partial function tomashag Linux - Hardware 8 02-14-2012 08:50 PM
[SOLVED] fgets() function in fedora10 not taking any i/p in aloop manascetatbbsr Programming 1 10-22-2010 06:25 PM
kill thread which is with block function... (fgets...) okeyla Programming 2 10-14-2009 07:32 PM
C++: obtaining and printing a function signature R00ts Programming 4 01-07-2008 11:54 AM
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 11:52 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