Thread: Problem with a loop

  1. #1
    Registered User
    Join Date
    Oct 2008
    Location
    Illinois
    Posts
    7

    Question Problem with a loop

    When I put the following code in a loop it will not write to the file. When I take it out of the loop it works fine (I realize that for now it is an infinite loop as that is part of the assignment). Any suggestions?

    Code:
            while(!done) {
                printf("Enter line #%d: ",line_count++);
                if (fgets(user_line,MAX_LINE_LENGTH,stdin) == NULL) {
                    fprintf(stderr,"Error reading user input: %s\n", strerror(errno));
                }
                if (fputs(user_line,memo_pntr) == EOF) {
                    fprintf(stderr,"Error writing to file %s: %s\n",memo_file_path, strerror(errno));
                }
                
            }
    Thanks!

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Did you check stderr for errors?
    Do you close the file (fclose()) after this loop? Maybe because the file is terminated violently (since it has infinite loop) the buffer isn't written to the file? Just a wild guess...

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Are you sure that you're entering the loop? To enter that loop the value of done must be zero. Post a short example that both compiles and demonstrates the problem - without seeing how you've set up the variables you're using it's hard to see what you're doing wrong.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    Illinois
    Posts
    7
    Yeah, I'm entering the loop and it closes afterwards (although with the current design it will never hit the close). I want to force the user to use cntrl-c to end for now.

    Here is the whole code:

    Code:
    /*
     *CS306 Lab 5 lab5.c
     *Author: Chris Wiegman
     *Execute the memo program as specified in lab 5.
     *
     *Call as:
     * memo [MEMO_FILE]
    */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    
    #define WAIT_TIME 10
    #define MAX_LINE_LENGTH 100
    
    void sigalrm_handler(int signal);
    void sigint_handler(int signal);
    void sigquit_handler(int signal);
    void error_cleanup();
    
    char *memo_file_path = NULL;
    FILE *memo_pntr;
    
    int main(int argc, char *argv[]) {
    
    	int line_count = 1, done = 0;
            char user_line[MAX_LINE_LENGTH+1];
    
    	switch (argc) {
    		case 1:
    			memo_file_path = "memo.text";
    			break;
    		case 2:
    			memo_file_path = argv[1];
    			break;
    		default:
    			fprintf(stderr,"Usage: memo [MEMO_FILE]\n");
    			return EXIT_FAILURE;
    			break;
    	}
    
            if ((memo_pntr = fopen(memo_file_path, "w+")) == NULL) {
    		fprintf(stderr,"Error opening file %s: %s\n",memo_file_path, strerror(errno));
    		return EXIT_FAILURE;
    	}
    
            while(!done) {
                printf("Enter line #%d: ",line_count++);
                if (fgets(user_line,MAX_LINE_LENGTH,stdin) == NULL) {
                    fprintf(stderr,"Error reading user input: %s\n", strerror(errno));
                }
                if (fputs(user_line,memo_pntr) == EOF) {
                    fprintf(stderr,"Error writing to file %s: %s\n",memo_file_path, strerror(errno));
                }
                
            }
    
            fclose(memo_pntr);
    
    	return EXIT_SUCCESS;
    
    }
    
    void sigalrm_handler(int signal) {
    
    }
    
    void sigint_handler(int signal) {
    
    }
    
    void sigquit_handler(int signal) {
    
    }
    
    void error_cleanup() {
    
    }

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Add fflush(memo_pntr) after you use fputs, or replace fputs with an fprintf. fputs must not flush the output buffer on completion.

    [edit]

    This explains the behaviour - and I'll guess that without the loop you were closing the file afterwards.
    http://crasseux.com/books/ctutorial/fputs.html

    [/edit]
    Last edited by Richie T; 11-30-2008 at 12:45 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    Illinois
    Posts
    7
    That was it. THANK YOU!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM