I am trying to write a code that reads data from a file, after reading the data it should look for duplicate and write only non duplicate values to a new file. however, when I run the program I get a segmentation error inside GetNext(). I've gone through the code several times I can't seem to find the invalid pointer inside GetNext() function. can any body help! see the code bellow.
//===========================================================================
// hybrid.h -- Specification file for hybrid class
// Time-stamp: <2010-10-25 19:24:46 Dnambembe>
// To compile:
//
// Copyright (c) 2010 by Domingos Nambembe. All rights reserved.
//===========================================================================
#include<fstream>
#define OUTDATA "licence.txt" // reading from file
#define fn "Noduplicate.text" //sorted file
struct NodeType; //Forward declaretion, complete declaretion is hiden
struct Record;
typedef Record ComponentType; // in implementation file
class HybridList
{
public:
void GetNext(); // extracts record from a file Licensea
void ReStart(); // calls getnext to get data
void SaveDB(Record* K); //create file with no duplicate
bool IsEmpty() const;
//condition:
// function value == true. if list is empty
// == false, otherwise
void Insert();
// precondition item is not in the list
//postcondition: item inserted && list is in ascending order
void Delete( ComponentType item);
// precondition: item is somewhere in list
//postcondition: item deleted && list in ascending order
HybridList();
// default constractor
// empty list is created
//HybridList(const SortedList& otherlist);
// copy-constractor, list is created as duplicate
~HybridList();
// destructor
// list is destroyed
private:
NodeType* head;
//Record Data;
};
//===========================================================================
// testhybrid.cpp --a test driver for the hybridlist class
// Time-stamp: <2010-10-29 18:52:46 Dnambembe>
// To compile:
// g++ testhybrid.cpp hybrid.o
// Copyright (c) 2010 by Domingos Nambembe. All rights reserved.
//===========================================================================
#include"hybrid.h"
#include<iostream>
usingnamespace std;
int main()
{
HybridList object;
for(int i = 0; i < 3; i++)
{
// object.ReStart();
object.GetNext();
object.Insert();
cout<< " Licensea file has been sorted and written to new file" <<endl;
}
}
It seems to me this is probably the most error-prone line in that member function:
bofs.read((char*)&Mydata, sizeof(Data));
is the size of the two records always the same? Check to make sure this doesn't over-write your pointer to a new element. That will cause a seg-fault. Also, this may be more a matter of preference, but perhaps when doing something of this nature you should use the size of the class/struct type (or data member!) instead of an instance of it.
Also, just purely for your benefit, I would like to know if you can use your IDE's debugging features--Visual Studio has excellent debugging features.
hi aiby
I don't think the input file has a problem. the licence.txt is a file I created using the code below if you compile and run it you have the
file created in your directory.
//============================================================================
// builtdoc.h -- A declaration for the classes that create the record of driv
// Time-stamp: <2010-10-19 19:51:46 Dnambembe>
//
//
// Copyright (c) 2010 by Domingos Nambembe. All rights reserved.
//============================================================================
#ifndef __builtdoc_H
#define __builtdoc_H // Prevent multiple #includes
#include<iostream>
#include<string.h> // need strlen & strcopy
#include<string>
#include<fstream>
#define fn "licence.txt"
usingnamespace std;
struct Record ; // forward declaration
class Tbuiltdoc
{
public:
Tbuiltdoc(); // prompt user to enter name & address first
//then License number
~Tbuiltdoc();
bool IsEmpty(); //call first to see if first record is not created
void SaveDB(Record *af); // this will insert subsiquent records
void GetInput();
private:
// char* NameAddress; // the string containg name and address
//long int License; // License number string
// pointer to the record struct
};
#endif // __bstream_H
void HybridList::Insert( )
{
// precondition: component members of the list are in ascending order
//postcondition: new Node containing item is in proper place
NodePtr newNodePtr = new NodeType; // set-up node to be inserted
newNodePtr->component = Data;
// if license number are iqual exit
// insert item
ComponentType newData = Data; //pass the struct record
SaveDB(&newData); //write the sorted data to new file
newNodePtr->Link = currentPtr;
if(prevPtr == NULL) head = newNodePtr;
Hi dnambembe
I used Dev-cpp4.9.9.2 for windows xp.
I'm going to linux to C/C++/QT programming at next week, so did you have any suggest for me such as linux version,tools,etc..
for linux version snd tools, i'll suggest a linux ubuntu 10.4 which is free and it allows you to download any application and tools you might need for your project. however if you are looking for something very fancy the latest mandrake version will be best because I found out that there a lot of books writen specifically for MN.