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: - #include <stdio.h>
-
#include <stdlib.h>
-
#include <iostream.h>
-
-
using namespace std;
-
-
typedef struct {
-
char street[24];
-
char city[20];
-
} Dest;
-
-
void add(char *filename)
-
{
-
FILE *fp;
-
Dest reg;
-
-
fp=fopen(filename, "a");
-
-
if(fp==NULL) perror("Error opening file");
-
else
-
{
-
fseek(fp, 01, 2);
-
fflush(stdin);
-
-
printf("Street: ");
-
fgets(reg.street, 24, stdin);
-
printf("City: ");
-
fgets(reg.city, 20, stdin);
-
-
fwrite(®, sizeof(Dest), 1, fp);
-
fclose(fp);
-
}
-
};
-
-
int main()
-
{
-
char test='d';
-
char filename[25]="///root/test";
-
-
while((test=='D')||(test=='d'))
-
{
-
system("clear");
-
add(filename);
-
-
printf("\n To continue press D/d");
-
cin >> test;
-
};
-
-
return 0;
-
}
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...
3 2008 Banfa 9,065
Recognized Expert Moderator Expert
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.
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
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
with -
test=getchar(); // read character
-
getchar(); // read newline key
-
note that fgets() leaves the newline character \n on the end of the input string - you may not want this.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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,...
|
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...
|
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...
|
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...
|
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...
| |
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)
|
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...
|
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;
|
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...
|
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/...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |