![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Aug 2005
Posts: 68
Rep Power: 4
![]() |
file reading anomaly
Hello,
I would like to know the cause of this anomaly in a recent code I made. The code opens a file, and reads the number of lines in it. It works with some files, and does not with others. The code snippet is the following : ...
#define NEXT_LINE 10
...
int main()
{
...
char c, str[256];
int lines=1;
ifstream fin;
cin >> str;
fin.open(str);
while (fin.good())
{
c=fin.get();
if (c==NEXT_LINE) lines++;
}
cout << "The number of lines in the file : " << lines;
...While this code works with a .txt and a .bak file I made for the program, it does not work with an .x and an .exe file. I don't know where I made the error, could you help me? Thank you, Leslie |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
.exe files and .x files are binary files. They are not organized into text lines with a newline character. As a matter of fact, they are not organized into lines, at all. Any binary value of 10 which happens to be part of the data will give you a "line" count.
Confine your line counting to files which have lines and count bytes (total length) for the others. For total length, check out 'stat' and friends. You don't even have to open the file. For line counts, why don't you just read lines (let the library function look for newlines) and be done with it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Aug 2005
Posts: 68
Rep Power: 4
![]() |
Wow thank you DaWei, you solved it. It was so obvious though...binary files
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| OnlineTextEditor.Com! | Sane | Show Off Your Open Source Projects | 43 | Jun 16th, 2006 8:55 AM |
| c++ reading from a file ... | programmingnoob | C++ | 8 | Apr 22nd, 2006 6:17 PM |
| reading in a file in java | ryanl | Java | 3 | Sep 8th, 2005 9:54 AM |
| After execution - Error cannot locate /Skin File? | wchar | Visual Basic | 1 | Mar 5th, 2005 9:04 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |