2013-07-25 81 views
0

我已经在这个代码盯着很长一段时间,我无法找出什么不对的代码,以及如何解决它。我认为其中一个数组正在写入已分配的数据。指针和字符串处理错误

调试器是让我去编译,但是当我做,我得到:

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);       

} 
+0

和......有什么问题,你遇到? – akira

+1

你在哪里初始化pStrPtr?你使用'strncat' ...之前的价值是什么?函数开始时的值是多少? – rabensky

+0

user2525288,当你编译这个你得到了什么警告? (你有所有的警告开启,对吧?)嗯 –

回答

0

原型是:

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

但该功能预计const char *

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

----------------------------------------^Here

而且,你必须增加的strPtr指针本身(而不是值)convertToPigLatin

*strPtr++; 

应该是:

strPtr++; 
+0

。我做了修复,但我仍然得到相同的错误... – user2525288

1

我认为这个问题可能是使用strcat()strncat()到没有初始化的缓冲区SED。调用convertToPigLatin()之前,请确保您BLATpStr[]memset()或一些这样的:

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

嗯。为什么只用[0]设置pStr?我不应该给它更大的内存分配? – user2525288

+0

这不是一个分配。这是一个初始化。您将第一个字节设置为空,因此字符串处理函数将其视为零长度字符串。 –

+0

明白了。我在初始化中用类似的方式处理它:char pStr [MAX_STR_SIZE] = {'\ 0'} ;.我想我已经缩小问题到一个事实,即当convertToPigLatin重申了几次,pStrPtr已经从原来的位置前进。这意味着该阵列很快就会填满,然后开始写作超过其分配。我怎样才能确保每个时间PSTR和pStrPtr(由函数本身创建),而循环迭代指向第一个内存位置? – user2525288

0

的decleration:

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

是不是与函数的定义一致:

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

不错,但那不幸的是。我仍然得到“在HW6_StringProcessing.exe中的0x774615de”未处理的异常:0xC0000005:访问冲突。“ – user2525288

+0

@ user2525288如果编译是正确的,那么只需使用调试器来找出访问冲突异常的位置。我没有你的数据。 – lulyon

0

这里有一个带注释的工作版本,其中包含一些更改和建议。你可能会想折腾最终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 == NULL)        //Check if file exists 
    { 
     printf("Failed"); 
     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. 
    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); 
}