Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7789993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:27:25+00:00 2026-06-01T21:27:25+00:00

My c program, written in the visual-studio 2010, is throwing an unhandled win32 exception.

  • 0

My c program, written in the visual-studio 2010, is throwing an unhandled win32 exception.

I think it’s in a strlen function, based on the debugger output, but I’m not sure.
The file I’m reading in is multiple lines with ; used as a delimiter, and the error seems to happen as I reach the end of the first linked list so presumably in readFile or insertNode.

The first line of the file is something like:

blah division;first department;second department

Any help would be appreciated. I searched through the first few pages of a StackOverflow search on unhandled win32 exceptions, and they seem to relate to access violations or memory overflow problems

#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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:27:26+00:00Added an answer on June 1, 2026 at 9:27 pm

    This loop might be the reason of your error:

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

    What happens when strtok returns NULL? Add a check for that between strtok and insertNODE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a program in C# using Visual Studio 2010. At current it
I have written a C# program in Visual Studio 2010 . I use database
I have a Visual Studio 2010 C++ program, the main function of which is:
I have a data acquisition program written in C++ (Visual Studio 6.0). Some clients
I have a program written in VB.Net (Visual Studio 2008) that uses a DLL
I'm using Visual Studio 2005. I have a program written in C#. When I
Visual Studio C++ 2008 / GCC 4.4.2 I have written a program to run
I have visual studio 2008. I have a written a small C program. I
My C(++) program, written and compiled using Visual C(++)/Visual Studio, runs fine on my
Consider the following code (written with Visual Studio 2010 and .NET 4.0) using System;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.