#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;

struct UKStudent
{
	char name [30];
	int noTest;
	float mark [10];
	int final;
	char grade [30];
};

struct OverseasStudent
{
	char country [30];
	char name [30];
	int noTest;
	float mark [10];
	int final;
	char grade [30];
};

union EveryStudent
{
	UKStudent               uks;
	OverseasStudent	oss;
};

struct Student
{
	char type;   //Not sure what is this?
	EveryStudent st;
};

infile.txt
----------------------------------------------------------------------
UK Michael_Owen
O Australian David_Cooper
O Japanese Arikato
----------------------------------------------------------------------

My 1st task is to read the infile.txt. My 2nd task is to convert the content in infile.txt to a binary file format, outfile.dat
Qns: 1st task: Can advise whether my code is ok?

My attempt code:

void createfile (fstream& afile, char filename [])
{
       afile.open (filename, ios::out);
        char type; //From struct student, not too sure

        for (int i=1; i <=10; i++)
       {
                type = rand () % 3; //Need to use rand?
                
                 switch (type)
                 {
                        case 'UK': afile << type << "\t" << (rand() % 30 + 1) << endl;
                                        break;
                        case 'O': afile << type <<"\t" << (rand() % 30 + 1)
                                                <<"\t" << (rand() % 30 + 1) << endl;
                                        break;
                  }
        }
        afile.close( );
}

Edited 6 Years Ago by makan007: n/a

>>Qns: 1st task: Can advise whether my code is ok?
Compile and run it to find out for yourself. But I faile to see how that's useful to your stated problem. The problem you posted does not ask you to write numbers to the output file.

I was tired last night & my first post code was entirely wrong.
I can compile, build the below code but somehow my compiler will crash when I execute. Is it ok?

Pls see amended code:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

const int MAX = 20;
const int MAX1 = 30;

struct UKStudent
{
	char name [MAX1];
	int noTest;
	float mark [MAX];
	int final;
	char grade [MAX1];
};

struct OverseasStudent
{
	char country [MAX1];
	char name [MAX1];
	int noTest;
	float mark [MAX];
	int final;
	char grade [MAX1];
};

union EveryStudent
{
	UKStudent       uks;
	OverseasStudent	oss;
};

struct Student
{
	char type;   
	EveryStudent st;
};


int arraytofile (fstream&, char [], Student []);


int main()
{

	fstream afile;
	
	Student stu [MAX];
	
	int size = arraytofile (afile,"csci124.txt",stu);
	

}


int arraytofile (fstream& afile, char filename [], Student stu [])
{
    afile.open (filename, ios::in);
	
	if(!afile.good ())
	{
		cout << "The input file does not exist. " << endl;
		return 1;
	}
	
	char n;
	int i = 0;
	
	while (afile >> n)
	{
		switch (n)
		{
			case 'U': stu [i].type; //Correct? How to refer back to infile.txt, first column?
					  afile >> stu [i].st.uks.name [MAX1];
					  break;
					
			case 'O': stu [i].type; //Correct? How to refer back to infile.txt, first column?
					  afile >> stu [i].st.oss.country [MAX1];
					  afile >> stu [i].st.oss.name [MAX1];
		}
		
		++i;
	}
	   
	
	afile.close ();
	return i;
}

infile.txt
----------------------------------------------------------------------
U Michael_Owen
O Australian David_Cooper
O Japanese Arikato
----------------------------------------------------------------------

afile >> stu[i].st.uks.name[MAX1];

This line (and similar lines in your code) doesn't do what you want. name[i] is a single character, not a string. Besides that, what does name[MAX1] refer to?

How to refer back to infile.txt, first column?

Hint: you just read it into n You are very close to a working file reading part.

Edited 6 Years Ago by nezachem: n/a

nezachem, my code do not have name, so I am unsure about what you are refering to.

I have defined const int MAX1 = 20; under line 7. name[MAX1] is to assign the array size for name to 20.

Can elaborate on the hint?

Edited 6 Years Ago by makan007: n/a

Break it down without the constants. You've declared an array of size 30 up above and you're trying to access the element #30 on lines 76-81. Except the index of element 30 is not 30, it's 29.

Nonetheless even if you had the proper index, you are reading into your char array incorrectly. Look up how to use the getline() method of your stream. See here for details.

When you have an array you declare it as type arr[size]; When you access the array you put in the desired index, e.g.,

int arr[5]; // holds 5 elements
                 //but the first element of an array is arr[0]
cin >> arr[5]; //INVALID since arr[5] is the 6th element of the array
                       // no such thing

now with:
afile >> stu [i].st.uks.name [MAX1] ;

you're accessing stu so far so good, member st.uks, ok that's valid, and now you try to access the name member, but at MAX1 which is one index too far (at best you'll hit another initialized area of memory but at worst your program will crash and burn).

So your ifstream statement is trying to write the name you read in from afile, but read it into a variable that's only 1 char long (accessing name[index] only give you one char out of your array) and into a character that you don't have the "rights" to.

Enter getline. Did you read the reference at all? This is a method that will help you read into your char arrays from the file. afile.getline(stu .st.uks.name) (NO index this time) will read from your afile into name. Read the reference about delimiters and decide how to separate one line of your text file into multiple "lines" to read into your variables (hint: what separates the country of the student from his/her name).

Edited 6 Years Ago by jonsca: n/a

Yes, I have read the link provided. But I cannot comprehend everything right now. Sorry.

Anyway, I amended the code iin line 76, 80 & 81. For your further advise.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

const int MAX = 20;
const int MAX1 = 30;

struct UKStudent
{
	char name [MAX1];
	int noTest;
	float mark [MAX];
	int final;
	char grade [MAX1];
};

struct OverseasStudent
{
	char country [MAX1];
	char name [MAX1];
	int noTest;
	float mark [MAX];
	int final;
	char grade [MAX1];
};

union EveryStudent
{
	UKStudent       uks;
	OverseasStudent	oss;
};

struct Student
{
	char type;   
	EveryStudent st;
};


int arraytofile (fstream&, char [], Student []);


int main()
{

	fstream afile;
	
	Student stu [MAX];
	
	int size = arraytofile (afile,"infile.txt",stu);
	

}


int arraytofile (fstream& afile, char filename [], Student stu [])
{
    afile.open (filename, ios::in);
	
	if(!afile.good ())
	{
		cout << "The input file does not exist. " << endl;
		return 1;
	}
	
	char n;
	int i = 0;
	
	while (afile >> n)
	{
		switch (n)
		{
			case 'U': stu [i].type; //Correct? How to refer back to infile.txt, first column?
					  afile.getline (stu [i].st.uks.name,MAX); //Amended the code. Is this considered as working? No crash now.
					  break;
					
			case 'O': stu [i].type; //Correct? How to refer back to infile.txt, first column?
					  afile.getline (stu [i].st.oss.country,MAX); //Amended the code. Is this considered as working? No crash now.
					  afile.getline (stu [i].st.oss.name,MAX); //Amended the code. Is this considered as working? No crash now.
		}
		
		++i;
	}
	   
	
	afile.close ();
	return i;
}

Edited 6 Years Ago by makan007: n/a

afile.getline (stu [i].st.oss.country,MAX,' '); is the only change I would make, as your country and name are separated by a space so to stay on the same line ' ' is needed.

You can set stu[i].type = n; (or read into stu.type with your loop condition (afile >> stu.type instead of afile >> n).

Edited 6 Years Ago by jonsca: n/a

afile.getline (stu [i].st.oss.country,MAX,' '); is the only change I would make, as your country and name are separated by a space so to stay on the same line ' ' is needed.

You can set stu[i].type = n; (or read into stu.type with your loop condition (afile >> stu.type instead of afile >> n).

Tks jonsca. Understand the explanation. I will now need to work on the other functions now.

I am back again. I am now doing my 2nd task which is to convert the infile.txt to a binary file format, I called it outfile.dat
I have opened the outfile.dat after compiled but found that there is no unreadable computer language in the text body. Pls let me know what went wrong?

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

const int MAX = 20;
const int MAX1 = 30;

struct UKStudent
{
	char name [MAX1];
	int noTest;
	float mark [MAX];
	int final;
	char grade [MAX1];
};

struct OverseasStudent
{
	char country [MAX1];
	char name [MAX1];
	int noTest;
	float mark [MAX];
	int final;
	char grade [MAX1];
};

union EveryStudent
{
	UKStudent       uks;
	OverseasStudent	oss;
};

struct Student
{
	char type;   
	EveryStudent st;
};


int arraytofile (fstream&, char [], Student []);
void process_bintext (fstream&, char [], Student [], int);

int main()
{

	fstream afile;
	
	Student stu [MAX];
	
	int size = arraytofile (afile,"infile.txt",stu);
	process_bintext (afile,"outfile.dat", stu, size);

}

//Read infile.txt
int arraytofile (fstream& afile, char filename [], Student stu [])
{
    afile.open (filename, ios::in);
	
	if(!afile.good ())
	{
		cout << "The input file does not exist. " << endl;
		return 1;
	}
	
	char n;
	int i = 0;
	
	while (afile >> n)
	{
		switch (n)
		{
			case 'U': stu [i].type = n; 
					  afile.getline (stu [i].st.uks.name,MAX);
					  break;
					
			case 'O': stu [i].type = n; 
					  afile.getline (stu [i].st.oss.country,MAX,' '); 
					  afile.getline (stu [i].st.oss.name,MAX);
		}
		
		++i;
	}
	   
	
	afile.close ();
	return i;
}

//Convert infile.txt as binary file
void process_bintext (fstream& afile, char filename [], Student stu [], int size)
{
	afile.open (filename, ios::out | ios::binary);
	
	if(!afile.good())
	{
		cout <<"Failed to write file. " << endl;
		return;
	}
	
	
	char n;
	
	for (int i = 0; i < size; i++)
	{ 	   
			stu [i].type = n;
			afile << afile.getline (stu [i].st.uks.name,MAX);
			afile << afile.getline (stu [i].st.oss.country,MAX,' '); 
		    afile << afile.getline (stu [i].st.oss.name,MAX);
			
			afile.write (reinterpret_cast <const char *> (&stu), sizeof (stu));
	}	   	   
	
	afile.close ();
}

Edited 6 Years Ago by makan007: n/a

You have to use the read() and write() methods for binary files so getline will defnitely not work (it doesn't work for text output either).
See http://www.angelfire.com/country/aldev0/cpphowto/cpp_BinaryFileIO.html for some examples (towards the bottom of the page). For your purposes you could probably just write the members of the structure out rather than trying to do the whole thing (like in their last example).

Edited 6 Years Ago by jonsca: n/a

I have amended the code to line 110, 111 & 112. Is this correct? I can see something in the outfile.dat now. But I noted that it does not display correctly... not all records are shown when I performed a cout on screen.

[infile.txt]
U Magic_Johnson
O Australian David Robertson
O Indian Suresh_Raj
U Mary_Jane

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

const int MAX = 20;
const int MAX1 = 30;

struct UKStudent
{
	char name [MAX1];
	int noTest;
	float mark [MAX];
	int final;
	char grade [MAX1];
};

struct OverseasStudent
{
	char country [MAX1];
	char name [MAX1];
	int noTest;
	float mark [MAX];
	int final;
	char grade [MAX1];
};

union EveryStudent
{
	UKStudent       uks;
	OverseasStudent	oss;
};

struct Student
{
	char type;   
	EveryStudent st;
};


int arraytofile (fstream&, char [], Student []);
void process_bintext (fstream&, char [], Student [],int);

int main()
{

	fstream afile;
	
	Student stu [MAX];
	
	int size = arraytofile (afile,"infile.txt",stu);
	process_bintext (afile,"outfile.dat", stu, size);

}

//Read infile.txt
int arraytofile (fstream& afile, char filename [], Student stu [])
{
    afile.open (filename, ios::in);
	
	if(!afile.good ())
	{
		cout << "The input file does not exist. " << endl;
		return 1;
	}
	
	char n;
	int i = 0;
	
	while (afile >> n)
	{
		switch (n)
		{
			case 'U': stu [i].type = n; 
					  afile.getline (stu [i].st.uks.name,MAX);
					  break;
					
			case 'O': stu [i].type = n; 
					  afile.getline (stu [i].st.oss.country,MAX,' '); 
					  afile.getline (stu [i].st.oss.name,MAX);
		}
		
		++i;
	}
	   
	
	afile.close ();
	return i;
}

//Convert infile.txt as binary file
void process_bintext (fstream& afile, char filename [], Student stu[], int size)
{
	afile.open (filename, ios::out | ios::binary);
	
	if(!afile.good())
	{
		cout <<"Failed to write file. " << endl;
		return;
	}
	
	
	char n;

	
	for (int i = 0; i < size; i++)
	{ 	   
			stu [i].type = n;
			afile.write (stu [i].st.uks.name,MAX);     //Added 
 			afile.write (stu [i].st.oss.country,MAX);  //Added
			afile.write (stu [i].st.oss.name,MAX);     //Added
			
			afile.write (reinterpret_cast <const char *> (&stu), sizeof (stu));
	}	   	   
	
	afile.close ();
}

Edited 6 Years Ago by makan007: n/a

Yes. Except your last write statement where you try to write the whole thing may not be working the way you want it to. That is to say I'm not entirely sure that it will.
Remember to put in an if statement amongst your other write statements because you won't write out a country for the UK students. You may need to write the type of student into the file as well. Convert your char U/O to a char * for that. You'll be reading them in (or someone will) but I suspect as long as you can somehow tell what kind of student it is you should be ok.

Edited 6 Years Ago by jonsca: n/a

Yes. Except your last write statement where you try to write the whole thing may not be working the way you want it to. That is to say I'm not entirely sure that it will.
Remember to put in an if statement amongst your other write statements because you won't write out a country for the UK students. You may need to write the type of student into the file as well. Convert your char U/O to a char * for that. You'll be reading them in (or someone will) but I suspect as long as you can somehow tell what kind of student it is you should be ok.

Hmm... ok, let me try out although I am 50% understood.

Need further advise on the if else statement from line 111 onwards.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

const int MAX = 20;
const int MAX1 = 30;

struct UKStudent
{
	char name [MAX1];
	int noTest;
	float mark [MAX];
	int final;
	char grade [MAX1];
};

struct OverseasStudent
{
	char country [MAX1];
	char name [MAX1];
	int noTest;
	float mark [MAX];
	int final;
	char grade [MAX1];
};

union EveryStudent
{
	UKStudent       uks;
	OverseasStudent	oss;
};

struct Student
{
	char type;   
	EveryStudent st;
};


int arraytofile (fstream&, char [], Student []);
void process_bintext (fstream&, char [], Student [],int);

int main()
{

	fstream afile;
	
	Student stu [MAX];
	
	int size = arraytofile (afile,"infile.txt",stu);
	process_bintext (afile,"infile.dat", stu, size);

}

//Read infile.txt
int arraytofile (fstream& afile, char filename [], Student stu [])
{
    afile.open (filename, ios::in);
	
	if(!afile.good ())
	{
		cout << "The input file does not exist. " << endl;
		return 1;
	}
	
	char n;
	int i = 0;
	
	while (afile >> n)
	{
		switch (n)
		{
			case 'U': stu [i].type == n;
					  afile.getline (stu [i].st.uks.name,MAX);
					  break;
					
			case 'O': stu [i].type == n; 
					  afile.getline (stu [i].st.oss.country,MAX,' '); 
					  afile.getline (stu [i].st.oss.name,MAX);
					  break;
		}
		
		++i;
	}
	   
	
	afile.close ();
	return i;
}

//Convert infile.txt as binary file
void process_bintext (fstream& afile, char filename [], Student stu[], int size)
{
	afile.open (filename, ios::out | ios::binary);
	
	if(!afile.good())
	{
		cout <<"Failed to write file. " << endl;
		return;
	}
	
	
	char n;

	
	for (int i = 0; i < size; i++)
	{ 	   
			//Not quite sure how to do the if...else... as I got no idea how to define the type variable here, (stu [i].type == n)
			if (stu [i].type == 'U')
			{
				afile.write (stu [i].st.uks.name,MAX); 
 			
			}
			else
			{
			
 				afile.write (stu [i].st.oss.country,MAX); 
		    	afile.write (stu [i].st.oss.name,MAX); 
			
			}
			
	}	   	   
	
	afile.close ();
}

Edited 6 Years Ago by makan007: n/a

That seems to be right the way you have it. What problems is it causing?

I was also suggesting that you write out the type (accessible via pointer as &stu.type to pass into the write method) into the file so that when you are reading it back in, going along you can know whether to read 1 or 2 char [] for UK or non-UK students.

Because the outfile.dat displayed the following (which is like missing another two records I specified in infile.txt):
Magic_Johnson % & ' ( ) * + , - . S T U V W X Y Z { Australian David R P

Latest code.

//Convert infile.txt as binary file
void process_bintext (fstream& afile, char filename [], Student stu[], int size)
{
	afile.open (filename, ios::out | ios::binary);
	
	if(!afile.good())
	{
		cout <<"Failed to write file. " << endl;
		return;
	}
	
	
	char n;

	
	for (int i = 0; i < size; i++)
	{ 	   
			//Not quite sure how to do the if...else... as I got no idea how to define the type variable here, (stu [i].type == n)
			if (stu [i].type == n)
			{
			
				afile.write (stu [i].st.uks.name,MAX); 
 			
			}
			else
			{
			
 				afile.write (stu [i].st.oss.country,MAX); 
		    	afile.write (stu [i].st.oss.name,MAX); 
			
			}
				
			afile.write (reinterpret_cast <const char *> (&stu[i].type), sizeof (n));
	
	}	   	   
	
	afile.close ();
}

afile.write (reinterpret_cast <const char *> (&stu[i].type), sizeof (n)); Since stu.type is already a char you can simply do afile.write(&stu[i].type,1); Try this change and move it in front of the other write() statements. Failing that, I would double check your input results (loop through and print out the parts of the struct) just to be on the safe side.

That seems to be right the way you have it. What problems is it causing?

I was also suggesting that you write out the type (accessible via pointer as &stu.type to pass into the write method) into the file so that when you are reading it back in, going along you can know whether to read 1 or 2 char [] for UK or non-UK students.

afile.write (reinterpret_cast <const char *> (&stu[i].type), sizeof (n)); Since stu.type is already a char you can simply do afile.write(&stu[i].type,1); Try this change and move it in front of the other write() statements. Failing that, I would double check your input results (loop through and print out the parts of the struct) just to be on the safe side.

afile.write(&stu.type,1); // Why the '1'?


void process_bintext (fstream& afile, char filename [], Student stu[], int size)
{
	afile.open (filename, ios::out | ios::binary);
	
	if(!afile.good())
	{
		cout <<"Failed to write file. " << endl;
		return;
	}
	
	
	char n;

	
	for (int i = 0; i < size; i++)
	{ 	   
			
			if (stu [i].type == n)
			{
				
				afile.write (&stu[i].type,1);
				afile.write (stu [i].st.uks.name,MAX); 
				
 			
			}
			else
			{
				
				afile.write(&stu[i].type,1); 
 				afile.write (stu [i].st.oss.country,MAX); 
		    	afile.write (stu [i].st.oss.name,MAX); 
				
			}
				
			
	
	}	   	   
	
	afile.close ();
}

I noticed that the outfile.dat (Binary File)when open in notepad, they are not a bunch of black blocks.
Example,
outfile.dat
---------------------------------------------------------------------
Magic_Johnson % & ' ( ) * + , - . P S T U V W X Y Z { Australian David R

afile.write(&stu.type,1); // Why the '1'?

since we a writing a char (defined as being one byte) that's how big it is (so sizeof(stu.type) = 1), just like when you had to call is like afile.write(arr,MAX); when you had MAX number of chars in the array.

when open in notepad, they are not a bunch of black blocks.

I got something like this:

U Michael_Owen                 O          Australian David_Cooper       O          Japanese Arikato

The spacing comes from the extra room left over in the arrays. The fact that they remain as letters shouldn't be a surprise, as notepad's job is to interpret a file of characters and display it for the user to read. Try putting a struct of ints or doubles into your code and writing that out after your actual structs. I did that and got a few "black blocks."

Edited 6 Years Ago by jonsca: n/a

Notepad does not know how to read binary files, so any attempt to make Notepad read binary data will fail. The only way to read binary data is either (1) write your own program to read it then display the data, or (2) use a hex/ascii editor which will display each byte of the file in both hex and ascii.

I got something like this:

U Michael_Owen                 O          Australian David_Cooper       O          Japanese Arikato

The spacing comes from the extra room left over in the arrays. The fact that they remain as letters shouldn't be a surprise, as notepad's job is to interpret a file of characters and display it for the user to read. Try putting a struct of ints or doubles into your code and writing that out after your actual structs. I did that and got a few "black blocks."

The top display is for which file? I don't quite understand the word in bold.

Next, I am going write a function to open the binary file, outfile.dat
and upate the following data to a text file, updateoutfile.txt (See Code for content in the file & toggle to text)

1      UK       Michael_Owen1          0         0        0      0      0       Fail
2      Aust    Michael_Owen2          0         0        0      0      0       Fail
3      Indian  Suresh                     0         0        0      0     0        Fail

Now the U & O will be missing from column1 (replaced by some serialno) & those records in U will have the country written to column2. Column3 - Name, Column 4 - 8 are some test scores. Last column is the grade.

Can show me an example of writing the above data to updateoutfile.txt? Will I be using codes like afile.seekg, afile.seekp...etc?

Edited 6 Years Ago by makan007: n/a

The top display is for which file? I don't quite understand the word in bold.

That would be the equivalent to your outfile.dat.

When I said actual struct I meant (sorry for the mixup) the (name, country) data from your struct. I say actual becauase what I had done was made up another struct with an int and a double and written that to the file after I wrote out the name and country. I wanted to see if I could get the "blocks" in the output and I did because the binary representation of int and double doesn't translate readily into human readable ASCII when notepad tries to read the data.

Will I be using codes like afile.seekg, afile.seekp...etc?

I am not sure if you need them if you read everything back in and write it out in the proper order interspersing the new data with the old. To read it in, you need to use the read() method of your input stream (make a new input stream for binary) in a similar way to how you wrote it out (designating a destination for the input and length). Then use the << operator with a new output stream (don't designate it for binary).

Give this one a try without the examples (or look at the quick examples on http://www.cplusplus.com/reference/iostream/fstream/ . Come back with some code and any questions you have.

I am not sure if you need them if you read everything back in and write it out in the proper order interspersing the new data with the old. To read it in, you need to use the read() method of your input stream (make a new input stream for binary) in a similar way to how you wrote it out (designating a destination for the input and length). Then use the << operator with a new output stream (don't designate it for binary).

Give this one a try without the examples (or look at the quick examples on http://www.cplusplus.com/reference/iostream/fstream/ . Come back with some code and any questions you have.

I had tried but my program crash when execute. I believe I need
(ios::in | ios::out).

Attached codes:

int main ()
{
      process_text (afile, "outfile.dat", "updateoutfile.txt", stu, size);
}
//Process binary file to text file
void process_text (fstream& afile, char filename [], char filename1 [], Student stu[], int size)
{
	afile.open (filename, ios::in | ios::out);
	
	if(!afile.good ())
	{
		cout << "Failed to update." << endl;
		return;
	}
	
	int i;	  
	//int n;
	//Student st;
	
	//afile.seekg (n-1) * sizeof (Student), ios::beg);
	afile.read (&stu[i].type,1);
	afile.read (stu [i].st.uks.name,MAX); 
	afile.read (stu [i].st.oss.country,MAX); 
        afile.read (stu [i].st.oss.name,MAX); 
	
	for (int i = 0; i < size; i++)
	{ 	  
		
		//afile.write (&stu[i].type,1); 
		afile << i + 1 << endl;
		afile.write (stu [i].st.oss.country,MAX);
		//afile.write (stu [i].st.uks.name || stu [i].st.oss.name);
	      //afile.write (stu [i].st.uks.noTEST || stu [i].st.oss.noTEST);
		//afile.write (stu [i].st.uks.final);
		//afile.write (stu [i].st.uks.grade);
	}	 	 	 
			
	//afile.seekp (n-1) * sizeof (Student), ios::beg);	
			
	afile.close ();
	
}

Edited 6 Years Ago by makan007: n/a

afile.open (filename, ios::in | ios::out |ios::binary); //you probably don't need the ios::out, just make another fstream for your output with no binary. I would at least for the time being read your variables back into a fresh array of structs in case you need the other values later.

One thing you need to consider, you need to null terminate your character arrays (so stick a '\0' after the last character) so that they will be usable with the << operator for the output stream, otherwise you'll get lots of extra characters and you might run over the subsequent arrays in the process -- either that or you'll have to use the put/seekp/etc and put out the char array one char at a time.

Any reference to see? It seems complex.

afile.open (filename, ios::in | ios::out |ios::binary); //you probably don't need the ios::out, just make another fstream for your output with no binary. I would at least for the time being read your variables back into a fresh array of structs in case you need the other values later.

One thing you need to consider, you need to null terminate your character arrays (so stick a '\0' after the last character) so that they will be usable with the << operator for the output stream, otherwise you'll get lots of extra characters and you might run over the subsequent arrays in the process -- either that or you'll have to use the put/seekp/etc and put out the char array one char at a time.

Edited 6 Years Ago by makan007: n/a

This article has been dead for over six months. Start a new discussion instead.