Thread: File and auto startup problem

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    Question File and auto startup problem

    Hi,

    im writting a code for count the reboots and shutdowns in windows7.
    In the first run the program works fine, write the file and donīt show error msg. After a reboot or shutdown the program auto startup fine but donīt write the file.

    the code:
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <windows.h> 
    //------------------------------------------------------------------------------ 
    int s(void); 
    int c(void); 
    //------------------------------------------------------------------------------ 
    int main(int argc, char *argv[]){ 
      
     if(s()== 0) return 0; 
     if(c()== 0) return 0; 
      
     while(1){Sleep(100);} //  view return function 
      
     return 1; 
    } 
    //------------------------------------------------------------------------------ 
    int s(void) 
    { 
       HKEY hkey; 
      
       const char PATH[] = 
       "C:\\Users\\User1\\Desktop\\reboot.exe"; // desk for now
      
        
       int regResult=RegOpenKeyEx(HKEY_CURRENT_USER/*HKEY_LOCAL_MACHINE*/, 
       "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_SET_VALUE,&hkey); 
       if(regResult != 0) /*return 0;*/ printf("REG OPEN ERROR\n"); 
      
       regResult=RegSetValueEx(hkey,"count",0,REG_SZ,(BYTE *)PATH, 
       strlen(PATH)); 
       if(regResult != 0) /*return 0;*/printf("REG SET ERROR\n"); 
        
       regResult=RegCloseKey(hkey); 
       if(regResult != 0) /*return 0;*/printf("REG CLOSE ERROR\n"); 
       return 1; 
    } 
    //------------------------------------------------------------------------------ 
    int c(){ 
          FILE *counter; 
          
          counter=fopen("filecounter.txt","a"); 
          if(counter==NULL) /*return 0;*/ printf("FILE OPEN ERROR\n"); 
          
          int check=fprintf(counter,"hello\n"); 
          if (check==0) /*return 0;*/ printf("FPRINTF ERROR\n"); 
          
          //int check = fputs("hello\n",counter);            // donīt work
          //if (check!=0) return 0; // printf("FPUTS ERROR\n"); 
          
          if(fclose(counter)!= 0) /*return 0;*/ printf("FCLOSE ERROR\n"); 
          
          return 1; 
    } 
    //------------------------------------------------------------------------------
    Thanks

    PS: The counting funcion is not write yet. Im just trying write the file.

    PS2: Compiling with Dev-c++ 4.9.9.2, Windows7 Ultimate

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It could be a permissions error or a path error. Check errno after fopen() fails and that will tell you why it's failing. You'll need to #include <errno.h>.

    Also, I'd highly recommend you not get in the habit of using one-character function names. Symbol names should convey their purpose.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> fopen("filecounter.txt","a");
    The location of this file depends on the current working directory. I recommend using a full path name.

    >> Compiling with Dev-c++ 4.9.9.2
    That's fairly old. This should be an easy transition into something newer - wxDev-C++

    gg

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    Hi guys,

    the problem was the full path in c() function.

    Code:
    //------------------------------------------------------------------------------  int c(){        FILE *counter;               counter=fopen("C:\\Users\\User1\\Desktop\\filecounter.txt","a");        if(counter==NULL) /*return 0;*/ printf("FILE OPEN ERROR\n");               int check=fprintf(counter,"hello\n");        if (check==0) /*return 0;*/ printf("FPRINTF ERROR\n");               if(fclose(counter)!= 0) /*return 0;*/ printf("FCLOSE ERROR\n");               return 1;  }  //------------------------------------------------------------------------------
    Now write the file on startup.

    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need to auto-format a text file to HTML
    By Jeff Mitchell in forum Tech Board
    Replies: 3
    Last Post: 04-09-2011, 10:51 PM
  2. startup script to launch .swf file
    By cDev in forum C Programming
    Replies: 5
    Last Post: 07-26-2006, 01:29 PM
  3. Weird problem on '02 3.4L V6 auto
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-12-2006, 12:05 AM
  4. Startup using STARTUP folder method for winXP & win98
    By hanhao in forum Windows Programming
    Replies: 2
    Last Post: 05-26-2005, 04:59 AM
  5. c++ file at startup
    By wazilian in forum Windows Programming
    Replies: 1
    Last Post: 02-26-2003, 06:54 PM

Tags for this Thread