473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problems when using fwrite or fread.

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(triangles 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 3743
weaknessforcats
9,208 Expert Mod 8TB
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
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
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 Expert Mod 8TB
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>::iterator 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
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...
0
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...
4
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...
2
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
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. ...
6
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...
30
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
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...
12
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.