I'm not really getting what you are trying to say, but I can help a bit
Text file:
If you want to read into an int,long,char*,char,etc. use file>>
If you want to read including spaces, use
file.getline(char* str,int size,char delim='\n');
If you want to read data but not store it, use
file.ignore(int size=1,char delim=EOF);
Binary file:
If you have an int n and you want to read directly into it, do
file.read((char*)&n,sizeof(int));
similarly this is how you write an int to a file
file.write((char*)&n,sizeof(int));
If you want to directly jump to a specific point in the file use seekg() and seekp()
Details of all these functions is available on this site on the reference section.
I advise you to use binary file approach as then you would know how many bytes each object occupies. For eg. (assuming int on you computer is 4 bytes), reserve 1st 4 bytes for rows, next 4 bytes for columns and store contents of matrix in blocks of 4 bytes.
However, if you want to store file as text, you don't need to format it
Instead of
<matrix>
rows = 2
cols = 2
1 2
2 4
</matrix>
|
Do
For eg. if you want to add 2 matrices, open 2 files in read and 1 in write
mode (all files in binary mode, if using binary files). Read the number of columns(c) and rows(r) of both files and check if they are equal. If they are, then read one int from 1st file, another int from another file, add them and store them in the other file