File I/O help

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);

cout << "Reading " << length << " characters..." << endl;

}
if (choice>5) {
cerr << "Invalid Option" <<endl;
}

}
}
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.
whats a code tag?
is that like
// text stuff
No, it is "code" and "/code" (without the quotes), in square brackets. Put the first before code and the second after.
Last edited on
It's ok. I did it for you. :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#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);

cout << "Reading " << length << " characters..." << endl;

}
if (choice>5) {
cerr << "Invalid Option" <<endl;
}

}
}
Last edited on
Topic archived. No new replies allowed.