I'm sure this is a simple issue, but I am having trouble solving it:
I receive this from the compiler:
cdcheck.cpp|31|error: 'slectionsIn' was not declared in this scope
the offending blocks of code:
in main:
...snip
ifstream selectionsIn (argv[2]);
PlayListItem item;
int itemCount = 0;
while (slectionsIn >> item)
{
...snip
in PlayListItem.cpp:
std::istream& operator>> (std::istream& in, PlayListItem& pl){
I can post the contents of these files if needed. It should be noted that this compiles correctly if I change from istream to ifstream in the declaration, but I can not modify PlayListItem.cpp, only main.
Try fixing the misspelled identifier that was mentioned in the compiler error message:
while (selectionsIn >> item) // not: slectionsIn
Compare
ifstream selectionsIn(argv[2]);
to
while (slectionsIn >> item)
Related
I've been trying to write the below object into a file and got lot of trouble since strings are dynamically allocated.
class Student{
string name, email, telephoneNo;
int addmissionNo;
vector<string> issued_books;
public:
// There are some methods to initialize name, email, etc...
};
So I got to know that I can't just write into a file or read from a file an object with serialization. So I searched all over the internet about serialization with cpp and got to know about Boost library.But I wanted to do it my own (I know writing a library that already exist is not good, but I wanna see what's going on inside the code). So I got to know about overloading iostream << and >>. And I also know that serialize/deserialize into/from text.
But I want to serialize into a binary file. So I tried overloading ostream write and istream read. But then I got size issues(as write and read needs the sizeof the object it writes/reads).Then I also got to know about stringstream can help to serialize/deserialize objects into/from binary. But I don't how to do that?
So my real question is How to serialize and deserialize an object into/from binary files without third party libraries?
I have found a solution serialize and deserialize an object into/from a file. Here is an explaination
As I told you this is my class. And I have added two functions which overload the iostream's write and read.
class Student{
string name, email, telephoneNo;
int addmissionNo;
vector<string> issuedBooks;
public:
void create(); // initialize the private members
void show(); // showing details
// and some other functions as well...
// here I'm overloading the iostream's write and read
friend ostream& write(ostream& out, Student& obj);
friend istream& read(istream& in, Student& obj);
};
But I have also told you that I have tried this already. The problem I have was how to read without object member's size. So I made changes as below (Please read comments also).
// write: overload the standard library write function and return an ostream
// #param out: an ostream
// #param obj: a Student object
ostream& write(ostream& out, Student& obj){
// writing the objet's members one by one.
out.write(obj.name.c_str(), obj.name.length() + 1); // +1 for the terminating '\0'
out.write(obj.email.c_str(), obj.email.length() + 1);
out.write(obj.telephoneNo.c_str(), obj.telephoneNo.length() + 1);
out.write((char*)&obj.addmissionNo, sizeof(obj.addmissionNo)); // int are just cast into a char* and write into the object's member
// writing the vector of issued books
for (string& book: obj.issuedBooks){
out.write(book.c_str(), book.length() + 1);
}
return out;
}
// read: overload the standard library read function and return an istream
// #param in: an istream
// #param obj: a Student object
istream& read(istream& in, Student& obj){
// getline is used rather than read
// since getline reads a whole line and can be give a delim character
getline(in, obj.name, '\0'); // delimiting character is '\0'
getline(in, obj.email, '\0');
getline(in, obj.telephoneNo, '\0');
in.read((char*)&obj.addmissionNo, sizeof(int));
for (string& book: obj.issuedBooks){
getline(in, book, '\0');
}
return in;
}
As you can see I have wrote length+1 for the terminating '\0'. It is usefull in read function as we have used getline instead of read. So getline reads until the '\0'. So no need of a size. And here I'm writing and reading into/from a file.
void writeStudent(Student s, ofstream& f){
char ch; // flag for the loop
do{
s.create(); // making a student
f.open("students", ios::app | ios::binary); // the file to be written
write(f, s); // the overloaded function
f.close();
cout << "Do you want to add another record? (y/n): ";
cin >> ch;
cin.ignore();
} while(toupper(ch) == 'Y'); // loop until user stop adding records.
}
void readStudent(Student s, ifstream& f){
char ch; // flag for the loop
do{
f.open("students", ios::in | ios::binary);
cout << "Enter the account no of the student: ";
int no;
cin >> no;
int found = 0;
while (read(f, s)){
if (s.retAddmissionNo() == no){
found = 1;
s.show();
}
}
if (!found)
cout << "Account Not found!\n";
f.close();
cout << "Do you want another record? (y/n): ";
cin >> ch;
} while(toupper(ch) == 'Y');
}
That's how I solved my problem. If something wrong here please comment. Thank you!
I am trying to read the txt file into the vector in function readIn. I cannot for the life of me get it to work. Either I get the following message "error: expected primary-expression before '&' token
readIn(ifstream& infile, vec);" or the function isn't called at all.
int main() {
const int MAXSIZE = 100;
vector<int> vec (MAXSIZE);
ifstream infile;
infile.open("in9.3.txt");
readIn(ifstream& infile, vec);
return(0);
}
void readIn(ifstream& infile, vector<int> &vec) {
int a, count;
count = 0;
while (!infile.eof()) {
infile >> a;
vec.at(count) = a;
count++;
}
infile.close();
vec.resize(count);
}
You must not specify the type of the parameter when passing to a function. What you have written is incorrect:
readIn(ifstream& infile, vec); // error
Note that you are trying to pass the variable infile which is defined in main. The compiler is complaining about the fact that you prefixed this with ifstream&. The correct call is:
readIn(infile, vec);
Also beware that since the function is defined after main, there must be a function declaration somewhere before main. It is not clear whether you did this or not, since you have not shown a complete program. In any case, you can either move the whole definition before main, or just add this line:
void readIn(ifstream&, vector<int>&);
as the title suggests, I am having a problem with not being able to read from an input file after passing the ifstream object to a class function. Basically I'm trying to sort a list of numbers using a heap ADT implemented with an array.
int main() {
ifstream infile("input.txt");
HeapSort* heap = new HeapSort(20); // creates a heap (array) with size 20
heap->buildHeap(&infile);
return 0;
}
void HeapSort::buildHeap(ifstream* infile) {
int data;
while (infile >> data) {cout << data << endl;}
infile->close();
}
the error occurs in the conditional of the while loop inside buildHeap. The compiler can't recognize the operator ">>" between an 'int' and an 'ifstream' object. However, strangely enough, if I write that same while loop inside main(), it'll work just fine. Also of note is that if I remove the while loop, the compiler returns no errors. Meaning, simply the act of passing the ifstream object from main to buildHeap is OK.
Please avoid suggesting alternative ways of achieving this. I was asked to not use any special fstream functions like eof(). I can only use the ">>" operator to read from the desired file.
You're passing a pointer to a stream, so you need to dereference it:
while (*infile >> data)
If you want your code to look like what you say you did in main, then you pass a reference:
heap->buildHeap(infile);
//...
void HeapSort::buildHeap(ifstream& infile)
{
int data;
while (infile >> data) { ... }
infile.close();
}
How these two function differs? Is there any different kinds of istream other than std::cin and what is the point of returning the istream?
using namespace std;
istream& readInput(istream& in, vector<string>& wordList)
{
string word;
while (in >> word)
{
wordList.push_back(word);
}
in.clear();
return in;
}
void readInput(vector<string>& wordList)
{
string word;
while (cin >> word)
{
wordList.push_back(word);
}
cin.clear();
}
cin is absolutely not the only one istream, I think that it is not even the most used one. Read about e.g. ifstream and istringstream.
Returning istream& from the function may be useful in various situations - it depends on the context. For just simple calls of readInput one can skip it and make the function void.
I am geting an an error in the following code it worked fine in visual studio but once i have moved it over to Xcode that uses gcc to compile get this error No matching constructor for initialisation of 'ifstream' i have looked at adding this as a reference rather than a copy as suggested on this site but it still came up with the error.
void getAndSetTextData::GetBannedList(string fileName)
{
bannedWordCount = 0;
ifstream inFile(fileName);
while(inFile >> currentWord)
{
bannedWords.push_back(currentWord);
bannedWords[bannedWordCount++] = currentWord;
}
inFile.close();
}
Any help would be appreciated.
ifstream constructor accepts a const char* as the filename (prior C++11):
ifstream inFile(fileName.c_str());
An additional constructor that accepts a const std::string& as the filename was added in C++11.
Minor point: consider changing argument string fileName to const string& fileName to avoid unnecessary copy of fileName.
first you should check that weather the file is opened or not. for example if you dont have permission to access the file or if you are opening a file in write mode when there is no enough disk space, etc...
so
ifstream inFile(fileName);
if( ! inFile )
return;
while(inFile >> currentWord)
and about your question, are you including the fstream?