0

Let's say I have a file called "____.srt" , where ___ could be anything. I'm trying to change its name, by adding '1' , so it'll look like this "____1.srt".

The file is read from an argument using CMD, argv[1].

void main(int argc,char* argv[]){   
char* pt; 
pt = strstr(argv[1],".srt\0"); // checks if end of input-string is .srt
    if( pt == NULL)
    {
        fprintf(stdout,"Invalid input.\nInput file must be inputfile.srt\n");
        fprintf(stdout,"Program will now exit\n");
        return; 
    }
    strcpy(pt,'1'); 
    strcat(argv[1],".srt");

    fprintf(stdout,"%s file was created.\n",argv[1]);  }

This doesn't seem to work. Can someone figure out the problem? I'd appreciate any help. Thank you

Jadenkun
  • 317
  • 2
  • 16
  • I don't know too much `c` but, shouldn't you check the value of `argc` before `strstr(argv[1],".srt\0")`. – dcg Apr 13 '17 at 12:03
  • You don't state how "it doesn't seem to work". You are printing out the new filename, but not actually renaming the file (or even checking if it exists in the first place). Also, there is no point in having a `\0` in a literal string. – Klitos Kyriacou Apr 13 '17 at 12:12
  • @Klitos I am checking if the file exists . I didn't mention it becuase its not the point. How else will I check if a string ends with ".srt" if I dont put \0? – Jadenkun Apr 13 '17 at 12:53
  • 1
    See http://stackoverflow.com/questions/10347689/how-can-i-check-whether-a-string-ends-with-csv-in-c. There is no way in the C library to directly match the end of a string. The "\0" in your string literal is taken to be the end of the string ".srt". The function strstr can't possibly distinguish between ".str\0" and ".srt". – Klitos Kyriacou Apr 13 '17 at 13:20
  • It's also good policy to catch any error code(s) from the rename operation, such as the function return code, and errno. These codes will at least tell you why the call is failing. If you want to see the error text from a system() call, use popen() instead. – MikeW Dec 14 '18 at 09:26

1 Answers1

0

After a couple of modifications, your code looks like this and I hope it works. In your code you didn't handle the strings properly.

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

int main(int argc,char* argv[]){
    char* pt;
    int flag;
    pt = strstr(argv[1],".srt\0"); // checks if end of input-string is .srt
    if( pt == NULL)
    {
        fprintf(stdout,"Invalid input.\nInput file must be inputfile.srt\n");
        fprintf(stdout,"Program will now exit\n");
        return 0;
    }

    pt = (char*)malloc(sizeof(argv[1] + 1)); // filename length + number or additional digits
                                             // Needed for using strncpy function
    strncpy(pt, argv[1], strlen(argv[1]) - 4); // copying filename except the .srt extention.
    strcat(pt, "1.srt"); //using pt for new filename
    flag = rename(argv[1], pt);

    if (flag == 0) // rename returns 0 on success
        fprintf(stdout,"New file name: %s\n", pt);
    else
        fprintf(stdout,"An error occurred\n");
}