Ehem, that makes sense, thanks CornedBee!
Ehem, that makes sense, thanks CornedBee!
If i use EOF as the delimiting character for istream::getline , will it read the whole file at once?
getline(TheFile,str,EOF)
If yes , will it still swallow the '\n' character?
Or anyway is it possible to do what i want with istream::getline (namely to detect the end of line in some way )
or shall i look for another method?
Cheers
EOF isn't necessarily a valid char value. You can't pass it to getline().
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Ok i understand.
So maybe i have to go about it something like that:
Code:while (!eof()) {getline(TheFile,str,)}
That's simply a syntax error.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Or read() with a fixed buffer. It's considerably more efficient than scanning the stuff you read for newlines.
Another thing: see the FAQ for eof(). Never loop until eof() is true. If there's an I/O error, it won't ever be.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Thanks Cornedbee, you gave me the final solution!
I let alone getline() and i rewrite the whole program with read()
Code:int main () { int length; char * str; cout << "Enter file name : "; string FileName; cin >> FileName; ifstream TheFile(FileName.c_str()); ofstream TheCopy; TheCopy.open ("Your_Uppercased_Copy.srt",ios::app); // get length of file: TheFile.seekg (0, ios::end); length = TheFile.tellg(); TheFile.seekg (0, ios::beg); // allocate memory: str = new char [length]; // read data as a block: TheFile.read (str,length); TheFile.close(); str[0] = static_cast<char>( toupper(str[0])); for(int i = 0; str[i]; i++) if (str[i] == '.'|| str[i] == '!'|| str[i] == '?') str[i+1] = static_cast<char>( toupper(str[i+1])); for(int j = 0; str[j]; j++) if (str[j] == '.'|| str[j] == '!'|| str[j] == '?') if (str[j+1] == '\n') str[j+2] = static_cast<char>( toupper(str[j+2])); for(int k = 0; str[k]; k++) if (str[k] == '.'|| str[k] == '!'|| str[k] == '?') if (str[k+1] == ' ') str[k+2] = static_cast<char>( toupper(str[k+2])); TheCopy.write (str,length); TheCopy.close(); delete[] str; return 0; }
You shouldn't use seekg() and tellg() with non-binary streams. You could get corrupted data on Windows.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Ok thanks, but if i add "ios::binary" it doesnt work.
ifstream TheFile(FileName.c_str(), ios::binary)
I know. The two simply don't mix. There's no fast way to determine how many characters there are in a file. Best thing seekg and tellg can do for you is give you a guideline for allocating, but you still need to be prepared for 1) getting less than that and b) getting more than that.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law