C 指针和字符串处理错误

C 指针和字符串处理错误,c,string,pointers,C,String,Pointers,我已经盯着这段代码看了很长一段时间了,我不知道这段代码出了什么问题以及如何修复它。我认为其中一个数组正在写入已分配的内容 调试器允许我编译,但当我编译时,我得到: Unhandled exception at 0x774615de in HW6_StringProcessing.exe: 0xC0000005: Access violation. 代码如下: #include <stdio.h> #include <stdlib.h> #include <stri

我已经盯着这段代码看了很长一段时间了,我不知道这段代码出了什么问题以及如何修复它。我认为其中一个数组正在写入已分配的内容

调试器允许我编译,但当我编译时,我得到:

Unhandled exception at 0x774615de in HW6_StringProcessing.exe: 0xC0000005: Access violation.
代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_SIZE 100

void convertToPigLatin (char * strPtr, char * pStrPtr);

int main(int argc, char *argv[])
{
   char str[MAX_STR_SIZE];
   char pStr[MAX_STR_SIZE];
   FILE *fileInPtr;                             //Create file name
   FILE *fileOutPtr;    

   fileInPtr = fopen("pigLatinIn.txt", "r");    //Assign text to file
   fileOutPtr = fopen("pigLatinOut.txt", "w");

   if(fileInPtr == NULL)                        //Check if file exists
   {
      printf("Failed");
      exit(-1); 
   }
   fprintf(fileOutPtr, "English Word\t\t\t\tPig Latin Word\n");
   fprintf(fileOutPtr, "---------------\t\t\t\t----------------\n");

   while(!feof(fileInPtr))                  //Cycles until end of text
   {

      fscanf(fileInPtr, "%99s", str);       //Assigns word to *char

      str[99] = '\0';                       //Optional: Whole line

      convertToPigLatin(str, pStr); 

      //fprintf(fileOutPtr, "%15s\t\t\t\t%15p\n", str, pStr); 

      printf("%s\n", str); 
   }  

   system("pause"); 
}

void convertToPigLatin (const char * strPtr, char * pStrPtr)
{
   int VowelDetect = 0; 
   int LoopCounter = 0; 
   int consonantCounter = 0; 
   char cStr[MAX_STR_SIZE] = {'\0'};
   char dStr[] = {'-','\0'}; 
   char ayStr[] = {'a','y','\0'};
   char wayStr[] = {'w','a','y','\0'};

   while (*strPtr != '\0')
   {
      if (*strPtr == 'a' || *strPtr == 'e' || 
          *strPtr == 'i' || *strPtr == 'o' || 
          *strPtr == 'u' || VowelDetect ==1)
      {
         strncat(pStrPtr, strPtr, 1); 
         VowelDetect = 1; 
      }
      else
      {
         strncat(cStr, strPtr, 1); 
         consonantCounter++; 
      }
      *strPtr++;
   }
   strcat(pStrPtr, dStr); 
   if (consonantCounter == 0)  
   {
      strcat(pStrPtr, wayStr);
   }
   else
   {
      strcat(cStr,ayStr);
      strcat(pStrPtr, cStr);
   }  
   printf("%s\n", pStrPtr);                         

}
#包括
#包括
#包括
#定义最大长度为100
void convertToPigLatin(char*strPtr,char*pStrPtr);
int main(int argc,char*argv[])
{
字符str[最大字符大小];
字符pStr[最大字符大小];
FILE*fileInPtr;//创建文件名
文件*fileOutPtr;
fileInPtr=fopen(“piglatin.txt”,“r”);//为文件分配文本
fileOutPtr=fopen(“pigLatinOut.txt”,“w”);
if(fileInPtr==NULL)//检查文件是否存在
{
printf(“失败”);
出口(-1);
}
fprintf(fileOutPtr,“英语单词\t\t\t\tPig拉丁单词\n”);
fprintf(fileOutPtr,“-----------\t\t\t-----------\n”);
while(!feof(fileInPtr))//循环直到文本结束
{
fscanf(fileInPtr,“%99s”,str);//将单词分配给*char
str[99]='\0';//可选:整行
转换拉丁语(str,pStr);
//fprintf(fileOutPtr,“%15s\t\t\t\t%15p\n”,str,pStr);
printf(“%s\n”,str);
}  
系统(“暂停”);
}
void converttopigratin(常量字符*strPtr,字符*pStrPtr)
{
int-VowelDetect=0;
int循环计数器=0;
int辅音计数器=0;
char cStr[MAX_STR_SIZE]={'\0'};
字符dStr[]={'-','\0'};
char-ayStr[]={'a','y','\0'};
char wayStr[]={'w','a','y','\0'};
而(*strPtr!='\0')
{
如果(*strprtr=='a'| |*strprtr=='e'| |
*strprtr=='i'| |*strprtr=='o'| |
*strprtr='u'| |元音检测==1)
{
strncat(pStrPtr,strPtr,1);
元音检测=1;
}
其他的
{
strncat(cStr,strPtr,1);
辅音计数器++;
}
*strprpr++;
}
strcat(pStrPtr、dStr);
if(辅音计数器==0)
{
strcat(pStrPtr、wayStr);
}
其他的
{
strcat(cStr、ayStr);
strcat(pStrPtr、cStr);
}  
printf(“%s\n”,pStrPtr);
}
原型是:

void convertToPigLatin (char * strPtr, char * pStrPtr);
但此函数需要
const char*

void convertToPigLatin (const char * strPtr, char * pStrPtr)
----------------------------------------^这里

此外,您还必须在
converttopigratin

*strPtr++;
应该是:

strPtr++;

我认为问题可能是对未初始化的缓冲区使用了
strcat()
strncat()
。在调用
converttopigratin()
之前,请确保使用
memset()
或类似的工具来blat
pStr[]

while(!feof(fileInPtr)) {
    ...
    str[99] = '\0';
    pStr[0] = '\0';    // or memset(pStr, 0, sizeof(pStr));
    convertToPigLatin(str, pStr);
    ...
}
消除歧视:

void convertToPigLatin (char * strPtr, char * pStrPtr);
与功能的定义不一致:

void convertToPigLatin (const char * strPtr, char * pStrPtr)
{
   ...                       
}

这是一个带注释的工作版本,其中包含一些更改和建议。您可能想抛出最后的
printf
,但我想确保在第一次运行时有可见的输出。这里的意图是 尽量贴近问题的实际代码,不要完全重写

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

#define XSTR(n) #n // a hack to allow stringify to use the *value of a macro
#define STR(n) XSTR(n)
#define MAX_STR_SIZE 100

void convertToPigLatin (const char * strPtr, char * pStrPtr);

int main(int argc, char *argv[])
{
    // The str and pStr aren't very meaningful, I'd suggent eng and pig.
    // Adding -"Str" to all your strings just slows down reading.
    // - plus it makes "str" an anomaly.
    char str[MAX_STR_SIZE + 1];   // +1 since NUL is usually not counted..
    char pStr[MAX_STR_SIZE + 1];  // ...by functions like strlen().  
    FILE *fileInPtr  = fopen("pigLatinIn.txt", "r");    //Assign text to file
    FILE *fileOutPtr = fopen("pigLatinOut.txt", "w");
    const char *fmt = "%15s     %s\n"; // for head and table output.
    if( ! (fileInPtr && fileOutPtr)) {
        perror("unable to open file(s) for I/O")
        return -1;
    }
    fprintf(fileOutPtr, fmt, "English Word", "Pig Latin Word");
    fprintf(fileOutPtr, fmt, "------------", "---------------");

    // put test before fscanf so we don't lose the last input line on EOF
    while(!feof(fileInPtr))           //Cycles until end of text
    {
        if(1 == fscanf(fileInPtr, "%" STR(MAX_STR_SIZE) "s", str))
        {
            str[MAX_STR_SIZE] = '\0';      //Optional: Whole line
            convertToPigLatin(str, pStr); 
            fprintf(fileOutPtr, fmt, str, pStr);
        }
        // else fprintf(stderr, "skipping empty line\n");  // for example.
    }  
    // system("pause") would be... icky, sleep(...) or 
    // getchar() are better, but still odd since output is going
    // to a file
    fclose(fileInPtr);
    fclose(fileOutPtr);
    return 0;           // a zero exit status from main() means success :-)
}

