Trying to write a simple program that will read data but keep stumbling upon the fact that I kinda can't limit the amount that is read.
I have simple student.dat file which is filled ATM with nothing else but simple string "somethinginside", without quotes.
When I do this on each cout it outputs whole file plus several weird chars.
#include <iostream>
#include <fstream>
struct students
{
char name[7];
char lastname[15];
};
int main()
{
int input(0); //for pause later
students data;
std::fstream dat ("student.dat", std::ios::in | std::ios::binary); //opens file for read
dat.read ((char *) &data, sizeof(data)); //should read data
std::cout << "name " << data.name << std::endl; //outputs more than it should
std::cout << "lastname " << data.lastname << std::endl; //outputs more than it should
dat.close(); // closes
std::cin >> input; //pause to see output
return true;
}
If I try adding this one after reading
std::fstream dt ("something.dat",std::ios::out | std::ios::binary);
dt.write (data.name, 10);
dt.close();
it will output "somethingi", while I'm pretty sure, I hope, that I defined it's length in structure and when reading how much to read.
I get same even if I try reading it with
dat.read (data.name, sizeof(data.name));
I'd be grateful for any helpful advice :)