I'm a newbie in C programming. I wrote a program which renames files in series. Sometimes it works, sometimes -in same conditions - it crashes without no apparent reason. The code error it gives is c000005, i searched and discovered that it is a problem with the memory. I noticed that if i run the program from the IDE (Code::Blocks 13.12) it always works! But when i launch the app from the debug directory or from the release one, sometimes it crashes! How could i solve this problem?
The program is meant to rename subtitles of TV episodes. The subtitles will be named in order: if the season is number two, the first episode of the second season will be 2x01 - EpisodeTitle.srt, the second 2x02 - Episode Title.srt, and so on. Many times in the subtitles there is written the name of the episode, so the program will search for it in the .srt file. If it finds the "Episod" string, than it searches for the string between two inverted commas ("). So for example, if in the subtitle file there is this kind of line:
Episodio 01 "Chapter 1"
The program copy the title Chapter 1 and insert it in the name of the file, so it will be renamed 1x01 - Chapter 1.srt
If the program doesn't find the title, situated between two inverted commas, it will search for it in the line below. So if in the file there are two lines like those, the program will still work:
Episodio 01:
"Chapter 1"
If there isn't the name of the episode inside the subtitles, the program will name the episode "1x01.srt" (if it is the first episode of the first season")
#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
void f_rename(char stg);
char *FindName(char* file);
char stg;
int main()
{
printf("Che stagione?"); /* Asks for the number of the season*/
scanf("%c",&stg);
f_rename(stg);
}
void f_rename(char stg)
{
DIR *d;
struct dirent *ent;
char *newname=(char*)malloc(sizeof(char)*9);
newname[0]=stg; /* sets the first character to the number of the season */
newname[1]='x';
d= opendir(".");
int i=0;int k=0; int count1=0;int count2=0;
if (d)
{
while ((ent = readdir(d)) != NULL) /* ent struct contains the name of the file (d_name) */
{
if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) /*delete . and .. entry of readdir()*/
{
if (strstr(ent->d_name, ".srt") != NULL) /* searches only for .srt*/
{
i++; /*count how many .srt files there are*/
char *result = FindName(ent->d_name);
if (i==10) {count2++, i=0;} /* if there are 10 subtitles, it sets i to zero, in order to have 1x10*/
newname[2] = (char)(count2 + (int)'0');
newname[3] = (char)(i + (int)'0'); /*sets the number of the subtitles as a character*/
if (result != NULL) /* Check if there is the name of the episode inside subtitles*/
{
int len = strlen(result);
newname=(char*)realloc(newname,sizeof(char)*(len+11)); /*gives newname only the space it needs, so 7char ("1x01 - ") plus 4 (.srt) plus the lenght of the name of the episode)*/
newname[4]=' ';newname[5]='-';newname[6]=' ';
for (int j=7;j<len+4;j++) newname[j]='\0';
strcat(newname, result);
strcat(newname,strstr(ent->d_name, ".srt"));
rename(ent->d_name,newname);
}
else /* if there isn't the name of the episode inside the subtitles, give the name 1x01.srt*/
{
newname=(char*)realloc(newname,sizeof(char)*9);
for(int j=4;j<8;j++) newname[j]='\0';
strcat(newname,strstr(ent->d_name, ".srt"));
rename(ent->d_name,newname);
}
}
}
}
closedir(d);
}
}
char *FindName(char *file)
{
char string[]="Episod";char *result;
FILE *stream = fopen(file,"r");
char buf[300];
char *p1; char *p2; int count=0;int count2=0;
while(fgets(buf, 300, stream) != NULL)
{
/* if it found the string 'Episod', then searches for the inverted comma, if there isn't, it searches for it in the next line, if the there isn't, the function returns 0. */
if ((strstr(buf,string)) != NULL || count2==1) /* searches the string 'Episod */
{
count++;
if (strstr(buf,"\"")==NULL)
{
if (count==2) break;
else {count2++;continue;}
}
p1= strstr(buf,"\"")+1; /* take the position of the begin of the name*/
p2= strstr(p1,"\"");
size_t len = p2-p1;
result=(char*)malloc(sizeof(char)*(len+1));
strncpy(result,p1,len); /* copy the name into result*/
result[len]='\0';
fclose(stream);
return(result);
}
}
fclose(stream);
return(0); /*if it didn't find the name, return 0 */
}
I tried to debug and it reports a problem in TpWaitForAlpcCompletion(), i don't know what does it mean. Is there anyone who could help me in order not to make it crash?