Reputation: 13
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct node
{
char data;
int count;
node * left;
node *right;
};
//to create a new node
node* create_node(char ch)
{
node *new_node=new node;
new_node->data=ch;
new_node->count=1;
new_node->left=NULL;
new_node->right=NULL;
return new_node;
}
//function to insert a data in new node
node * insert_data(node *root,char ch)
{
if (root==NULL) {
return create_node(ch);
}
if (ch==root->data) {
(root->count)++;
return root;
}
if (ch<root->data)
root->left=insert_data(root->left,ch);
else
root->right=insert_data(root->right,ch);
return root;
}
//preorder traversal of BST(binary search tree)
void display(node* temp)
{
if (temp==NULL)
return;
cout<<temp->data<<" ";
cout<<temp->count<<endl;
display(temp->left);
display(temp->right);
}
int main()
{
string file_name;char ch;
cout<<"enter file name\n";
cin>>file_name;
fstream file(file_name,ios::in);
node* root;
root=NULL;
//reading characters from a file
while (file>>ch&&file!='\0')
{
root=insert_data(root,ch);
}
file.close();
display(root);
return 0;
}
Bear with me as I'll be posting the error and project description.
Error Line 53: error: no matching function for call to `std::basic_fstream >::basic_fstream(std::string&, const std::_Ios_Openmode&)'
Project Description: Full Project Description:
In this assignment, you will be using a binary search tree to gather information about a text file. This should test the use of binary search trees, basic file handling and string/char manipulation. Background: In cryptography, one way to attempt to break a code is to calculate the amount of each single letter, the amount of combinations of letter pairs, the amount of 3-letter combinations and so on. That is, the amount of any particular subsequence of letters. For example, in the string “aadabcdaa”, this has the following subsequence frequency
a: 5
b: 1
c: 1
d: 2
aa: 2
ad: 1
da: 2
ab: 1
bc: 1
cd: 1
aad : 1
and so forth. Your program will open a file that count the occurrences up to consecutive letters ‘k’, so if the user enters k=4, you would store the number of all consecutive letter sequences up to 4.
Description: Your program should begin by prompting the user for a filename. Then, open that file and use it as necessary to do the following: Use a binary search tree that stores at each node both a string and a count of how many of that node it has found. Then, go through the file given (text file) and starting with the first letter, put it onto the tree with a count of 1. Then get the next character in the file, push it onto the tree, and so forth. If you ever try to add a node that has already been added (for example, pushing an ‘a’ onto the tree that already has an ‘a’, increment the count at that node. Once that is done, go through the file again and get all consecutive 2-letter occurrences and push them onto the tree. Again, if there is a match, increment the count. Repeat this entire process until you reach a ‘k’ long sequence. Note: You can of course combine all these steps also if you want such that it only requires one read of the file.
Output: Once your program is done, do an inorder traversal of the tree, outputting the data in the following format: a: 27
aa: 6
aaa: 3
etc. indicating that the letter a was found 27 times, the sequence ‘aa’ was found 6 times and so on. Additional complication: Since it is common in code to either use misleading spaces or leave spaces out altogether, your code should ignore any spaces in the message. Simply deal with alphabetical characters and their sequence. So for example, the sequence ‘a a’ would still count as consecutive ‘aa’. The file that you take in will contain only alphabetical characters and spaces. It might or might not have endline characters, but you should ignore them in either case as you do for spaces. I suggest you start this assignment by having it store the statistics for single-letter occurrences and then try it for multiletter sequences. That way you know the tree operations and traversals are working fine before you mess around with parsing the string. Error handling: Your program should handle all reasonable file errors. So if the file doesn’t exist or can’t be read, you should output an error message.
Upvotes: 1
Views: 251
Reputation: 154
Change
fstream file(file_name,ios::in);
To
fstream file(file_name.c_str(),ios::in);
You probably compile with C++ 98/03
According to cplusplus, with the C++98, the constructor of fstream
is
explicit fstream (const char* filename,
ios_base::openmode mode = ios_base::in | ios_base::out);
Upvotes: 1