xcode - Reading a file in C++ in one block, can't output the contents -
i wrote simple program read contents of text file in 1 block , save buffer display contents of buffer size of buffer, although compiled exact code in windows 7 working fine whenever compile on mac there no buffer output @ size of buffer returning -1, iam not getting error during compilation
myfile.txt content
hello world !!!
a b c d e f g h ... z
a b c d e f g h ... z
my code
#include <iostream> #include <fstream> #include <string> using namespace std; int main () { fstream myfile; myfile.open("mytext.txt"); myfile.seekg(0,ios::end); long int size = myfile.tellg(); myfile.clear(); myfile.seekg(0,ios::beg); char* buffer = new (nothrow) char[size]; myfile.read(buffer,size); myfile.close(); (int = 1; < size;i++ ) if ((buffer[i]>= 65 && buffer[i] <=90) || (buffer[i] >= 97 && buffer[i] <= 122) || buffer[i] == ' ' || buffer[i]== '\n') cout << buffer[i]; cout << size; delete [] buffer; getchar(); return 0;
i checked file reading operation via:
if(myfile.good() == false) cout << "error openning file \n"; if(myfile.bad() == true) cout << "read/writing error on i/o operation \n"; if (myfile.fail() == true) cout <<"logical error on i/o operation \n";
my output :
error openning file
logical error on i/o operation
-1
my guess not opening file: worked in windows because filename not case sensitive, while on mac is. (and anyway, saying file called myfile.txt , in program opening mytext.txt)
check file has been correctly opened.
Comments
Post a Comment