Thread: writing a string to file skips 1st character

  1. #1
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62

    writing a string to file skips 1st character

    Hi,
    I have been playing with the write to file functions lately which are very useful and mostly straight forward. However I have come across one problem I can't get my head around. The problem is that when I read a few lines characters and hit return for each line.... every second line is not read - starting with the first one.

    I entered the following and hit return after each "line"
    1
    2
    3
    4
    5
    6
    7
    8



    New Proverbs written to file.
    About to read proverbs from file.
    The proverbs in the file are:

    Many a mickle makes a muckle.
    Too many cooks spoil the broth.
    He who laughs last didn't get the joke in the first place.
    2 <----- These are the new entries appended to the file.
    4
    6
    8

    Where is 1,3,5,7? I believe this is down to the Carriage return and the buffer but I can't explain it. I double checked that my getch() calls were not causing this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    const int LENGTH = 80;                     /* maximum input length */
    
    int main(void)
    {
      int i=0;
      char *proverbs[] =
           { "Many a mickle makes a muckle.\n",
             "Too many cooks spoil the broth.\n",
             "He who laughs last didn't get the joke in"
                                    " the first place.\n"
           };
      char more[LENGTH];                        /* store a new proverb */
      FILE *pfile = NULL;
      char *filename = "c:\\myfile.txt";
      
      /* create a new file, if myfile does not already exist */
      if(!(pfile=fopen(filename,"w")))
      {
        printf("\nError opening &#37;s for writing. Program terminated.",filename);
        exit(1);
      }
      
      /* write out first three proverbs to the file */
      printf("\nNew file created. About to write out first three proverbs to the file.");
      /* getch(); */
    
      int feedback = sizeof proverbs;
      printf("\nsizeof proverbs:%d",feedback);
      feedback = sizeof proverbs;
      printf("\nsizeof proverbs[0]:%d",feedback);
    
      int count = sizeof proverbs / sizeof proverbs[0];   /* result = 16/16 = 1 */
      for(i=0; i<count; i++)
        fputs(proverbs[i], pfile);
      fclose(pfile);
    
      printf("\nProverbs written to file.");
      /* getch(); */
      
      /* open the file to append more proverbs */
      if(!(pfile=fopen(filename,"a")))
      {
        printf("\nError opening %s for writing. Program terminated.",filename);
        exit(1);
      }
      
      printf("\nEnter proverbs of less than 80 characters to append to file or press Enter to end:\n");
      while(fgets(more, LENGTH, stdin))
      {
        fgets(more, LENGTH, stdin);            /* read a proverb         */
        if(more[0]=='\n')                      /* if its empty line      */
          break;                               /* end input operation    */
        fputs(more, pfile);                    /* write the new proverb  */
      }
      fclose(pfile);
    
      printf("\nNew Proverbs written to file.");
      /* getch(); */
      
      if(!(pfile=fopen(filename,"r")))
      {
        printf("\nError opening %s for reading. Program terminated.",filename);
        exit(1);
      }
    
      printf("\nAbout to read proverbs from file.");
      /* getch(); */
      
      /* read and output the file contents */
      printf("\nThe proverbs in the file are:\n\n");
      while(fgets(more, LENGTH, pfile))             /* read a proverb */
        printf("%s", more);                          /* display it     */
      fclose(pfile);
      remove(filename);
      
      printf("\nPress any key to exit.");
      getch();
      return(0);
    }
    I think I have come across this ghost before, and I'm sure its the buffer - maybe I need to use flush?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    while(fgets(more, LENGTH, stdin))          /* first */
      {
        fgets(more, LENGTH, stdin);            /* second */ /* read a proverb         */
        if(more[0]=='\n')                      /* if its empty line      */
          break;                               /* end input operation    */
        fputs(more, pfile);                    /* write the new proverb  */
      }
    This loop reads two lines from stdin and writes only the second one to the file.
    Kurt

  3. #3
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Quote Originally Posted by ZuK View Post
    Code:
    while(fgets(more, LENGTH, stdin))          /* first */
      {
        fgets(more, LENGTH, stdin);            /* second */ /* read a proverb         */
        if(more[0]=='\n')                      /* if its empty line      */
          break;                               /* end input operation    */
        fputs(more, pfile);                    /* write the new proverb  */
      }
    This loop reads two lines from stdin and writes only the second one to the file.
    Kurt
    Thanks Zuk, that was killing me - I did it right for the while fgets loop further down but didn't spot the difference. Thanks for setting me straight!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM