Thread: improve program.

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    improve program.

    Hey can some of you tell me some way to make my program better? some tips or something, thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
    	int opencount = 0;
       char ligne[1000];
       char * buff;
       long num;
    
       FILE *dataIn = NULL;
    
       dataIn = fopen("c:\\time.txt", "r");
        if ( dataIn == NULL )
    	  {
            perror("something went wrong\n" );
            return EXIT_FAILURE;
    	  }
    
    	while(fgets(ligne, sizeof(ligne), dataIn) !=NULL)
    	  opencount++;
    
    
       printf("You rebooted your comp %d times.\n\n", opencount);
    
    
       fseek (dataIn , 0 , SEEK_END);
       num = ftell (dataIn);
       rewind (dataIn);
    
       buff = (char*) malloc (num);
         if (buff == NULL)
           exit (EXIT_FAILURE);
    
       fread (buff,1,num,dataIn);
       printf("%s", buff);
    	fclose(dataIn);
    	free (buff);
    
    
       return EXIT_SUCCESS;
    }
    thanks all

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Aside from having to launch this from autoexec or some other method to make it accurate, why would you need all of those file operations when you could just read in one int value, print your output message, update the count, and then rewrite that new value to the file... that appears what you're trying to do; perhaps I'm really missing something?

    You might want to move the file from the root directory, change its attributes, or make a backup copy somewhere else, so that it wouldn't be so easy to get deleted.

    If your goal is to count reboots, then use the fact that the first run could be NULL to create a new file to start the process instead of printing an error message. Since it's text, you could just create the first occurance from a text editor.

    if((dataIn = fopen("c:\\time.txt", "r")) == NULL)

    can save a few chars of typing.

    const char filename[] = "somepath";
    fopen(filename, mode);

    Is nice for not having to hunt down lines deeper in your program.

    just my opinion.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    15
    ronin, what im trying to do is read from the file that this prog creates

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    
    	FILE * pFile;
       time_t now;
       time(&now);
    
    	pFile = fopen ("c:\\time.txt","at");
       fputs (ctime(&now), pFile);
       fclose(pFile);
    
       return EXIT_SUCCESS;
    }
    i put this in the start up folder and everytime the computer boots up it writes to the file saying what time and date it started. Then with the other code i posted i read from that file and it tells me how many time i rebooted the computer and at what time and date. anyway thanks for all the info ronin and vVv. I'll do the modifications.

  4. #4
    LOL. I think someone is cheating. What, you didn't know I had an account here also?

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    15
    lol, i already posted my code i just wanna know some way to improve it, no harm in doing that? hehe. And yes i did know you were a member here, i read ur post hehe
    Last edited by emperor; 01-05-2003 at 02:10 AM.

  6. #6
    I have seen your name a few times. But I guess I can allow that. Since I want to know what they say to make it better

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM