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

Home Posts Topics Members FAQ

calling the function directly works but …

1 New Member
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. typedef struct filename
  3. {
  4.         int age;
  5.         char name[100];
  6. }name_t;
  7.  
  8. name_t * fname=NULL;
  9.  
  10. void quit()
  11. {
  12.       printf("\nPress enter to exit");
  13.       fflush(stdin);
  14.       getchar(); 
  15. }
  16.  
  17. enter(char prompt[])
  18. {
  19.       puts(prompt);
  20.       fflush(stdin);
  21.       getchar(); 
  22. }      
  23.  
  24. void read_txt_file()
  25. {   
  26.     char ch;   
  27.     fname=(name_t *)malloc(sizeof(name_t));
  28.     FILE *fptr=NULL;
  29.     atexit(quit);
  30.     printf("Please enter the file name to read : ");
  31.     fflush(stdin);
  32.     scanf("%s",fname->name);
  33.     fptr=fopen(fname->name,"r");
  34.     if(fptr == NULL)
  35.     {
  36.             perror("Could not open the file ");
  37.             return;
  38.     }
  39.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++");
  40.     printf("Contents of the file %s are : ",fname->name);
  41.  while(ch != EOF)
  42.     {
  43.             ch=fgetc(fptr);
  44.             printf("%c",ch);
  45.     }
  46.     enter("press enter");
  47.     fclose(fptr);       
  48. }
  49.  
  50. display_menu()
  51. {
  52.   int choice;
  53.   while(1)
  54.   {
  55.         system("cls");
  56.         printf("\t\t1.read and display from a file\n \
  57.                 \b2.quit\n");
  58.                 scanf("%d",&choice);
  59.         switch(choice)
  60.         { 
  61.                       case 1 :
  62.                            read_txt_file();
  63.                            break;
  64.                       case 2 :
  65.                            exit(0);
  66.                       default :
  67.                               printf("please enter proper choice(1-3)\n Enter to continue");
  68.                               fflush(stdin);
  69.                               getchar();
  70.         }
  71.    }
  72.  
  73. }
  74.  
  75.  
  76.  
  77. int main()
  78. {
  79.    /*
  80.    read_txt_file();
  81.    */
  82.    display_menu();
  83.    return 0;
  84. }
  85.  
when I use the display_menu function to call read_txt_file()(works only if I add a getchar() after it) ...I am unable to get the files contents to the stdout without using getchar()...why is it needed???
but when I use the read_txt_file() directly(works) I can see the contents of the file to my stdout
what is the problem with the display_menu
please help
Apr 2 '11 #1
2 1197
horace1
1,510 Recognized Expert Top Contributor
you type the number and hit the <ENTER> key to terminate the line.
The number is read but the <ENTER> character is left in the input stream. If you read a string next it reads the <ENTER> character and thinks it has an empty string. You need the getchar() to remove the <ENTER>
Apr 3 '11 #2
donbock
2,426 Recognized Expert Top Contributor
Modify your scanf format string so that it "eats" the newline for you.
Apr 4 '11 #3

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

Similar topics

3
by: Daniel Hansen | last post by:
I'm sure I saw this somewhere but can't remember where and can't find it now... Is there a PHP function or global variable that will return name of the calling function? I want to do this for...
1
by: Andrew Wilkinson | last post by:
Hi, First off I know that in almost all cases this would be a terrible thing to do, but this is an unusual case where this makes sense. Basically I have a procedure where you pass a string...
6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
6
by: jchao123 | last post by:
Dear All, I have an MDB file (Access 2000/XP) which contains generic routines I use in various apps (eg, API calls, File access classes etc). I have compiled into an MDE file which I reference...
2
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
16
by: Saroj | last post by:
Is there an way for a function to know who called him ?
4
by: Peter Afonin | last post by:
Hello, I have a weirdest issue I've ever had. I have a function that enters some data into the Oracle table and returns the sequential row number for the new record (autonumber): Private...
2
by: sumanthsclsdc | last post by:
Hello friends, I have a problem, I implemented a class which uses tkinter and displays the window as required, the class will create a window with listbox and inserts some items into it, I...
4
by: astri | last post by:
#include "Unit1.h" #include "math.h" #include "fixed_math.hpp" #include "algorithm.h" #define MBIT 0x4000 #define CBIT 16 long constbl; void __fastcall TForm1::Button1Click(TObject...
15
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
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?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.