if my text document starts with (example) 4 letters, I write 5 letters to the file it will only display the first 4. basically whatever number of chars I start with is the only amount it will print
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define FILENAME "S:\\Unit_5_text_document.txt"
int x;
int choice=0;
string comment;
int main(int argc, char* argv[]) {
fstream myFile(FILENAME, ios::in | ios::binary);
//Check #of characters
myFile.seekg(0, myFile.end);
int length = myFile.tellg();
myFile.seekg(0, myFile.beg);
char *Words = new char [length];
while (choice!=5) {
//options
cout << "what would you like to do?" << endl;
cout
<< "1.Read - 2.Write - 3.Size of file - 4.Number of characters in the file - 5.End"
<< endl;
cin>> choice;
if (choice==1) {
//reads the file
myFile.read(Words, length);
for (int i =0; i < length; i++) {
cout << Words[i];
}
cout<< endl;
}
if (choice==2) {
ofstream myFile(FILENAME, ios::out | ios::binary);
cout << "what would you like to enter into the file?" << endl;
cin >> Words;
myFile.write(Words, sizeof(Words));
}
if (choice==3) {
streampos begin, end;
ifstream myfile(FILENAME, ios::binary);
begin = myfile.tellg();
myfile.seekg(0, ios::end);
end = myfile.tellg();
//myfile.close();
cout << "size is: " << (end-begin) << " bytes.\n";
}
if (choice==4) {
myFile.seekg(0, myFile.end);
int length = myFile.tellg();
myFile.seekg(0, myFile.beg);
Please use code tags, it makes your code easier to read.
In line 4 of main you set the length int to whatever the initial size of the file is. After that line, the length is never changed. In your choice == 4 block you try to change it, but you declare a new length variable local to the scope of the if block, which then goes away when the block is done executing, so your initial length from line 4 of main stays the same.