#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>
struct NGC
{
double X;
double Y;
double Z;
int Point_ID;
NGC* next;
};
void SaveNGC(char* FileName, NGC* ngc);
void OpenNGC(char* FileName);
void AddPoint(NGC** Head, double X, double Y, double Z)
{
NGC* temp = *Head;
static int num = 0;
while(temp != NULL)
{
temp = temp->next;
}
if(*Head == NULL)
{
*Head = new NGC;
(*Head)->X = X;
(*Head)->Y = Y;
(*Head)->Z = Z;
(*Head)->Point_ID = num;
(*Head)->next = NULL;
}else{
NGC* temp = new NGC;
temp->X = X;
temp->Y = Y;
temp->Z = Z;
temp->Point_ID = num;
temp->next = *Head;
*Head = temp;
}
num++;
}
void Print(NGC* Head, int Number)
{
if(Head == NULL)
{
cout << "Head == NULL\n";
return;
}
NGC* temp = Head;
while(temp != NULL)
{
if(temp->Point_ID == Number)
{
cout << temp->X << endl;
cout << temp->Y << endl;
cout << temp->Z << endl;
return;
}
temp = temp->next;
}
cout << Number << " not found!!!\n";
}
int FileLen(char* FileName)
{
FILE* file;
int count = 0;
file = fopen(FileName, "rb+");
if(file == NULL)
{
return -1;
}
while(1)
{
char c;
c = fgetc(file);
if(c == EOF)
{
break;
}else{
count++;
}
}
return count;
}
int main(int argc, char* argv[])
{
bool go = true;
NGC* ngc;
ngc = NULL;
char yn;
char Path[255];
double X, Y, Z;
char rw[6];
cout << "Read or Write a file? ";
cin >> rw;
if(strcmp(rw, "read") == 0)
{
cout << "Enter Path: ";
cin >> Path;
OpenNGC(Path);
return 0;
}
while(go == true)
{
cout << "Enter X: ";
cin >> X;
cout << "Enter Y: ";
cin >> Y;
cout << "Enter Z: ";
cin >> Z;
AddPoint(&ngc, X, Y, Z);
cout << "Enter another point? ";
cin >> yn;
if(yn == ''n'')
{
go = false;
}
}
yn = ''y'';
while(yn == ''y'')
{
cout << "Display 3D_POINT? ";
cin >> yn;
if(yn == ''y'')
{
int num;
cout << "3D_POINT: ";
cin >> num;
Print(ngc, num);
}
}
cout << "Enter Path: ";
cin >> Path;
SaveNGC(Path, ngc);
delete ngc;
return 0;
}
void SaveNGC(char* FileName, NGC* ngc)
{
ofstream fout(FileName, ios::binary);
fout.write(&ngc, sizeof(ngc));
fout.close();
}
void OpenNGC(char* FileName)
{
NGC* ngc2;
ngc2 = NULL;
int num;
ifstream fin(FileName, ios::binary);
fin.read(&ngc2, sizeof(ngc2));
fin.close();
cout << "Enter Point_ID: ";
cin >> num;
Print(ngc2, num);
delete ngc2;
}
Cannt Open Struct
Im trying to make my own 3d character file. So I started to make an editor for it just so I can make it work with OpenGL. But everytime I goto load the struct it wont load it. Im pretty sure its saving right and all the code looks right. Can someone plz help me?
thx
nuke
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
You should try STL library instead of creating your own linked list each time you need it with specified params.
Here''s an example of a vector library that adds pointers created in run time and it can add as much of them as you can. Though it lacks searching by IDs and cetera, but I think you can easily do a linear search with it to find specified item:
" Do we need us? "
Here''s an example of a vector library that adds pointers created in run time and it can add as much of them as you can. Though it lacks searching by IDs and cetera, but I think you can easily do a linear search with it to find specified item:
// Library.h: interface for the CLibrary class.////////////////////////////////////////////////////////////////////////#include <vector>using namespace std;#if !defined(AFX_LIBRARY_H__F8285E45_82D8_4003_889A_DD3C4EDE53F4__INCLUDED_)#define AFX_LIBRARY_H__F8285E45_82D8_4003_889A_DD3C4EDE53F4__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000template <class T> class CLibrary {private: vector <T> Array;public: bool Ptr; // if members of vector are pointers, delete all of them automatically on destruction // Get empty cell void __inline Add(T obj){Array.push_back(obj);}; // Array retreval operations T Get(int i){return Array[i];}; int __inline GetSize(){ return Array.size();}; void __inline Erase() { if(Ptr) { for (int i=0; i<Array.size(); i++) delete Array[i]; } Array.erase(Array.begin(), Array.end()); }; CLibrary(){Ptr = false;}; virtual ~CLibrary(){Erase();};};#endif // !defined(AFX_LIBRARY_H__F8285E45_82D8_4003_889A_DD3C4EDE53F4__INCLUDED_)
" Do we need us? "
" Do we need us? "Ionware Productions - Games and Game Tools Development
I''m not sure I follow your code, but you can''t save and load pointers, since they probably won''t point at the same thing when you read them as when you saved them.
[ PGD - The Home of Pascal Game Development! ] [ Help GameDev.net fight cancer ]
That looks like its from VC++ are you sure it will work on linux? I dont get your code it seems confusing.
My friend said you can save a linked list, that is what that is.
My friend said you can save a linked list, that is what that is.
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
sizeof(ngc) will be 4 (bytes) because it''s a pointer. That''s probably not what you want in the above example...
wouldnt sizeof(NGC) give me 4 bytes but sizeof(ngc) give me the size of the linked list? NGC is the struct itself but ngc is what data has been written to.
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
I was talking about this code here...
The bit "sizeof(ngc)" here is saying "sizeof(NGC*)" which is gonna be four. I'd advise you to test it. One possible way of getting around this is to use void ptrs and casting, but that's pretty damned ugly. For example...
Uncomment the #define to read in the stuff once you've run the program once. Note that using void* is pretty iffy in C++ and I'd strongly advice you to avoid it. A better solution would be to use a list or vector from the STL (as someone suggested above, I think).
EDIT: and using void* + casting would probably mess up if you had any pointers within your structure.
Alternatively, give your NGC class methods to "write" and "read". You should be doing things in OO after all with C++. The pseudocode would look like this:
[edited by - Alimonster on May 27, 2002 10:15:22 PM]
quote:
void SaveNGC(char* FileName, NGC* ngc)
{
ofstream fout(FileName, ios::binary);
fout.write(&ngc, sizeof(ngc));
fout.close();
}
The bit "sizeof(ngc)" here is saying "sizeof(NGC*)" which is gonna be four. I'd advise you to test it. One possible way of getting around this is to use void ptrs and casting, but that's pretty damned ugly. For example...
#include <fstream>#include <iostream>using namespace std;#define DO_WRITEvoid write(const char* filename, void* ptr, size_t size){ ofstream fout(filename, ios::binary); fout.write((char*)ptr, size);}void read(const char* filename, void* ptr, size_t size){ ifstream fin(filename, ios::binary); fin.read((char*)ptr, size);}int main(){ int myArray[10]; #ifdef DO_WRITE for (int i = 0; i < 10; ++i) myArray[i] = i; write("c:\\abc.dat", myArray, sizeof(myArray));#else read("c:\\abc.dat", myArray, sizeof(myArray)); for (int i = 0; i < 10; ++i) cout << myArray[i] << endl;#endif // DO_WRITE return 0;}
Uncomment the #define to read in the stuff once you've run the program once. Note that using void* is pretty iffy in C++ and I'd strongly advice you to avoid it. A better solution would be to use a list or vector from the STL (as someone suggested above, I think).
EDIT: and using void* + casting would probably mess up if you had any pointers within your structure.
Alternatively, give your NGC class methods to "write" and "read". You should be doing things in OO after all with C++. The pseudocode would look like this:
void doWrite(NGC* myList){ //write length of list NGC* pCurrent = myList; while (myList) { myList[current node]->write(...); }}void doRead(NGC* firstNode){ // read list length for (wanted length) { //create new NGC new_ngc->read(...) // add new ngc node to the list }}
[edited by - Alimonster on May 27, 2002 10:15:22 PM]
I dont get the 2nd code. You say I should write a read and write funtion but it is a linked list. How would I write a funtion to save for each items data? This isntan array.
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
Yeah, that was a mess-up in the pseudo-code for doWrite. It would look like this now I think about it...
Any clearer? Remember that in C++ a struct is *exactly* like a class but it starts off with "public" instead of "private" access. Therefore, you could have your NGC struct like this:
[edited by - Alimonster on May 27, 2002 10:45:12 PM]
void doWrite(ofstream& output, NGC* myList){ // write the list length - you'd store that somewhere.... NGC* pCurrent = myList; while (pCurrent) { pCurrent->write(output); pCurrent = pCurrent->Next; }}
Any clearer? Remember that in C++ a struct is *exactly* like a class but it starts off with "public" instead of "private" access. Therefore, you could have your NGC struct like this:
struct NGC{ double X; double Y; double Z; int Point_ID; NGC* next; void write(ofstream& output) { output << X; //etc } void read(ifstream& input) { input >> X; //etc }};
[edited by - Alimonster on May 27, 2002 10:45:12 PM]
OpenNGC dosnt work everytime it says "Segmentation fault"
Here is my save funtion and struct incase thats it.
[edited by - Nukem on May 27, 2002 11:41:10 PM]
Here is my save funtion and struct incase thats it.
struct NGC{ double X, Y, Z; int Point_ID; NGC* next; void Write(ofstream& output) { output << X << Y << Z << Point_ID; } void Read(ifstream& input) { input >> X >> Y >> Z >> Point_ID; }};void SaveNGC(char* FileName, NGC* ngc){ NGC* ngc2 = ngc; ofstream fout(FileName, ios::binary); while(ngc2) { ngc2->Write(fout); ngc2 = ngc2->next; }}
void OpenNGC(char* FileName, NGC* ngc){ ifstream fin(FileName, ios::binary); for(int a = 0; a != FileLen(FileName; a++) { ngc->Read(fin); }}
[edited by - Nukem on May 27, 2002 11:41:10 PM]
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement