C语言中的随机错误
我是C编程的新手。我写了一个程序,对系列文件进行重命名。有时它工作,有时-在相同的条件下-它崩溃没有明显的原因。它给出的代码错误是c000005,我搜索后发现是内存问题。 我注意到,如果我从IDE(代码::Blocks 13.12)运行程序,它总是可以工作的!但是,当我从调试目录或发布目录启动应用程序时,有时它会崩溃! 我怎样才能解决这个问题 该节目旨在为电视剧的字幕重新命名。字幕将按顺序命名:如果第二季是第二季,第二季的第一集将是2x01-epiodetitle.srt,第二集将是2x02-epiodetitle.srt,依此类推。 很多时候,字幕中都会写上该集的名称,因此程序会在.srt文件中搜索该集。 如果它找到“Episod”字符串,则会搜索两个倒逗号(“)之间的字符串。例如,如果在字幕文件中有这样一行: 第01幕“第一章” 程序复制标题第1章并将其插入文件名中,因此将其重命名为1x01-Chapter 1.srt 如果程序没有找到位于两个倒逗号之间的标题,它将在下面的行中搜索。因此,如果文件中有两行类似的内容,程序仍将工作: 第1幕: “第一章” 如果字幕中没有该集的名称,则该节目将该集命名为“1x01.srt”(如果它是第一季的第一集)C语言中的随机错误,c,crash,C,Crash,我是C编程的新手。我写了一个程序,对系列文件进行重命名。有时它工作,有时-在相同的条件下-它崩溃没有明显的原因。它给出的代码错误是c000005,我搜索后发现是内存问题。 我注意到,如果我从IDE(代码::Blocks 13.12)运行程序,它总是可以工作的!但是,当我从调试目录或发布目录启动应用程序时,有时它会崩溃! 我怎样才能解决这个问题 该节目旨在为电视剧的字幕重新命名。字幕将按顺序命名:如果第二季是第二季,第二季的第一集将是2x01-epiodetitle.srt,第二集将是2x02-e
#包括
#包括
#包括
#包括
#包括
#包括
void f_rename(char stg);
char*FindName(char*file);
char-stg;
int main()
{
printf(“chestagione?”);/*询问季节编号*/
scanf(“%c”和&stg);
f_重命名(stg);
}
无效f_重命名(字符stg)
{
DIR*d;
结构导向;
char*newname=(char*)malloc(sizeof(char)*9);
newname[0]=stg;/*将第一个字符设置为季节编号*/
新名称[1]='x';
d=opendir(“.”);
int i=0;int k=0;int count1=0;int count2=0;
如果(d)
{
而((ent=readdir(d))!=NULL)/*ent结构包含文件名(d_name)*/
{
如果(strcmp(ent->d_name,“.”)=0&&strcmp(ent->d_name,“…”)!=0)/*删除readdir()的.和..条目*/
{
if(strstr(ent->d_name,“.srt”)!=NULL)/*只搜索.srt*/
{
i++;/*计算有多少个.srt文件*/
char*result=FindName(ent->d_name);
如果(i==10){count2++,i=0;}/*如果有10个副标题,它将i设置为零,以获得1x10*/
新名称[2]=(字符)(count2+(int)“0”);
newname[3]=(char)(i+(int)“0”);/*将字幕的数量设置为一个字符*/
if(result!=NULL)/*检查字幕中是否有剧集名称*/
{
int len=strlen(结果);
newname=(char*)realloc(newname,sizeof(char)*(len+11));/*只提供newname所需的空间,因此7char(“1x01-”)加上4(.srt)加上剧集名称的长度)*/
新名称[4]='';新名称[5]='-';新名称[6]='';
对于(int j=7;jd_名称,“.srt”);
重命名(ent->d_name,newname);
}
else/*如果字幕中没有该集的名称,请给出名称1x01.srt*/
{
newname=(char*)realloc(newname,sizeof(char)*9);
对于(int j=4;jd_名称,“.srt”);
重命名(ent->d_name,newname);
}
}
}
}
closedir(d);
}
}
char*FindName(char*file)
{
字符字符串[]=“Episod”;字符*结果;
文件*stream=fopen(文件,“r”);
char-buf[300];
字符*p1;字符*p2;整数计数=0;整数计数2=0;
while(fgets(buf,300,stream)!=NULL)
{
/*如果找到字符串“Episod”,则搜索倒逗号,如果没有,则在下一行中搜索,如果没有,则函数返回0*/
if((strstrstr(buf,string))!=NULL | | count2==1)/*搜索字符串的Episod*/
{
计数++;
if(strstr(buf,“\”)==NULL)
{
如果(计数=2)中断;
else{count2++;continue;}
}
p1=strstr(buf,“\”)+1;/*取名称开头的位置*/
p2=strstr(p1,“\”);
尺寸长度=p2-p1;
结果=(char*)malloc(sizeof(char)*(len+1));
strncpy(result,p1,len);/*将名称复制到result中*/
结果[len]='\0';
fclose(流);
返回(结果);
}
}
fclose(流);
返回(0);/*如果未找到名称,则返回0*/
}
我尝试调试,但它在TpWaitForAlpcCompletion()中报告了一个问题,我不知道这是什么意思。是否有人可以帮助我使其不崩溃?其中一个问题是我认为您未充分分配“newname”所需的空间……您没有考虑空终止符 您的代码(经过一点重新格式化)显示: 应该是:
int len = strlen(result);
newname=(char*)realloc(newname,sizeof(char)*(len+12));
/*
* gives newname only the space it needs, so 7char ("1x01 - ")
* plus 4 (.srt) plus the length of the name of the episode
* plus 1 for the null-terminator)
*/
你的代码非常复杂,很难理解,这是你的问题的一部分。所有这些
count
和count2
都不能解释你想做什么。如果你用一种更简单的方式编写程序,你会发现它们更容易调试
您还缺少应该进行的各种检查,包括fopen()
,rename()
,malloc()
,realloc()
,等等的返回值
您的字符串处理非常费力且不必要。您的注释使代码更难理解,而不是更容易理解。有些步骤、强制转换、sizeof(char)
s和内存分配完全不需要,使代码更难理解
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)
*/
int len = strlen(result);
newname=(char*)realloc(newname,sizeof(char)*(len+12));
/*
* gives newname only the space it needs, so 7char ("1x01 - ")
* plus 4 (.srt) plus the length of the name of the episode
* plus 1 for the null-terminator)
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <limits.h>
#include <sys/stat.h>
#include <dirent.h>
#define MAX_LINE 1024
void rename_files(const char season);
char * find_episode_name(const char * filename);
int main(void)
{
printf("Enter the season number: ");
fflush(stdout);
char buffer[MAX_LINE];
if ( !fgets(buffer, MAX_LINE, stdin) ) {
fprintf(stderr, "error calling fgets()");
exit(EXIT_FAILURE);
}
else if ( !buffer[0] ) {
fprintf(stderr, "You entered no input.\n");
exit(EXIT_FAILURE);
}
rename_files(buffer[0]);
return EXIT_SUCCESS;
}
void rename_files(const char season)
{
DIR * d;
if ( (d = opendir(".")) ) {
int num_srt = 0;
struct dirent * ent;
while ( (ent = readdir(d)) ) {
if ( strcmp(ent->d_name, ".") &&
strcmp(ent->d_name, "..") ) {
if ( strstr(ent->d_name, ".srt" ) ) {
++num_srt;
char newname[MAX_LINE];
char * result = find_episode_name(ent->d_name);
if ( result ) {
snprintf(newname, MAX_LINE, "%cx%02d - %s.srt",
season, num_srt, result);
} else {
snprintf(newname, MAX_LINE, "%cx%02d.srt",
season, num_srt);
}
if ( rename(ent->d_name, newname) == -1 ) {
char buffer[MAX_LINE];
snprintf(buffer, MAX_LINE,
"error renaming file '%s'", ent->d_name);
perror(buffer);
exit(EXIT_FAILURE);
}
free(result);
}
}
}
if ( closedir(d) == -1 ) {
perror("error closing directory");
exit(EXIT_FAILURE);
}
}
}
char * find_episode_name(const char * filename)
{
const char * epi = "Episod";
char * result = NULL;
char buffer[MAX_LINE];
FILE * infile = fopen(filename, "r");
if ( !infile ) {
snprintf(buffer, MAX_LINE, "error opening file '%s'", filename);
perror(buffer);
exit(EXIT_FAILURE);
}
bool keep_looking = true;
bool next_line = false;
while ( keep_looking && fgets(buffer, MAX_LINE, infile) ) {
if ( strstr(buffer, epi) || next_line ) {
const char * p1 = strchr(buffer, '"');
if ( !p1 ) {
if ( !next_line ) {
next_line = true;
}
else {
keep_looking = false;
}
}
else {
++p1;
const char * p2 = strchr(p1, '"');
if ( !p2 ) {
fprintf(stderr, "Badly formed input in file %s\n",
filename);
exit(EXIT_FAILURE);
}
const size_t len = p2 - p1;
if ( !(result = malloc(len + 1)) ) {
perror("couldn't allocate memory for result");
exit(EXIT_FAILURE);
}
strncpy(result, p1, len);
result[len] = '\0';
keep_looking = false;
}
}
}
if ( fclose(infile) == EOF ) {
snprintf(buffer, MAX_LINE, "error closing file '%s'", filename);
perror(buffer);
exit(EXIT_FAILURE);
}
return result;
}
paul@thoth:~/src/sandbox/soq$ ls
1x01 - Episode 1.srt 1x04 - Random Name.srt soq
1x02 - Episode 2.srt 1x05 - Another Random Name.srt soq.c
1x03 - Episode 3.srt data
paul@thoth:~/src/sandbox/soq$ cat '1x01 - Episode 1.srt'
paul@thoth:~/src/sandbox/soq$ cat '1x02 - Episode 2.srt'
Episodio 2
"Free ride on a donkey"
paul@thoth:~/src/sandbox/soq$ cat '1x03 - Episode 3.srt'
Episodio 3
"Milton Keynes is a long way"
paul@thoth:~/src/sandbox/soq$ cat '1x04 - Random Name.srt'
paul@thoth:~/src/sandbox/soq$ cat '1x05 - Another Random Name.srt'
Episodio 66 - "Fruitless in Milan"
paul@thoth:~/src/sandbox/soq$ ./soq
Enter the season number: 1
paul@thoth:~/src/sandbox/soq$ ls
1x01 - Fruitless in Milan.srt 1x04 - Free ride on a donkey.srt soq
1x02.srt 1x05 - Milton Keynes is a long way.srt soq.c
1x03.srt data
paul@thoth:~/src/sandbox/soq$