Q:

My C++ program can run but always pop up error

This is my Code:
GraphBuilder.h
//#pragma once
#include<iostream>
#include<stdio.h>
#include<fstream>
#include <stdlib.h>
#include <string>
using namespace std;

#define MaxVertexNum 500000    

struct Node{
    int data;
    struct Edge *next;
};

struct Edge{
    int data;
    int weight;
    struct Edge* next;
};

class GraphBuilder
{
public:
    GraphBuilder();
    void CreateGraph();
    void printGraph();
    Node *header;
    int total_of_nodes, total_of_edges;

private:
};

GraphBuilder.cpp
#include"GraphBuilder.h"
using namespace std;

GraphBuilder::GraphBuilder()
{
}

void GraphBuilder::CreateGraph()
{
    int i,j,k;
    int vex1, vex2, weight;
    char a;
    Edge *tmp, *newNode;     
    FILE *fp;
    int line= -1;
    fp = fopen("Text1.txt", "r");

    if(fp == NULL)
    {
        cout<<"Cannot open file!\n";
        return;
    }
    while(!feof(fp))
    {
        if(line == -1)
        {
            fscanf(fp, "%d %d", &total_of_nodes, &total_of_edges);    
            line++;
        }
        else break;
    }

    for(i=0;i<total_of_nodes;i++) 
    {
        header[i].data = i;   
        header[i].next = NULL;
    }    
    while(!feof(fp))
    {
        if(line == -1)
        {
            fscanf(fp, "%d %d", &total_of_nodes, &total_of_edges);  
            line++;
        }
        else
        {
            fscanf(fp, "%d %d %d", &vex1, &vex2, &weight);    
            newNode = (Edge *)malloc(sizeof(Edge));   
            newNode->data = vex2;          
            newNode->weight = weight;
            newNode->next = NULL;
            if (header[vex1].next == NULL)
                header[vex1].next = newNode;   
            else 
            {
                tmp = header[vex1].next;
                header[vex1].next = newNode;
                newNode->next = tmp;
            }
        }
    }
}

void GraphBuilder::printGraph()
{
    int i;
    Edge* tmp;
    for (i=0; i<total_of_nodes; i++)
    {
        cout<<header[i].data;
        if (header[i].next != NULL)
        {
            tmp = header[i].next;
            cout<<"->"<<tmp->data;
            while (tmp->next != NULL)
            {
                cout<<"->"<<tmp->data;
            }
        }
        cout<<endl;
    }
}

main.cpp
#include"GraphBuilder.h"
using namespace std;

void main()
{
    GraphBuilder gb;
    gb.CreateGraph();
    gb.printGraph();
}

I run the code on VS2012, and it always pop up the error that an Access violation occurs. I don't know why this error occurs, I am a freshmen in C++. Please tell me how to correct my code.
Thanks for your help. 

A:

Doesn't your compiler warn you?
const int total_of_nodes = 0, , total_of_edges = 0;

and
fscanf(fp, "%d %d", &total_of_nodes, &total_of_edges);

That can't be good. You're modifying const object, it's undefined behaviour.

A:

Your problem of access violation comes from accessing your header array without allocating space for it previously:
for(i=0;i<total_of_nodes;i++) 
    {
        header[i].data = i;   
        header[i].next = NULL;
    } 

Either use dynamic allocation by:
Node *header;
...
header=(Node*) malloc(SIZE*sizeof(Node));

or alternatively
Node *header = new Node[SIZE];

or allocate your header statically by using:
Node *header[SIZE];

A:

It seems header is never allocated.

