Thread: File Handling in c programming........

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    3

    File Handling in c programming........

    Hi,
    I just came across this program on the net and modified it a bit, but am not getting the same output.Could you'll please help me.

    P.S: This may be a silly one, but please bear with me as im a new programmer.Thank you.


    program to create a sequential file and to store data in a tabular form:


    [COLOR="Blue"]
    Code:
    #include <stdio.h>
    #include <string.h>
    #define FILENAME "seqtxtfi.txt"
    int main(void)
    {
      int n,i;
      char fld1[25];
      char fld2[20];
      char fld3[15];
      char fld4[15];
      FILE *fp;
    
      printf("Enter Ctrl-D to end input.\n");
      if ((fp = fopen(FILENAME, "w")) == NULL)
        printf("File could not be opened\n");
       
      else
      printf("Enter the no. of flds\n");
      scanf("%d",&n);
      for(i=0;i<n;i++)
      { start:
         printf("enter no.");
         scanf("%d",&n);
         printf("Enter Field 1   : ");
         fgets(fld1, sizeof(fld1), stdin);
         if (feof(stdin)) return 0;   
         fld1[strlen(fld1)-1] ='\t';
    
         printf("Enter Field 2   : ");
         fgets(fld2, sizeof(fld2), stdin);
         if (feof(stdin)) return 0;          
         fld2[strlen(fld2)-1] ='\t';
    
         printf("Enter Field 3   : ");
         fgets(fld3, sizeof(fld3), stdin);
         if (feof(stdin)) return 0;           
         fld3[strlen(fld3)-1] ='\t';
    
         printf("Enter Field 4   : ");
         fgets(fld4, sizeof(fld4), stdin);
         if (feof(stdin)) return 0;           
         fld4[strlen(fld4)-1] ='\0';
            
        {
           fprintf(fp, "%s%s%s%s\n", fld1, fld2, fld3, fld4);
           goto start;
           break;
    
         }
         fclose(fp);
      }
      return 0;
    }
    I expect the output to be like:
    fld1 fld2 fld3 fld4
    1 2 3 4
    3 4 5 6


    Unfortunately, i get Enter fld1 : Enter fld2:
    i.e the first fld1 does not get accepted. Later on its ok. Plz help me.Thanks in advance.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I think its beacuase after the scanf() (where you get 'n') theres still a '\n' character in the input buffer. This then gets passed to the first fgets(). If you put a getchar in between them it should sort it out.

    For more info on flushing the input buffer:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    3

    Question Explain a bit more in detail please....

    Quote Originally Posted by mike_g View Post
    I think its beacuase after the scanf() (where you get 'n') theres still a '\n' character in the input buffer. This then gets passed to the first fgets(). If you put a getchar in between them it should sort it out.

    For more info on flushing the input buffer:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    COULD U PLEASE EXPLAIN IT A BIT FURTHER?????WHERE EXACTLY IS IT WRONG??????

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    When you are entering data at this line:
    Code:
    scanf("%d",&n);
    you enter a number, but it is also followed by a newline character from when you hit the return key.

    The newline character sits in an area of memory called the input buffer. Then when you call fgets here:
    Code:
    fgets(fld1, sizeof(fld1), stdin);
    It sees that there is already a newline in the input buffer, so it takes it as if you just entered nothing but hit return.

    getchar() takes 1 character input from the input buffer, so if you call this between the scanf() and fgets() statements then it will remove the newline that was causing problems from the input buffer.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    there should be some kind of punishment for using all caps and talking like a 10 year old.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    3

    Thumbs up Thanks,but......

    Hi,
    Its me again....Thanks mike_g. It partially solved my problem. But when i say n=2, it displays
    Enter fld1:74678
    Enter fld2:78745
    Enter fld3:38487
    Again,
    ENTER fld1:48934
    /*then after enter, we have to press enter again*/
    Enter fld2:498482
    Enter fld3:897984
    and exits....
    but is there a way to prevent pressing enter twice??? Thanks again.


    __________________________________________________
    A "string" to simpleid:: making the same mistake twice never killed anybody and i guess YOU have to blame the makers of my Keyboard for a CAPSLOCK key. Well,i guess if you like to edit your posts,you should think of deleting them,too.THANKS FOR YOUR ADVICE.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should not mix scanf() with fgets(), they don't play well together.

    Use fgets() and sscanf() to read your first value of n.

    Also, your use of 'goto' is redundant, because that's where the for loop was going anyway.

    Post your latest code if you're still stuck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM