I am writing a c++ program to write data into a file in binary mode and read it from the file.
I am writing an object and reading to an object.
The problem I am facing is, when I write into the file and read it in that instance without closing the program, it works file. But after the program terminates execution, and comment out the writing code block and try to read the already written file, I get crappy output.
I am not able to understand what is going wrong.
Here is the code:
#include <fstream.h>
#include <string.h>
class Student{
protected:
char *Name, *Sub_code;
int Roll;
public:
Student(){}
};
class Details:private Student{
private:
char *Sub_Name;
int internal_marks, external_marks;
/*Methods*/
void setName();
void setRoll();
void setSubCode();
void setSubName();
void setInternalMarks();
void setExternalMarks();
public:
Details(){
Name = new char[1];
Sub_code = new char[1];
Sub_Name = new char[1];
Name[0] = '\0';
Sub_code[0] = '\0';
Sub_Name[0] = '\0';
internal_marks = 0;
external_marks = 0;
Roll = 0;
}
void setDetails();
void getDetails();
static void writeDetails(Details detail);
static void readDetails();
};
void Details::setName(){
cout<<"Enter Student Name : ";
char tmp[100];
tmp[0] = '\0';
cin>>tmp;
int len = strlen(tmp);
Name = new char[len];
strcpy(Name,tmp);
}
void Details::setRoll(){
cout<<"Enter Roll Number : ";
cin>>Roll;
}
void Details::setSubCode(){
cout<<"Enter Subject Code : ";
char tmp[100];
tmp[0] = '\0';
cin>>tmp;
int len = strlen(tmp);
Sub_code = new char[len];
strcpy(Sub_code,tmp);
}
void Details::setSubName(){
cout<<"Enter Subject Name : ";
char tmp[100];
tmp[0] = '\0';
cin>>tmp;
int len = strlen(tmp);
Sub_Name = new char[len];
strcpy(Sub_Name,tmp);
}
void Details::setInternalMarks(){
cout<<"Enter internal marks : ";
cin>>internal_marks;
}
void Details::setExternalMarks(){
cout<<"Enter external marks : ";
cin>>external_marks;
}
void Details::setDetails(){
setName();
setRoll();
setSubCode();
setSubName();
setInternalMarks();
setExternalMarks();
}
void Details::getDetails(){
cout<<Name<<"\t\t";
cout<<Roll<<"\t\t";
cout<<Sub_code<<"\t\t";
cout<<Sub_Name<<"\t";
cout<<internal_marks<<"\t";
cout<<external_marks<<"\t\n";
}
void Details::writeDetails(Details detail){
ofstream os("StudentsRecord.dat", ios::binary|ios::ate);
os.write(reinterpret_cast <char *>(&detail),sizeof(detail));
os.close();
}
void Details::readDetails(){
Details detail;
ifstream is("StudentsRecord.dat", ios::binary|ios::in|ios::beg);
cout<<"Name\tRoll\tSubject Code\tSubject Name\tInternal marks\tExternal Marks\n";
while (is.read(reinterpret_cast<char *>(&detail), sizeof(detail))){
detail.getDetails();
}
is.close();
}
int main(){
Details y,x;
/*for(int i = 0; i < 2; i++){
x.setDetails();
Details::writeDetails(x);
}*/
Details::readDetails();
return 0;
}
The commented code in the main() is the block that is used for writing the data in the file.
Here is the sample screen shot of the output I am getting.
Regards Priyabrata
The Detail class has char* inside. When you write it to the file you write the actual pointer address of the string, not the actual string. When you read it back in the same program it works because the data is still there.
When you rerun your program you get garbage because that pointer is not at some random piece of memory.
You should use a library that does serialization for you. It's kinda hard to get it right.
Take a look at https://code.google.com/p/protobuf/ , but I'm sure there are other ones as well.
Related
I want to do a program where I key in the a question and answer to a text file with unique id(auto increment number). Currently the program only works if the text file is empty, once I have written something in the text file, the next time I run it, it does not show any output. Just blank CMD
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class stud
{
public:
string answers, questions;
float score, totalScore;
int rollno;
stud()
{
rollno=0;
}
int get_no();
void getdata();
};
int stud::get_no()
{
ifstream outfile("questions.txt",ios::in);
int quizID;
while(!outfile.eof())
{
outfile>>rollno;
outfile>>questions;
outfile>>answers;
if(outfile.eof())
{
break;
}
}
quizID = rollno;
quizID = quizID + 1;
outfile.close();
return quizID;
}
void stud::getdata()
{
ofstream outfile("questions.txt", ios::out|ios::app);
rollno = get_no();
cout<<"Add New Question\n";
getline(cin,questions);
cout<<"Add New Answers\n";
getline(cin,answers);
outfile<<"ID "<<rollno<<": Question : "<<questions<<" Answer : "<<answers<<"\n";
outfile.close();
}
int main()
{
stud s1;
s1.getdata();
return 0;
}
I'm trying to read from the same file from another function but it doesn't work.
I guess the problem is that I'm trying to read from ifstream &input but I don't know the other way to implement that
#include <iostream>
#include <fstream>
using namespace std;
class Student{
public:
char name[40]; // i cant use string
int age;
void Input(ifstream &input)
{
input.getline(name, 40);
input >> age;
}
};
void Read(Student *students, int &numberOfStudents)
{
ifstream input("test.txt");
input >> numberOfStudents;
students = new Student[numberOfStudents];
for (int i = 0; i < numberOfStudents; ++i)
students[i].Input(input);
input.close();
}
int main()
{
int size = 0;
Student *students = NULL;
Read(students, size);
for (int i = 0; i < size; ++i)
cout << students[i].name << endl << students[i].age << endl;
return 0;
}
I made my input file
3
1
2
3
4
5
6
(if the program was working correctly i should've get 1 - name age - 2 etc)
but what i got is no names with ages = 1 2 3 respectively
The program does not work because you define:
void Read(Student *students, int &numberOfStudents)
And then
int size = 0;
Student *students = NULL;
Read(students, size);
The students pointer is passed-by value so Read() can not change the memory address outside the function.
To fix this simply pass the the pointer by-reference:
void Read(Student *& students, int &numberOfStudents)
Lastly as I commented you need to account white space e.g. '\n' line ending
In the file when using >> operator to extract data:
void Input(ifstream &input)
{
input.getline(name, 40);
input >> age;
input.ignore(1);
}
Same for reading the number of students in the file.
In binary file, there are some information which is student id, name and GPA for three students. I want to read them. id is integer type and GPA is float type. name is string type and the size is not fixed because the size of name is different each other. I want to get id, name and GPA for each student at function of student class.
probably, I think I have a problem at loading name.
I want to know how to fix it.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student {
private:
int id;
string name;
float GPA;
string line;
public:
Student() {};
Student(string line_) :line(line_) { };
int get_id();
string get_name();
float get_GPA();
void save_bi(ofstream &of);
void load_bi(ifstream &inf);
void save_txt(ofstream &of);
void print();
};
void Student::load_bi(ifstream &inf)
{
inf.read((char*)&id, sizeof(id));
inf.read((char*)&name, sizeof(name));
inf.read((char*)&GPA, sizeof(GPA));
//a.push_back(name);
}
int main()
{
ifstream inf;
inf.open("student_data.bin", ios::in | ios::binary);
if (!inf.is_open())
cout << "file open failure." << endl;
Student A;
A.load_bi(inf);
A.print();
Student B;
B.load_bi(inf);
B.print();
Student C;
C.load_bi(inf);
C.print();
inf.close();
return 0;
}
it is error message.
inline void _Container_base12::_Orphan_all() noexcept
{ // orphan all iterators
#if _ITERATOR_DEBUG_LEVEL == 2
if (_Myproxy != nullptr)
{ // proxy allocated, drain it
_Lockit _Lock(_LOCK_DEBUG);
for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
*_Pnext != nullptr; *_Pnext = (*_Pnext)->_Mynextiter)
(*_Pnext)->_Myproxy = nullptr;
_Myproxy->_Myfirstiter = nullptr;
}
#endif /* _ITERATOR_DEBUG_LEVEL == 2 */
}
Exception thrown: read access violation.
_Pnext was 0x13AED64.
A simple example of serializing your structure:
class Student
{
int id;
string name;
float GPA;
string line;
public:
void binary_write(std::ostream& out) const;
void binary_read(std::istream& input);
};
void Student::binary_write(std::ostream& out) const
{
out.write(&id, sizeof(id));
std::string::size_type length(name.length());
out.write(&length, sizeof(length));
out.write(name.data(), length);
out.write(&GPA, sizeof(GPA));
length = line.length();
out.write(line.data(), length);
}
void Student::binary_read(std::istream& input)
{
input.read(&id, sizeof(id));
std::string::size_type length;
input.read(&length, sizeof(length));
char * temp = new char[length];
input.read(temp, length);
name = std::string(temp);
input.read(&GPA, sizeof(GPA));
delete[] temp;
input.read(&length, sizeof(length));
temp = new char[length];
input.read(temp, length);
line = std::string(temp);
delete [] temp;
}
The above code uses the technique of writing the length of the string, followed by the string contents. The reading consists of using a temporary buffer, then reads the text into the buffer and creates a std::string from the buffer.
There are other methods to read in a string (search your C++ reference for "c++ string input iterator), but this illustrates the point.
The problem with writing pointers, and std::string contains pointers, is that there is no guarantee that the operating system will load your program into the exact same place in memory at every invocation.
The binary_write function is declared as const since it doesn't change any of the Student data members. This technique is called "const correctness". Search the internet for its definition.
The default accessibility for class is private, so I removed the private from the section declaring the variables.
I do not understand why my array of pointers is only saving the last line from the file that I am reading from. When I substitute a string literal into the setData() function the code works just fine. All that the "mann" file contains are a bunch of words order alphabetically. Thank you.
#include <iostream>
#include <fstream>
using namespace std;
class orignialData {
char* data;
public:
void setData(char* s) { data = s;}
char* getData() const {return data;}
};
class dataClass {
orignialData** W_;
public:
dataClass(char* filename);
void addData();
void viewAll();
};
dataClass::dataClass(char* filename) {
fstream file;
file.open(filename, ios::in);
if (file.fail()) {
cout << "There was an error reading the file...\n";
}
W_ = 0;
W_ = new orignialData*[5];
for (int i = 0; i < 5; i++)
W_[i] = new orignialData;
char buff[30];
char* temp;
while(file >> buff) {
cout << buff << endl;
static int i = 0;
W_[i] -> setData(buff);
i++;
}
file.close();
}
Instead of data = s, write data = strdup(s) to make a copy of the contents. Otherwise, you will assign the same pointer again and again, and you will overwrite the contents of the memory to which this pointer points again and again. At the end, your temporary buffer will contain the last line of your file, and all the pointers will point to exactly this buffer. That's what you are observing...
I am trying to read in two words from text file into one char array element. I CANNOT USE std::string. I get a seg fault because my loop is looping in a way that goes out of bounds. I cannot copy in the two words. please help me do this!! My loop for card struct works perfectly. I cannot get the "first last" into the people[].name
// deck of cards
// below are initializations
#include
#include
#include
#include
#include
using namespace std;
//globals
const int maxCards = 52;
//Structs
struct card {
char suit[8];
char rank[6];
int cvalue;
char location;
};
struct player {
char name[100];
int total;
card hand[4];
};
//program
int main()
{
//constants
char tempfName[100];
char templName[100];
//create struct array(s)
card deck[52];
card shuffledDeck[52];
player people[4];
//create pointers
card * deckPointer =NULL;
deckPointer = deck;
card * shuffledDeckPointer=NULL;
shuffledDeckPointer = shuffledDeck;
for(int i=0;i<4;i++)
{
strcopy(people[i].name,"first last");
}
//open player names file
ifstream fin2;
string fin2Name;
//get file name from user
cout << "Enter player file name...(Players.txt)" << endl;
getline(cin, fin2Name);
//open file
fin2.open(fin2Name.c_str());
//check if Players.txt opens correctly
if(!fin2.good())
{
cout << "Error with player file!" << endl;
return 0;
}
else
{
int j =0;
fin2 >> people[j].name; //prime file
while(fin2.good())
{
//find the length
int index =0, length=0;
while(tempfName[length] != '\0')
{
length++;
}
//now add space after first name
tempfName[length] = ' ';
length++;
while(templName[index] != '\0')
{
tempfName[length] = templName[index];
length++;
index++;
}
tempfName[length]='\0';
int counter =0;
while(templName[counter] != '\0')
{
people[0].name[counter] = templName[counter];
counter++;
}
}
}
it seems your tempfName is not pointing to a correct object in your while loop in else statement
else
{
int j =0;
fin2 >> people[j].name; //prime file
while(fin2.good())
{
//find the length
int index =0, length=0;
while(tempfName[length] != '\0')
{
length++;
}
You could do some cheating:
char full_name[256]; // Binary quantities are better.
snprintf(full_name, sizeof(full_name),
"%s %s",
first_name, last_name);
You should do more research regarding C-style string functions, like check a good reference book. It should have a chapter about the C-style string functions.
Here are some helpful ones:
strcpy, strcat, strchr, sprintf, strlen