473,549 Members | 2,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems when using fwrite or fread.

3 New Member
Hello there.

I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models.
When I store mesh data, I have to store data for each submodel and their respective triangle data, wich are the vertex and neighbor(triang les sharing a common edge) indexes. Now, I'm storing this data in these struct's:

Expand|Select|Wrap|Line Numbers
  1.  struct Face
  2. {
  3.     Face() {}
  4.     Face(GLushort v0, GLushort v1, GLushort v2)
  5.     {
  6.         vertexIndexes[0] = v0;
  7.         vertexIndexes[1] = v1;
  8.         vertexIndexes[2] = v2;
  9.     }
  10.     Face(GLushort v0, GLushort v1, GLushort v2,
  11.         GLushort v3, GLushort v4, GLushort v5)
  12.     {
  13.         vertexIndexes[0] = v0;
  14.         vertexIndexes[1] = v1;
  15.         vertexIndexes[2] = v2;
  16.  
  17.         neighborIndexes[0] = v3;
  18.         neighborIndexes[1] = v4;
  19.         neighborIndexes[2] = v5;
  20.     }
  21.     int vertexIndexes[3];
  22.     int neighborIndexes[3];
  23. };
  24.  
  25. struct SubModelData
  26. {
  27.     std::vector<Face> faces;
  28. };
  29.  
  30. struct ModelExtraData
  31. {
  32.     std::vector<SubModelData> submodels;
  33. };
  34.  
  35. typedef std::map<std::string, ModelExtraData> ExtraDataMap;
And this is how I register the structs in the Studio rendering code:

Expand|Select|Wrap|Line Numbers
  1.     // Data for shadow volume rendering.
  2.     ExtraDataMap    m_ExtraData;
  3.     ModelExtraData    *m_pCurretExtraData;
Now, the problem comes when I try to save all this data to a single .dat file of mine. I manage to save the data properly for the amount of submodels, the amount of triangles, but when it comes to saving the Face struct array, it either reads it or writes it improperly. This is because when I read it back into a new array, every index has it's variables filled correctly, but the problem comes after the vertex indexes of the 25th Face, after wich all the variables are filled with 0's.

This is how the functions that I use to save and read data looks like:
Expand|Select|Wrap|Line Numbers
  1. /*
  2. ====================
  3. StudioLoadData
  4.  
  5. Load data from the .dat file.
  6. ====================
  7. */
  8. bool CStudioModelRenderer::StudioLoadData( /*SubModelData &dst, int bPartIndex*/ )
  9. {
  10.     int i;
  11.     FILE *pFile;
  12.     char str1[256], szFile[256];
  13.  
  14.     std::string filename(m_pRenderModel->name);
  15.     sprintf(str1, "/%s.dat", filename.substr(0, filename.rfind('.')).c_str() );
  16.  
  17.     strcpy( szFile, gEngfuncs.pfnGetGameDirectory() );
  18.     strcat( szFile, str1 );
  19.  
  20.     pFile = fopen (szFile, "r");
  21.  
  22.     if ( pFile )
  23.     {
  24.         int iSubmodelCount;
  25.         fread( &iSubmodelCount, 4, 1, pFile );
  26.  
  27.         m_pCurretExtraData->submodels.resize(iSubmodelCount);
  28.  
  29.         for ( i = 0; i < iSubmodelCount; i++ )
  30.         {
  31.             int iTriangleCount;
  32.             Face faces[MAXSTUDIOTRIANGLES];
  33.             SubModelData &dst = m_pCurretExtraData->submodels[i];
  34.             fread( &iTriangleCount, 4, 1, pFile );
  35.  
  36.             dst.faces.reserve(iTriangleCount);
  37.  
  38.             fread( &faces[0], sizeof(Face), iTriangleCount, pFile );
  39.  
  40.             if ( faces )
  41.                 return true;
  42.         }
  43.         fclose( pFile );
  44.         return true;
  45.     }
  46.     return false;
  47. }
  48. /*
  49. ====================
  50. StudioWriteData
  51.  
  52. Writes Shadow volume data into a file.
  53. ====================
  54. */
  55. void CStudioModelRenderer::StudioWriteData( void )
  56. {
  57.     int i;
  58.     FILE *pFile;
  59.     char str1[256], szFile[256];
  60.  
  61.     std::string filename(m_pRenderModel->name);
  62.     sprintf(str1, "/%s.dat", filename.substr(0, filename.rfind('.')).c_str() );
  63.  
  64.     strcpy( szFile, gEngfuncs.pfnGetGameDirectory() );
  65.     strcat( szFile, str1 );
  66.  
  67.     if ( (pFile = fopen (szFile, "w")) != NULL )
  68.     {
  69.         int iSubmodelCount = m_pCurretExtraData->submodels.size();
  70.         fwrite( &iSubmodelCount, 4, 1, pFile );
  71.  
  72.         for ( i = 0; i < iSubmodelCount; i++ )
  73.         {
  74.             SubModelData &dst = m_pCurretExtraData->submodels[i];
  75.             int iTriangleCount = dst.faces.size();
  76.  
  77.             fwrite( &iTriangleCount, 4, 1, pFile );
  78.             fwrite( &dst.faces[0], sizeof(Face), dst.faces.size(), pFile );
  79.         }
  80.         fclose( pFile );
  81.     }
  82.  
  83. }
Please, don't laugh at it, It's mostly a debug code right now.

I've been working on it for a long time, but I still couldn't figure out what's causing this problem. The code seems correct, to me that is, with my limited programming knowledge.

Thanks for any help possible.
Oct 1 '08 #1
4 3758
weaknessforcats
9,208 Recognized Expert Moderator Expert
What's more important it defining your file layout. You need to know what you wrote in order to be able to read it back.

I know your data is in structs and what I see there are two arrays of int that are 3 elements each. Therefore, one format might be Face per record in the file.
Let's say vertexIndexes followed by neighborIndexes .

So, you write 3 ints for vertexIndexes followed by 3 ints for neighborIndexes followed by a \n.

When you read, you read 3 int for vertexIndexes and 3 int neighborIndexes .

Then move the ints that were read into a Face variable and off you go.

You should be able to use fprintf to write and fscanf to read.
Oct 1 '08 #2
Highlander2nd
3 New Member
I actually used that method before, I wrote the numbers out for each integer in one line. But no, I want to write out all of it in one for each submodel. The previous one was too... sloppy for me, and I would have to save for each submodel, wich would create a folder full of files for me.

I want to stay away from that method, it isn't what I want to achieve. I'll also have to write vertex and bone vertex index data, wich are stored in different types of variables. It's for optimization purposes, as the code right now limits me to low-poly models if I want to keep performance.

Thanks though, I'll keep trying and studying.
Oct 1 '08 #3
Highlander2nd
3 New Member
Alright, I think I've solved the problem. It seems as if I should've used iostream in the first place. It works right now.

My last question would be about the dst.faces array. If you were me, how would you load the data directly into that array? I hate using dst.faces.push_ back, because it messes up values that have -1, it could be done easier than having to do it for each Face.

Thanks.
Oct 2 '08 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
You can create a vector and iniialize it with an array:
Expand|Select|Wrap|Line Numbers
  1. int arr[9] = {1,2,3,4,5,6,7,8,9};
  2. vector<int> v(arr, arr+1);
  3.  
No need for inividual push_back calls. You can use an int* as a vector<int>::it erator so this is really an intialization from begin() to end(), where end() is one beyond end of the array.

You should be able to load this array in one read based a read that uses 9* sizeof(int) and a pointer to an int array. This will require a binary file rather than a text file. Here you write out 9*sizeof(int) and you read back 9*sizeof(int).

If your arrays have holes, the holes will be written and read back. That is, the disc file will be a mirror image of the memory array.
Oct 4 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

17
6519
by: Axel | last post by:
Hiho, here my Newbie question (Win32 GUI): I'm reading a file in binary mode to a char* named buffer. I used malloc(filesize) to make the needed space avaiable. The filedata in the buffer seems to be ok..I can write the variable buffer back to a file and the contents is ok. So I think until here all goes fine... But now I want write...
0
1163
by: pieter.breed | last post by:
Hi, background: ---------- I am the maintainer of a COM C++ dll that is used to read binary data from a specific file format. I was recently tasked to speed up the execution speed of this dll. In analysis I saw that you would very often get fseek()'s followed by either fwrite()'s or fread()'s. So my
4
7800
by: bill | last post by:
I am confused by the behavior of the following code. The function copy() takes two FILE *'s and copies the data from one to the other. In the main routine, I create a pipe and copy stdin to the write end while copying the read end to stdout. If the child handles copying the read end of the pipe to stdout, the main program returns. If the...
2
6301
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
2
1572
by: afrin216 | last post by:
Hi, I'm a beginner in programming with C++. Well I've built a program which requires extensive usage of files and I've been using binary file formats and fread and fwrite commands to perfection. The project was going well till the curtain was dropped.ie of lately I've been getting junk output whenever I used fread. I noticed that I got the...
6
1898
by: Lars Erik Bryld | last post by:
Hello group I am trying to convert a (supposedly) working python script into php. The script reads a colour gif-picture and produces a greyscale copy of it. The method is to first copy the gif header, then establish the size of the colour palette, then do a simple averaging of each colour byte triplet into a grayscale equivalent, and...
30
14252
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
11
4277
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At C89 fwrite/fread were added to the C standard to allow portable binary IO to files. I wonder though why the choice was made to extend the unix...
12
3155
by: Medvedev | last post by:
FILE *a,*b; int z; char str1; char str2; a= fopen(argv,"rb"); //Both files are EXE's b= fopen(argv,"rb+"); z= fread(str1,1,1,a); if (z !=1)
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.