So as a learning exercise, I am trying to read and write a binary tree to a file. After googling around, I decided to use a preorder traversal (including null nodes) to write the binary tree to the file. I got stuck trying to read a tree from a file. I can't figure out how to create an unknown number of nodes when they are needed. I could use an array, but that just seems bulky - plus it could run out of space. Is that what I have to do? I've heard of vectors before, but not very much.
I am trying to count every node of a binary tree internally (not passing in the root), all my attemps have ended in memory access violation or an inaccurate count (4 or 5) when it should be 10.
This is what I have tried
And yes I am NOT suppose to pass in the root.
int CountChildren() { if ( !Left() && !Right()) return 1; if ( Left() ) return Left()->CountChildren() +1; if ( Right() ) return Right()->CountChildren() +1; }
I'm trying to count the number of node in a binary tree. I have a public count function that calls a private count function. I keep getting a linker error: Undefined symbols for architecture x86_64:
"BST<int>::nodesCount(BST_Node<int>*, int&)", referenced from: BST<int>::nodesCount() in main.o
This is in Xcode. It doesn't seem to like calling the private function in the public one, but I'm not sure why since I'm doing that with several other functions.
//public function template <typename T> int BST<T>::nodesCount() {
if i have 2 variables for which values are given by the user,then,does the information get stored into the file in the name of the variable,or just like packs of information.....if the former is true,how to extract the information of a particular variable alone from the whole file?
I am trying to get the code to read from the txt file one bite at a time and then write this bite into the binary file but i cant seem to get it working.
FILE *fpcust, *fpcustbin; //<<<<<-----point to both sales and customers text files, and the new .bin files for both char buffer; int ch; int ch1; fpcust = fopen("c:customers.txt", "r"); //<<<<-----pointing to the file fpcustbin = fopen("c:customers.bin", "wb"); //<<<<<-----pointing to the new binary file, opening in writing binary
The Problem You are part of a company writing a spreadsheet program. As you know, spreadsheets can be sorted on any column. You're part of the project is to write one binary tree function to sort the data [Hint: use different fields when inserting nodes in the tree.] and a second function to list it in either an ascending or descending sequence. [Note: Each of these functions may actually need to be a set of related functions.]
For sample data you will have a disk file containing information about Shakespeare's plays. Your first function should create a tree based on the sort selected by the user and the second function to display the data in the sequence selected by the user. Regardless of the column being sorted, data in individual records always be displayed in the same line of the output.
Input : Each record will contain the following information: First Performed 9 characters Printed 5 characters Title 26 characters Type 7 characters
Output : Tabular output should be aligned in columns with two spaces between each. All columns should have headings. It should be sorted on the column specified by the user.
Example (This sample data provided so you can test your program.) If the data is:
1595-96 1600 A Midsummer Night's Dream Comedy 1594-95 1623 Two Gentlemen of Verona Comedy 1596-97 1623 King John History 1597-98 1598 Henry IV, Part 1 History 1611-12 1623 The Tempest Comedy 1602-03 1623 All's Well That Ends Well Comedy
[Code]...
Source: [URL]...
Possible outputs are
1 - for a sort by title: First Performed Printed Title Type --------- ------- -------------------------- ------- 1595-96 1600 A Midsummer Night's Dream Comedy 1602-03 1623 All's Well That Ends Well Comedy 1606-07 1623 Antony and Cleopatra Tragedy 1599-1600 1623 As You Like It Comedy
[Code]....
2 - for a sort by first performed: First Performed Printed Title Type --------- ------- -------------------------- ------- 1590-91 1594? Henry VI, Part 2 History 1590-91 1594? Henry VI, Part 3 History 1591-92 1623 Henry VI, Part 1 History 1592-93 1623 Comedy of Errors Comedy 1592-93 1597 Richard III History
int main () { FILE * pFile; long lSize; char * buffer; size_t result; pFile = fopen ( "myfile.bin" , "rb" );
[Code] .....
How to open binary for read and write? Why the buffer is char * buffer? i mean in binary u cant read chars . How can it be? how the data is represented? just like txt file? What the buffer will contain how to print this buffer???
Following is the program I wrote it basically takes 9 inputs and then save them into binary file. then print out the data stored in binary data and find inverse of it then print the inverse out. but its stuck in a loop somewhere.
Code: #include <stdio.h> int main() { int a[3][3],i,j; float determinant=0; int x; FILE *fp = fopen ("file.bin", "wb");
I'm writing a class to store a tree I have a small issue.
I have a class which stores a node of the tree, something like this:
template<class T> class node { private: (stuff regarding parent node and child nodes); T __data; public: // more stuff (constructors and utility functions) here };
And I want to I want to be able to do the conversion T(node<T>) implicitly. For example, I want to be able to do this:
node<int> myNode; myNode=5; myNode+=10; myNode*=9; int x=int(myNode/4); cout << x+myNode;
And this:
class myClass { int x; int y; void print () {cout << x << ' ' << y << endl;}
How to store values from a .txt file delimited with semicolons (;) into a class which is then stored into a Binary Search Tree. After browsing Google for a few hours, and trying various examples of people using Vectors, I just can't seem to get my program to work using Object Oriented Programming with an instance of the class Person.
My two classes are Person, and BinarySearchTree as follows:
class Person{ private: string first_surname; string second_surname; string name; int ID;
[Code] ....
Ok so my text file saves the data of each person in the same order as the class with each value separated by a semicolon.
i.e. First_Surname;Second_Surname;Name;ID;Telephone;Score;
void fillTree( BinarySearchTree *b) { string input[7]; Person p; fstream file("scores.txt", ios::in); // reads text file if(file.is_open()) {
[Code] ....
I understand that I get an error because a vector is saved as integers, and I am using strings, my question is, any other way to read the .txt file and save each data separated by a semicolon, into the Person class?
Standard example. I have a large text file and I wish to lex it into words. I tell the program that all words are delimited by ' ' ';' ':' and ''.
When I run the program it seems to be outputting the occurances of the letters and not the words. Im gobsmacked, I dont know what the hell is going on. Heres the function that lexes letters and not words. I want words dammit words!!
First youll see I define root node and point it to null; This forms the base of the BST. Then keep munching one character at a time until EOF reached. If the character is not a delimiter, assign it to "word" string, character by character. If it is a delimiter, take the so-far-constructed "word" and chuck it in the BST, then clear the word string through .clear().
I have a std::vector of short ints that I need to write to a specific location in a binary file (without using .NET code). To that end, I wrote up this code:
Code: ofstream fileStream (filePathString, ios::out | ios::binary); int curPos = 2821; fileStream.seekp(curPos); int iter = 0; while (iter < 1024*1024){ char bytesToWrite[2]; //Low byte bytesToWrite[0] = LOBYTE(dataVector[iter]);
[Code]...
The code runs without crashing, but when I look at the file afterwards in a hex editor, every byte (even those outside the range I thought I was writing to) are replaced with 00. I suspect I'm missing something in my understanding of file streams. Did I write that code correctly? Seekp does move the pointer over the next byte to be overwritten, yes? Am I getting a memory leak somewhere?
except when the string exceed 11 characters. I guess it's because it has to pick a fixed sized for the string? but what if I want to always be able to have string up to 200 character? because now I can't exceed 11..I know writing a string with c_str() works, but I would like to write/read the structure in one shot.
The Objective Of This Program Is To Create A File To Write Text And Read Back The File Content. To Do That I Have Made Two Function writeFile() To Write And readFile() To Read.The readFile() function works just fine but writeFile() doesn't.
How writeFile() function Works? when writeFile() function Execute It Takes Characters User Type And When Hit Enter(ASC|| 10) It Ask "More?(Y/N)" That Means What User Want? Want To Go Next Line Or End Input?
If "Y" Than Inputs Are Taken From Next Line Else Input Ends.
But The Problem Is When Program Encounters ch==10 It Shows "More?(Y/N)" And Takes Input In cmd variable.If cmd=='Y' I Mean More From Next Line Than It Should Execute Scanf Again To Take ch I Mean User Input.But Its Not!!! Its Always Showing "More?(Y/N)" Again And Again Like A Loop.
Code: #include <stdio.h> void writeFile(void); void readFile(void); int main(){