Created
March 11, 2019 09:50
-
-
Save ecomaikgolf/87ed9aeca090efb400b1e4b6b262b749 to your computer and use it in GitHub Desktop.
Ejercicio 6 de ficheros binarios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <cstring> | |
using namespace std; | |
struct Student{ | |
char id[20]; | |
char surname[40]; | |
char name[20]; | |
int group; | |
}; | |
int main(){ | |
ifstream text("students.txt"); | |
ofstream data("students.dat", ios::binary); | |
if(!text.is_open() || !data.is_open()) return -1; | |
string lec; | |
Student student; | |
while(getline(text,lec) && !data.fail() && !text.eof()){ | |
//We read all the data and put it on a struct | |
strncpy(student.id, lec.c_str(), 19); | |
student.id[19] = '\0'; | |
getline(text,lec); | |
strncpy(student.surname, lec.c_str(), 39); | |
student.surname[39] = '\0'; | |
getline(text,lec); | |
strncpy(student.name, lec.c_str(), 19); | |
student.name[19] = '\0'; | |
getline(text,lec); | |
student.group = stoi(lec); | |
//We write the data | |
data.write((const char*)&student, sizeof(student)); | |
} | |
text.close(); | |
data.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment