Thread: the simplest problem of all (about fgets)

  1. #1
    Registered User white's Avatar
    Join Date
    Nov 2004
    Posts
    39

    the simplest problem of all (about fgets)

    Well I am ashamed to say this ...but in all the time I am writing programs in C I always used data etc...directly from files. So I basically don't know how to put user input...I've checked some previous posts and tutorials but I can't really see what's wrong...the program works fine without the red part

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int linesoffilex(FILE *estream)
    {
    	fseek(estream, 0, SEEK_SET);
    	
    	char mb[8];
    	char* forpop = "%7s\n";
    	
    	int j=0;
    	
    	while (fscanf (estream, forpop, mb) == 1)
    	{
    		j++;
    	}
    	return j;
    }
    int main ()
    {
    	time_t timer;
    	struct tm *tm_now;
    	char filtime[BUFSIZ];
    	char name[2];
    	
    	timer = time(NULL);
    	tm_now = localtime (&timer);
    	
    	printf("press 1. to open a previous month or\n ANY key for current month\n");
    	fgets(name, 1, stdin);
    	
    	if (name == "1")
    	{
    		printf("enter full file name\n");
    		fgets(filtime, 12, stdin);
    	}
    	else
    	{
    		
    		strftime (filtime , sizeof filtime, "%b %Y.txt", tm_now);
    	}
    	
    	FILE *astream;
    	astream = fopen(filtime, "a");
    	fclose(astream);
    	
    	FILE *bstream;
    	bstream = fopen(filtime, "r");
    	int j, g, k;
    	j = linesoffilex(bstream);
    	
    	double *kb;
    	kb = malloc(sizeof(double)*j);
    	
    	fseek(bstream, 0, SEEK_SET);
    	char * del = "%lf\n";
    	
    	for (g = 0; g < j; g++)
    	{
    		fscanf(bstream, del, &kb[g]);
    	}
    	
    	double sum;
    	
    	for (k = 0; k < j; k++)
    	{
    		sum = kb[k]+sum;
    	}
    	fclose(bstream);
    	
    	FILE *cstream;
    	cstream = fopen("statistics.txt", "w");
    	fprintf(cstream, "Total kb so far: %lf\n",sum);
    	fprintf(cstream, "Total kb remaining: %lf\n",3000-sum);
    	fprintf(cstream, "Next day estimated usage: %lf\n",sum/j);
    	fprintf(cstream, "Monthly estimated usage: %lf\n",(sum/j+1)*31);
    	fprintf(cstream, "Daily recommented usage: %lf\n",(3000-sum)/(31-j));
    	fclose(cstream);
    	
    	return 0;
    }
    What it does is that is exiting before the user inputs anything
    ----------------

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    fgets is to input strings from a file stream.
    You passed '1' to it as if you are looking for a single character? There are better functions for that.
    Or do this
    Code:
    char name[10]; //make it bigger than 2, how can you fit a name in 2 characters?
    fgets(name,sizeof(name),stdin);
    Remember fgets does not throw out the new line character (it says in the string).
    But if you looking to input a single character, look into fgetc/getc or getchar.

  3. #3
    Registered User white's Avatar
    Join Date
    Nov 2004
    Posts
    39
    Code:
    ....
    char name;
    	char option[2];
    	
    	timer = time(NULL);
    	tm_now = localtime (&timer);
    	
    	printf("press 1. to open a previous month or\n ANY key for current month\n");
    	name = getchar();
    	int char_count = 0;
    	while( (name != '\n')  &&  (char_count < 2))
    	{
    		option[char_count] = name;
    	}
    ....
    well i've tried this as well but it still doesn't work.. i mean it doesn't go into the if..else statement
    Last edited by white; 11-02-2005 at 06:58 AM.
    ----------------

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Code:
    	char ch;
    
    	printf("press 1. to open a previous month or\n ANY key for current month\n");
    	ch = getchar();
    	
    	if ( ch == '1' )
    	{
    		// do something
    	}

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by xeddiex
    Code:
                    int ch;
    
    	printf("press 1. to open a previous month or\n ANY key for current month\n");
    	ch = getchar();
    	
    	if ( ch == 1 )
    	{
    		// do something
    	}
    getchar returns an int not a char

    ssharish2005

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The int is so EOF can be detected. Once you've determined the EOF has not been reached, you can assign it to a char.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with fgets....
    By Huskar in forum C Programming
    Replies: 5
    Last Post: 03-29-2009, 10:13 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  4. print problem while using fgets()
    By learninC in forum C Programming
    Replies: 12
    Last Post: 05-15-2005, 09:29 PM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM