writing structure to file

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
Hi, i am facing a issues of writing structure to file.
I am able to write a structure to file, however i need to have a "line number" for every line in the file.

I try to use a loop to do the line number using this method

for int i = 0; i < ???; i++

but my structure writen to the file is dynamic, meaning my structure is design to keep on writing this structure to the file until it is choosen not to.

What should i put for the "???" .?

if i write like this for int i=0; i<100;i++, then line number 101 will not appear for the 101 line written to the file.

Or do i have to write finish the structure to the file, close it. Follow by reading in line by line using ifstream to get the number of line in the file and then open the file again and write the number of line?


Thank you
 

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
If getline for text file is getline(myfile,line);
then for binary i someone write like this

Code:
	std::ifstream ifs(L"c:\\james.rar", std::ios::binary);
	char buff[1000];
	ifs.seekg (0, std::ios::beg);
	int count = 0;
	while(!ifs.eof()) {
		ifs.read(buff, 1000);
		std::cout << count++ << std::endl;
	}

does the above means it will store every 1000 character as a new line?


for structure, is my code correct?

Code:
std::ifstream ifs("file.dat", std::ios::binary);
	struct astructure;
        astructure structure1;
	ifs.seekg (0, std::ios::beg);
	int count = 0;
	while(!ifs.eof()) {
		ifs.read(structure1);
		std::cout << count++ << std::endl;
	}
 

KnightNiwrem

Senior Member
Joined
Jun 1, 2014
Messages
1,057
Reaction score
0
If getline for text file is getline(myfile,line);
then for binary i someone write like this

Code:
	std::ifstream ifs(L"c:\\james.rar", std::ios::binary);
	char buff[1000];
	ifs.seekg (0, std::ios::beg);
	int count = 0;
	while(!ifs.eof()) {
		ifs.read(buff, 1000);
		std::cout << count++ << std::endl;
	}

does the above means it will store every 1000 character as a new line?


for structure, is my code correct?

Code:
std::ifstream ifs("file.dat", std::ios::binary);
	struct astructure;
        astructure structure1;
	ifs.seekg (0, std::ios::beg);
	int count = 0;
	while(!ifs.eof()) {
		ifs.read(structure1);
		std::cout << count++ << std::endl;
	}

I can't seem to understand what it is you are trying to do. Looking at your code though, my first question is: Have you tried to compile it yet?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Hi, i am facing a issues of writing structure to file.
I am able to write a structure to file, however i need to have a "line number" for every line in the file.

I try to use a loop to do the line number using this method

for int i = 0; i < ???; i++

but my structure writen to the file is dynamic, meaning my structure is design to keep on writing this structure to the file until it is choosen not to.

What should i put for the "???" .?

if i write like this for int i=0; i<100;i++, then line number 101 will not appear for the 101 line written to the file.

Or do i have to write finish the structure to the file, close it. Follow by reading in line by line using ifstream to get the number of line in the file and then open the file again and write the number of line?


Thank you

The technique you are visualising a file is wrong. Fundamentally a file has no lines. It's just a stream. If you have dynamic structures, then you will want a lookup table inside the file.

Meaning the construct of the file should start with key => value pair lookup table where the key is the approach to lookup a structure and value is the offset of the file in bytes to seek to before you reach the structure.

In your structure, you should also have size of the structure so that you know exactly how many bytes to read in (can be more), or more importantly how to map(using pointers) to the memory of bytes so that accessing the structure members will be correct.

The layout of the file should be something like this

<LOOKUP TABLE>
<FREE SPACE>
<STRUCT #0>
<STRUCT #1>
<STRUCT #2>
...
<STRUCT #N>


This is a simple layout. Should you get more advance, you need to think, how you want to handle the case where there are more entries than FREESPACE available for a lookup table.

You can design your <LOOKUP TABLE> in this manner

<LOOKUP TABLE HEADER>
<LOOKUP TABLE BODY/ENTRIES>

The HEADER should contain information like SIZE of the table, freespace available to expand the table, pointer to expansion of the body

So in fact, your file could be structure in

<LOOKUP TABLE HEADER #0><LOOKUP TABLE BODY #0>
<LOOKUP TABLE FREE SPACE #0>
<STRUCT #0>
<STRUCT #1>
...
<STRUCT #N>
<LOOKUP TABLE HEADER #1><LOOKUP TABLE BODY #1>
<LOOKUP TABLE FREE SPACE #1>
<STRUCT #N+1>
...
<STRUCT #M>


File design can be very creative.

There are also other simpler method of designing a file, such as creating a file-based hash table, a file-based tree.

There is no need for line numbers in a binary file structure because it simply doesn't make sense. It's a blob of binary.

When you want to try out something more advance. Once you get the stream I/O method working, if you are programming on Unix system, try out memory mapped file approach of accessing files. It's the most efficient and intuitive way of dealing with files as if they are dynamic memory .
 
Last edited:

cwchong

Master Member
Joined
Jan 7, 2005
Messages
4,654
Reaction score
96
"line number for every line in the file"
sounds very much to me like a presentation-level thing, not something you store as part of the file structure.

perhaps you are not understanding the original question you were given?
 

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
I have finally made changes to my code. My binary file is in the order
ID field
1 xxx
2 xxx
3 xxx

However the next time when my program is run, the file will become like this
1 xxx
2 xxx
3 xxx
1 xxx

Below is my part of my coding
Code:
struct x
{
int yID;
char item;
};

Code:
int GenerateID()
{
   static int pID=0;
   pID++;
   return pID;
}


Code:
void createMenu(x& xyz)
{
  ofstream outFile;
  outFile.open("file.dat", ios::out|ios::app|ios::binary);

   xyz.yID = GenerateID();
   cout << "Enter item ";
   cin >>xyz.item;
   outFile.write(reinterpret_cast<char *> (&xyz), sizeof(x));
   cout << endl;

  outFile.close();

}


Code:
void displayfile(x& xyz)
{
 ifstream inFile;
 inFile.open("file.dat",ios::in | ios::binary);
 if(!inFile)// 
  {
   cout<<"error";
   exit(-1);
  }

  while (inFile.read(reinterpret_cast<char *> (&xyz), sizeof(x)))
  {
      cout << xyz.yID << "\t"; // it can read the yID in structure well. In other words the yID has a proper value and not a garbage value.
    }
 inFile.close();
}


now i try to change my code to solve the error of
1 xxx
2 xxx
3 xxx
1 xxx

i change the write generate id function to read in the existing id first, but i seem to get garbage.
Code:
int GenerateID(x& xyz)
{  int abc;
   ifstream inFile;
   inFile.open("file.dat",ios::in | ios::binary);
   abc=(reinterpret_cast<char *> (&xyz), sizeof(x));//try to read in the binary file and read the last id
   cout << x;//test what the value of the id in the binary file, but the value is garbage value, if done correctly, the value should be either 0 or a value of the last id in the binary file
   if (abc==0)//if the id value is 0
   {
   static int pID=0; //then the function will generate a default value of 0 for id
   pID++; //increase the id value cause the first id generated is a value of 1 
   return pID;
   }
   else
   {
   static int pID=x; //else the value of x which is the existing id will pass into the id generate 
   pID++;
   return pID;
   }
}

The output is
id ------ field
217 --- xxx //i am not sure why after changing the code, the id generated is some weird number.
218 --- xxx


It's a long post, i hope question is clear. Sorry for the confusion as im hiding the details of my code.
 
Last edited:

AnimeNewbie

Suspended
Joined
Nov 1, 2003
Messages
8,050
Reaction score
1,966
Most C++ programmer write number of elements before writing array of binary structures. When reading, you read the number 1st, then the array.
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top