void convertToPigLatin (const char * strPtr, char * pStrPtr)
{
    int VowelDetected = 0;
    int LoopCounter = 0; 
    int ConsonantCounter = 0; 
    char cStr[MAX_STR_SIZE + 1] = {'\0'};
    char dStr[]   = "-";    // same as {'-','\0'}; 
    char ayStr[]  = "ay";   // same as {'a','y','\0'};
    char wayStr[] = "way";  // same as {'w','a','y','\0'};

    pStrPtr[0] = '\0';      // clear out pStr so trash doesn't prefix output.

    // a better loop control would be:  for( /*prior*/ ; *strPtr ; ++strPtr)
    //  - which lets you move the strPtr++ to the top line of the for loop.
    while (*strPtr != '\0')    // could simplify to:  while(*strPtr)
    {
#if 0   // disabling this to show a different option in the #else clause
        if (   *strPtr == 'a'
            || *strPtr == 'e'
            || *strPtr == 'i'
            || *strPtr == 'o'
            || *strPtr == 'u'
            || VowelDetected == 1)
        {
#else
        if (strchr("aeiou", *strPtr) || VowelDetected == 1)
        {
#endif
            strncat(pStrPtr, strPtr, 1); 
            VowelDetected = 1; 
        }
        else
        {
            strncat(cStr, strPtr, 1); 
            ConsonantCounter++; 
        }
        ++strPtr;   // a *strPtr++ would increment the target char itself.
    }
    strcat(pStrPtr, dStr);
    if (ConsonantCounter == 0)     // or: if( ! Consonantcounter)
    {
        strcat(pStrPtr, wayStr);
    }
    else
    {
        strcat(cStr,ayStr);
        strcat(pStrPtr, cStr);
    }  

    printf("# %s\n", pStrPtr);
}
#包括
#包括
#包括
#定义XSTR(n)#n//一种允许stringify使用宏的*值的方法
#定义STR(n)XSTR(n)
#定义最大长度为100
void converttopigratin(const char*strPtr,char*pStrPtr);
int main(int argc,char*argv[])
{
//str和pStr不是很有意义,我建议使用eng和pig。
//将“Str”添加到所有字符串只会降低阅读速度。
//-此外,它使“str”成为一种异常现象。
字符str[MAX_str_SIZE+1];//+1,因为通常不计算NUL。。
char pStr[MAX_STR_SIZE+1];/…由strlen()等函数执行。
FILE*fileInPtr=fopen(“piglatin.txt”,“r”);//为文件分配文本
FILE*fileOutPtr=fopen(“piglatiout.txt”、“w”);
const char*fmt=“%15s%s\n”;//用于头和表输出。
if(!(fileInPtr&&fileOutPtr)){
perror(“无法打开I/O文件”)
返回-1;
}
fprintf(fileOutPtr,fmt,“英语单词”,“猪拉丁单词”);
fprintf(fileOutPtr,fmt,“-----------”,“-----------”;
//将测试放在fscanf之前,这样我们就不会丢失EOF上的最后一个输入行
while(!feof(fileInPtr))//循环直到文本结束
{
如果(1==fscanf(fileInPtr,“%”STR(MAX_STR_SIZE)“s”,STR))
{
str[MAX_str_SIZE]='\0';//可选:整行
转换拉丁语(str,pStr);
fprintf(fileOutPtr、fmt、str、pStr);
}
//else fprintf(stderr,“跳过空行”;//例如。
}  
//系统(“暂停”)会…恶心,睡眠(…)或
//getchar()更好,但仍然很奇怪,因为输出正在进行
//归档
fclose(fileInPtr);
fclose(fileOutPtr);
返回0;//main()的零退出状态表示成功:-)
}
void converttopigratin(常量字符*strPtr,字符*pStrPtr)
{
int-VowelDetected=0;
int循环计数器=0;
int辅音计数器=0;
char cStr[MAX_STR_SIZE+1]={'\0'};
char dStr[]=“-”;//与{'-','\0'}相同;
char ayStr[]=“ay”;//与{'a','y','\0'相同;
char wayStr[]=“way”;//与{'w'、'a'、'y'、'\0'相同;
pstrprtr[0]='\0';//清除pStr,这样垃圾就不会作为输出的前缀。
//更好的循环控制是:for(/*previor*/;*strprtr;++strprtr)
//-这使您可以将strPtr++移动到for循环的顶行。
while(*strprtr!='\0')//可以简化为:while(*strprtr)
{
#如果0//禁用此选项以在#else子句中显示其他选项
如果(*strPtr=='a'
||*strprtr=='e'
||*strprtr==“我”
||*strprtr=='o'
||*strprtr=='u'
||元音检测==1)
{
#否则
if(strhr(“aeiou”,*strprtr)| |元音检测==1)
{
#恩迪夫
strncat(pStrPtr,strPtr,1);