473,473 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

fgets() problem

2 New Member
Hi!

I'm trying to write a program for my school project and I encountered a problem I can't resolve, nor understand.

I want to make a program that create a file and add records to this file until the user decide to stop.
Code:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream.h>
  4.  
  5. using namespace std;
  6.  
  7. typedef struct {
  8.   char street[24];
  9.   char city[20];
  10. } Dest;
  11.  
  12. void add(char *filename)
  13. {
  14.   FILE *fp;
  15.   Dest reg;
  16.  
  17.   fp=fopen(filename, "a"); 
  18.  
  19.   if(fp==NULL) perror("Error opening file");
  20.   else
  21.   {
  22.     fseek(fp, 01, 2);
  23.     fflush(stdin);
  24.  
  25.     printf("Street: ");
  26.     fgets(reg.street, 24, stdin);
  27.     printf("City: ");
  28.     fgets(reg.city, 20, stdin);
  29.  
  30.     fwrite(&reg, sizeof(Dest), 1, fp);
  31.     fclose(fp);
  32.   }
  33. };
  34.  
  35. int main()
  36. {
  37.   char test='d';
  38.   char filename[25]="///root/test";
  39.  
  40.   while((test=='D')||(test=='d'))
  41.   {
  42.         system("clear");       
  43.         add(filename);
  44.  
  45.         printf("\n To continue press D/d");
  46.         cin >> test;
  47.   };
  48.  
  49.   return 0;
  50. }
The problem is: after a loop of while, if i continue to add record, the program don't let me enter record for "Street", jumps to next "City". The message "Street" appear on screen, but i can't enter data.

I use fedora and gnu c++.

Thanks for any tips.

P.S. Works on windows (DevC++) just fine, but in linux...
Jan 23 '07 #1
3 2008
Banfa
9,065 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1.     fflush(stdin);
  2.  
You can not fflush stdin, it invokes undefined behaviour and you may get different program behaviour on different systems.

I suggest you start by removing this call.
Jan 23 '07 #2
horace1
1,510 Recognized Expert Top Contributor
in addtion to using fflush() on stdin (as indicated by banfa) you are mixing C and C++ I/O, i.e. using fgets() on stdin and cin >>, which can cause problems.

Your problem is probably being caused by
Expand|Select|Wrap|Line Numbers
  1. cin >> test;
  2.  
where you will enter D (or some other character) followed by the newline key - the character is returned in variable test and the newline will be left in the input stream - the next fgets() will then return immediatly with an empty string (apart from newline character). Try replacing
Expand|Select|Wrap|Line Numbers
  1. cin >> test;
  2.  
with
Expand|Select|Wrap|Line Numbers
  1.         test=getchar();  // read character
  2.         getchar();       // read newline key
  3.  
note that fgets() leaves the newline character \n on the end of the input string - you may not want this.
Jan 23 '07 #3
schwerzerspaner
2 New Member
Hi and thanks for replies.
I follow your advice, i modified the code: erase "fflush" and replace "cin" and now it's working.

Thanks a lot for your help and time.
Jan 23 '07 #4

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

Similar topics

5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
4
by: Karthick S. | last post by:
Hi, fgets returns NULL on error and the first argument on success. I want to open a file and read it using fgets. This is returning NULL, when the file is empty. Is there anyway to circumvent...
7
by: Emerson | last post by:
Hi all: my problem is in the comments of the code . would u please give me some clue what 's the problem? what u in advance. /*******************************/ #include "stdio.h" #include...
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
11
by: santosh | last post by:
Hi, A book that I'm currently using notes that the fgets() function does not return until Return is pressed or an EOF or other error is encountered. It then at most (in the absence of...
7
by: RSoIsCaIrLiIoA | last post by:
until a poor newbie can build a better function than sscanf and fgets scanf("%s", string) is like gets(string)
42
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters...
9
by: uidzer0 | last post by:
Hey everyone, Taken the following code; is there a "proper" or dynamic way to allocate the length of line? #include <stdio.h> #include <errno.h> int main(int argc, char **argv) { FILE *fp;
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.