簡體   English   中英

C編程,導致未處理的win32異常,可能是在strlen函數中。

[英]C programming, causing unhandled win32 exception, possibly in strlen function.

我在Visual Studio 2010中編寫的C程序拋出了未處理的Win32異常。

我認為它基於調試器的輸出,處於strlen函數中,但我不確定。 我正在讀取的文件帶有多行; 用作分隔符,並且錯誤似乎在我到達第一個鏈表的末尾時發生,大概是在readFile或insertNode中。

文件的第一行是這樣的:

blah division;first department;second department

任何幫助,將不勝感激。 我搜索了關於未處理的win32異常的StackOverflow搜索的前幾頁,它們似乎與訪問沖突或內存溢出問題有關

#define _CRT_SECURE_NO_WARNINGS 1
#define FLUSH while (getchar () != '\n')
#define DEFAULT "dept.txt"
#define LENGTH 50
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

//Structures
 typedef struct DEPT {
char * DeptName;
struct DEPT * link;  
} DEPT;

typedef struct {
char divisionName[LENGTH];
DEPT * first;
} DIVISION;

//Function Declarations
int ReadFile (DIVISION DivArr [], int * lastDiv);
FILE * getFileName (void); 
DEPT * insertNODE (DEPT * pList, char * string);

int main (void) {
//Local Declarations
//Create the array of the DIVISION Structure
DIVISION DivArr[20];
int i;
int lastDiv;
//Statements
//Read in File
if  (ReadFile (DivArr, &lastDiv)) {
    return 1;
}
for (i = 0; i < lastDiv; i++) {
    printf ("%s\n",DivArr[i].divisionName);
}
return 0;
}
/*==================================ReadFile==================================
Calls getFileName to get the file name to open, then reads the file's data into 
DivArr, parsing them appropriately, returning 1 if the file can't be opened */
int ReadFile (DIVISION DivArr [], int * lastDiv){
//Local Declarations
FILE * datafile;
char tempstring[300], *Ptoken;
int linenum = 0;
//Statements
datafile = getFileName();

//return from function with 1 if file can't be opened
//go through file line by line
while (fgets(tempstring, sizeof(tempstring), datafile)) {
    //tokenize string
    Ptoken = strtok (tempstring , ";");
    //first part of string is assigned to divisionName
    strncpy(DivArr[linenum].divisionName,  Ptoken, LENGTH - 1); 
    DivArr[linenum].first = NULL;

    //subsequent parts are assigned to linked list
    while(Ptoken) {
        Ptoken = strtok (NULL, ";\n");
        DivArr[linenum].first = insertNODE (DivArr[linenum].first, Ptoken);
    }
    linenum++;
}
*lastDiv = linenum;
fclose(datafile);
return 0;
} //ReadFile
/* =================================getFileName===============================
Gets input from the keyboard and if enter is pressed, returns default, otherwise                             returns specified filename */
FILE * getFileName (void){ 
//local declarations
int open = 1;
char read[LENGTH];
FILE * datafile = NULL;
//Statements
//open file
do{
    printf ("Enter a filename to open, or press enter for default:");
    fgets (read, LENGTH - 1, stdin);
    if ('\n' == read[0]) { 
        strncpy (read , DEFAULT, LENGTH - 1);
    }
    else
    read[strlen(read) - 1] = '\0';
    if((datafile  = fopen(read, "r")) == NULL)
        printf ("Error opening %s\n", read);
    else 
        open = 0;
} while (open == 1);
return datafile;
} //getFileName
/* =================================insertNODE================================
Gets the address of the beginning of the list for the structure, then
allocates memory for nodes, then allocates memory for string, then passes
string to allocated memory, then links node
*/
DEPT * insertNODE (DEPT * pList, char * string)
{
//Local Declarations
DEPT * pNew;
DEPT * pWalker = pList;
DEPT * pPre;
//Statements
if ( !(pNew = (DEPT*)malloc(sizeof(DEPT)))) 
        printf ("\nMemory overflow in insert\n"),
            exit (100);
printf ("size of string + null = %d\n",strlen(string) + 1);


    if(!(pNew->DeptName =(char*)calloc(strlen(string) + 1, sizeof(char))))
    {
        printf ("\nMemory overflow in string creation\n");
        exit (100);
    }
    strncpy(pNew->DeptName, string, strlen(string)); 
    printf("%s is %d long", pNew->DeptName, strlen(pNew->DeptName));

if (pWalker == NULL) //first node in list
{
    pNew->link = pList;
    pList = pNew;
}
else { 
    while (pWalker){
        pPre = pWalker;
        pWalker = pWalker->link;
    }
    pPre->link = pNew;
    pNew->link = NULL;
}
return pList;
}

此循環可能是您出錯的原因:

//subsequent parts are assigned to linked list
while(Ptoken) {
    Ptoken = strtok (NULL, ";\n");
    DivArr[linenum].first = insertNODE (DivArr[linenum].first, Ptoken);
}

strtok返回NULL時會發生什么? strtokinsertNODE 之間添加一個檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM