Searching in binary file C++ - c++

#include <iostream>
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class Student{
private:
char name[40];
char grade;
float marks;
public:
void getdata();
void display();
char* getname(){return name;}
void search(fstream,char*);
};
void Student::getdata(){
char ch;
cin.get(ch);
cout<<"Enter name : ";
cin.getline(name,40);
cout<<"Enter grade : ";
cin>>grade;
cout<<"Enter marks : ";
cin>>marks;
cout<<"\n";
}
void Student::display(){
cout<<"Name : "<<name<<"\t";
cout<<"Grade : "<<grade<<"\t";
cout<<"Marks : "<<marks<<"\t"<<"\n";
}
void search(fstream fin,char* nm)/*initializing argument 1 of 'void search(std::fstream, char*)'*/{
Student s;
fin.open("stu.txt",ios::in|ios::binary);
while(!fin){
fin.read((char*)&s,sizeof(s));
if(s.getname()==nm){
cout<<"Record found !";
s.display();
break;
}
}
fin.close();
}
int main(){
system("cls");
char nam[40];
Student arts[3];
fstream f;
f.open("stu.txt",ios::in|ios::out|ios::binary);
if(!f){
cerr<<"Cannot open file !";
return 1;
}
for(int i=0;i<3;i++){
arts[i].getdata();
f.write((char*)&arts[i],sizeof(arts[i]));
}
f.close();
cout<<"Enter name to be searched for : ";
cin.getline(nam,40);
char* p = new char[40];
p=nam;
search(f,p);/*synthesized method 'std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)' first required here*/
getch();
f.close();
return 0;
}
The above program first creates a file "stu.txt" and writes user-given input to the file.
It is then supposed to search for a record based on the name the user enters ( using the search() function ). I'm getting errors when calling search() and also defining search(). I've put in the errors the compiler throws as comment lines. Could anyone explain what's going wrong in there ?

fstream is not copyable, so you have to pass fstream as a reference, or in c++11, move it.
Given you access f after calling search, it is best to pass it by reference.
Change your function to accept the fstream as a reference:
void search(fstream& fin,char* nm)

Related

Why is the syntax for taking the input about name not working?

#include<iostream>
#include<string>
using namespace std;
class data{
string name;
string code;
public :
void getname()
{
cout<<"Enter name : ";
getline(cin,name);
}
void getcode()
{
cout<<"Enter code : ";
getline(cin,code);
}
void display()
{
cout<<"Name : "<<name;
cout<<"Code : "<<code;
}
};
int main()
{
int n,i=0; cin>>n;
data stud[n];
while(i<n)
{
stud[i].getname();
stud[i].getcode();
i++;
}
for(int i=0;i<n;i++)
stud[i].display();
return 0;
}
In Line 13: The getline() is not executed. Can anyone explain its cause and an alternative?
Initially, I thought that I may have missed a header file or the syntax is wrong, but that is not the case as the syntax for input of code works just fine.
When using cin and getline function alternatively, you should clear the cin input stream. by using cin.ignore() funtion.
Here I am using cin.ignore() after your cin statements because you want to ignore the "\n" left in the buffer after taking your int variable with cin.
#include<iostream>
#include<string>
using namespace std;
class data{
string name;
string code;
public :
void getname()
{
cout<<"Enter name : ";
getline(cin,name);
}
void getcode()
{
cout<<"Enter code : ";
getline(cin,code);
}
void display()
{
cout<<"Name : "<<name;
cout<<"Code : "<<code;
}
};
int main()
{
int n,i=0;
cin>>n;
cin.ignore();
data stud[n];
while(i<n)
{
stud[i].getname();
stud[i].getcode();
i++;
}
for(int i=0;i<n;i++)
stud[i].display();
return 0;
}
Hope This Might Helps: )
For starters there is a typo in this function
void getname()
{
cout<<"Enter name : ";
getline(cin,code);
^^^^
}
And this declaration of the array
int n,i=0; cin>>n;
data stud[n];
is invalid. The variable n is not initialized and variable length arrays is not a standard C++ geature.

My c++ program terminated without taking the input.What should i do?

The code below,works fine but it does not take any value for age and terminates.`
#include <iostream>
#include <string>
using namespace std;
class user{
int id,level=1,kills=0,age;
char name[20],server[40];
public:
void get(){
cout<<"Enter your name:";
cin>>name[20];
cout<<"Enter your age:";
cin>>age;
}
};
int main(){
user u;
u.get();
return 0;
}
/*Output
Enter your name:Jack
Enter your age:
C:\Users\user\documents\c++
*/
In the output section ,age is not accepted and the program terminates.
Use string name instead of char name[20] to take multi-character value. char name[20] will terminate after taking a single character.
Also, its valued will not be displayed on giving output.
Modified code for reference.
#include <iostream>
#include <string>
using namespace std;
class user{
int id,level=1,kills=0,age;
string name,server;
public:
void get(){
cout<<"Enter your name:";
cin>>name;
cout<<"Enter your age:";
cin>>age;
}
//test output
void put(){
cout<<name<<endl;
cout<<age<<endl;
}
};
int main(){
user u;
u.get();
//test
u.put();
return 0;
}
Just modify the code to this :
#include <iostream>
#include <string>
using namespace std;
class user{
int id,level=1,kills=0,age;
char name[20],server[40];
public:
void get(){
cout<<"Enter your name:";
cin>>name; // changes done here
cout<<"Enter your age:";
cin>>age;
}
};
int main(){
user u;
u.get();
return 0;
}
Job Done :)
Your problem is here:
cin>>name[20];
Why:
'name[20]' is 21th char of the array you defined before. It counts from 0! As this, it is simply a single char. If you now enter more than a single char, the rest is read by the cin>>age.
Example:
cout<<"Enter your name:";
cin>>name[20];
cout<<"Enter your age:";
cin>>age;
std::cout << "Name " << name << std::endl;
std::cout << "Age " << age << std::endl;
And entering:
Enter your name:1234
Enter your age:Name
Age 234
As you see, the '1' is now in the name and the rest is stored in age.
But attention: You defined your array as `name[20], which means you have 0..19 elements. Accessing name[20] is wrong!
But what you simply want to do was:
cin >> name;
The easiest way to handle strings (a long sequence of characters) or even the strings that have spaces just use the following library in C++.
#include <bits/stdc++.h>
Then just declare a string variable.
String name;
Now you can save a very long string without any error. e.g.
name = jshkad skshdur kslsjue djsdf2341;
and you'll get no error, enjoy ;)

error in gets(), not compile errors but unable to receive input

I encountered a problem where gets() is not working sometimes without any error compiling. In other words, gets() will not return any value but no warning or error explanation.
Here is the code where it's not returning value
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class student
{
private:
int admno;
char sname[20];
public:
void Takedata()
{
cout<<"Enter admission number ";
cin>> admno;
cout<<"Enter student name " ;
gets(sname);
}
void Showdata()
{
cout<<"Admission number "<<admno<<"\nStudent name "<<sname;
}
};
int main ()
{
student obj ;
obj.Takedata();
obj.Showdata();
getch();
return 0;
}
And in contrast here is the code where it's returning value to "sname"
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class student
{
private:
int admno;
char sname[20];
public:
void Takedata()
{
cout<<"Enter student name " ;
gets(sname);
}
void Showdata()
{
cout<<"\nStudent name "<<sname;
}
};
int main ()
{
student obj ;
obj.Takedata();
obj.Showdata();
getch();
return 0;
}
If anything is unclear don't hesitate to ask me! I'm glad to accept ant answer/solution/advise!
Use cin.ignore() before taking string input.I try this and it works fine.
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class student
{
private:
int admno;
char sname[20];
public:
void Takedata()
{
cout<<"Enter admission number ";
cin>> admno;
cin.ignore();
cout<<"Enter student name " ;
gets(sname);
}
void Showdata()
{
cout<<"\nAdmission number "<<admno<<"\nStudent name "<<sname;
}
};
int main ()
{
student obj ;
obj.Takedata();
obj.Showdata();
getch();
return 0;
}

C++ encrypt and decrypt secret messages using files

I have my prototypes in a header file, but I need some help. I am having some trouble getting the program to compile all the way through. It appears to be getting caught in a loop with the input. Possibly some issues with the functions. Thanks in advance for any input.
#include <iostream>
#include <conio.h>
#include "header.h"
#include <fstream>
class Caesar
{
public: void readText(char *input);
void encrypt(char *input,char *output,char *key);
void decrypt(char *input,char *output,char *key);
};
void main()
{
Caesar a;
char key[1000];
ifstream fin;
int choice;
char input[100],output[100];
cout<<"\n Enter input file: ";
cin>>input;
cout << input;
cout<<"\n Enter output file: ";
cin>>output;
cout <<output;
cout<<"\n Enter key: ";
cin>>key;
cout <<key;
cout<<"\n\n 1. Encrypt\n 2. Decrypt\n\n Select choice(1 or 2): "<< endl;
cin >> choice;
cout << choice;
a.readText(input);
if(choice==1)
{
a.encrypt(input,output,key);
}
if(choice==2)
{
a.decrypt(input,output,key);
}
else
{
cout<<"\n\n Unknown choice";
}
}
void Caesar::readText(char *input)
{
ifstream reader;
char buf;
reader.open(input);
cout<<"\n\n <--- "<<input<<" --->\n";
buf=reader.get();
while(!reader.eof())
{
cout<<buf;
buf=reader.get();
}
reader.close();
}
void Caesar::encrypt(char *input,char *output,char *key)
{
ifstream reader;
ofstream writer;
char buf;
reader.open(input);
writer.open(output);
buf=reader.get();
while(!reader.eof())
{
if(buf>='a'&&buf<='z')
{
buf-='a';
buf+=key[buf];
buf%=26;
buf+='A';
}
writer.put(buf);
buf=reader.get();
}
reader.close();
writer.close();
readText(input);
readText(output);
}
void Caesar::decrypt(char *input,char *output,char *key)
{
ifstream reader;
ofstream writer;
char buf;
reader.open(input);
writer.open(output);
buf=reader.get();
while(!reader.eof())
{
if(buf>='A'&&buf<='Z')
{
buf-='A';
buf+=26-key[buf];
buf%=26;
buf+='a';
}
writer.put(buf);
buf=reader.get();
}
reader.close();
writer.close();
readText(input);
readText(output);
}
if(choice=1)
should be
if(choice==1)
and also in the other if
In your case, you are assigning the value 1 to choice, then test if choice is true, and it is, since any non-zero numeral type is implicitly casted to bool true.
I have just executed your code and tried debugging it and took a screen shot
you program gets into a loop after entering the choice.there is no problem with cin>>.
From your comments, it seems like you're just trying to debug main. Everything seems to work fine. What are you inputting for key? If it's a very large integer, that may be your problem as it might exceed the maximum integer range and cause overflow.
Your key is an integer variable. You are inputting a string for the file name that holds your key, so that should be changed to a C string array. Change all of the passed key parameters to char* instead of int.
You have an infinite loop when the readText() function is called.
Maybe try this:
void Caesar::readText(char *input)
{
ifstream reader(input);
if(reader.is_open())
{
char buf;
cout<<"\n\n <--- "<<input<<" --->\n";
while(reader.get(buf))
{
cout << buf;
}
}
reader.close();
}
Make sure that your text file is in the same folder as your code. See this for more details: ifstream not opening file

reading/writing class objects to files in c++

#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
class Student{
private:
char name[40];
char grade;
float marks;
public:
void getdata();
void display();
};
void Student::getdata(){
char ch;
cin.get(ch);
cout<<"Enter name : ";
cin.getline(name,40);
cout<<"Enter grade : ";
cin>>grade;
cout<<"Enter marks : ";
cin>>marks;
cout<<"\n";
}
void Student::display(){
cout<<"Name : "<<name<<"\t";
cout<<"Grade : "<<grade<<"\t";
cout<<"Marks : "<<marks<<"\t"<<"\n";
}
int main(){
system("cls");
Student arts[3];
fstream f;
f.open("stu.txt",ios::in|ios::out|ios::binary);
if(!f){
cerr<<"Cannot open file !";
return 1;
}
for(int i=0;i<3;i++){
arts[i].getdata();
f.write((char*)&arts[i],sizeof(arts[i]));
}
f.seekg(0); //The question is about this statement;
cout<<"The contents of stu.txt are shown below : \n";
for(int j=0;j<3;j++){
f.read((char*)&arts[j],sizeof(arts[j]));
arts[j].display();
}
f.close();
return 0;
}
The above program reads and writes objects of Student from/to the file "stu.txt".
It runs fine. But it runs fine even if I switch off the fin.seekg(0) statement. I don't understand this part ? Are we not supposed to set the file pointer to the beginning of the file - before starting to read objects from the file(in the context of this particular program)?.
If you are at the end of the file, the read method call will fail, thus the Student structures are left untouched (so you are just printing again the same structures which you left untouched after writing them to file).