-1

I need to rename the file, but I do not understand what is wrong, the file is encrypted, but the name does not change, how to use the rename () function correctly. I need to change the file name to "encrypt.yes"

#include <stdio.h>                                                                                  
#include <stdlib.h>  
#include <string.h>


int main(void)                                                                                       
{   

char new[20];                                                                                                
char old[20];
int rename(const char *old, const char *new);                                                                                 
int ch;                                                                                         
FILE *fps;                                                                            
printf("Enter file name (with extension like file.txt) to encrypt : ");
strcpy(new,"encrypt.yes");
rename(old, new);                                
scanf("%s", old);

fps = fopen(old, "r+");                                           
if (fps == NULL) {                                                                              
    printf("Could not open file '%s'\n", old);                                                
    return 1;                                                                                   
}                                                          
while ((ch = fgetc(fps)) != EOF) {                                                              
    ch += 100;                                                                                  
    fseek(fps, -1, SEEK_CUR);                                                                   
    fputc(ch, fps);                                                                             
    fseek(fps, 0, SEEK_CUR);

}                                                 

fclose(fps);                                                                                    

printf("File '%s' encrypted successfully\n", old);                                            
return 0;                                                                                       

}

Jessica
  • 3
  • 4
  • Just `unlink(fname); rename("temp.txt",fname);`. You must also open `temp.txt` in mode `"wb"` as the encryption can result in non-printable characters. – Paul Ogilvie Jul 31 '19 at 11:48
  • 2
    `ch` must be `int` as `EOF` is -1. – Paul Ogilvie Jul 31 '19 at 11:52
  • 2
    See: [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) Also, consider spacing your code a bit more for readability, e.g. `if (fps == NULL)` is much easier to read -- especially for older eyes... Consider removing the old DOS `conio.h` and removing the non-portable `getch()` and replacing with `getchar()` since you are only using it to pause operation and hold the console window open. – David C. Rankin Jul 31 '19 at 13:15
  • 1
    @PaulOgilvie: ... as `EOF` is negative (usually -1). – pmg Jul 31 '19 at 13:15
  • Is it possible to somehow supplement this code so that when it starts in a folder or directory, it encrypts all the files in it? It may seem strange, but I'm not going to harm anyone, it just interests me for educational purposes. I recently started learning C language – Jessica Jul 31 '19 at 16:32
  • Well, sure it is. See [here](https://stackoverflow.com/questions/1271064/how-do-i-loop-through-all-files-in-a-folder-using-c) or [here](https://stackoverflow.com/questions/1271064/how-do-i-loop-through-all-files-in-a-folder-using-c) (C++, but method 3 and 4 can be used with minor changes from C, too). – Aconcagua Jul 31 '19 at 17:04

1 Answers1

1

Don't know if I understand you completely, but I think you're trying to modify each byte as-is. Adapting this answer to your code, you could do something like below. NOTE: There is still much room for improvement and error handling.

Also, I know you tagged this as Windows, but my Windows VM is screwing up at the moment, so I wrote and tested this code on Linux with gcc. It may work on Windows as-is, but I did not test it. Nonetheless, I think it's straight-forward enough to get you 95% there. The key is opening the file in "r+" mode so you can read from and write to it.

And lastly, don't gloss over the comments you've gotten so far from Paul Ogilvie and David C. Rankin; that's good advice.

#include <stdio.h>                                                                                  
#include <stdlib.h>                                                                                 

int main()                                                                                          
{                                                                                                   
    char fname[20];                                                                                 
    int ch;                                                                                         
    FILE *fps;                                                                                      

    printf("Enter file name (with extension like file.txt) to encrypt : ");                         
    scanf("%s", fname);                                                                             

    fps = fopen(fname, "r+");                                                                       
    if (fps == NULL) {                                                                              
        printf("Could not open file '%s'\n", fname);                                                
        return 1;                                                                                   
    }                                                                                               

    while ((ch = fgetc(fps)) != EOF) {                                                              
        ch += 100;                                                                                  
        fseek(fps, -1, SEEK_CUR);                                                                   
        fputc(ch, fps);                                                                             
        fseek(fps, 0, SEEK_CUR);                                                                    
    }                                                                                               

    fclose(fps);                                                                                    

    printf("File '%s' encrypted successfully\n", fname);                                            
    return 0;                                                                                       
}  
riffic
  • 131
  • 6
  • 1
    This does not convert/encrypt `0x0D 0x0A`, cr/lf (on Windows). For that the file must be opened in binary mode. Probably seek -1 on cr/lf behaves unexpected. – Paul Ogilvie Jul 31 '19 at 14:10
  • Thank you all so much for the help, you really helped me. – Jessica Jul 31 '19 at 14:44
  • Is it possible to somehow supplement this code so that when it starts in a folder or directory, it encrypts all the files in it? – Jessica Jul 31 '19 at 14:50
  • It may seem strange, but I'm not going to harm anyone, it just interests me for educational purposes. I recently started learning C language – Jessica Jul 31 '19 at 14:51
  • @Jessica you probably need use Win API. See the [„How to list files in a directory using the Windows API?“](https://stackoverflow.com/questions/41404711/how-to-list-files-in-a-directory-using-the-windows-api) question. – eanmos Aug 01 '19 at 11:42