Draznar

I am once more working to convert some older code from VC++ 6.0. I have run into a bug problem with ifstream. Below is a small sample project that I am trying to get to work as a "proof of concept" of sorts. It replicates my problem on a smaller scale. Here we go.

Code Snippet

#include "stdafx.h"

#include <iostream>

using namespace std;

#include <fstream>

char *filename = "PL_TABLE.DAT";

int _tmain(int argc, _TCHAR* argv[])

{

char comments[150];

ifstream datafile;

datafile.open(filename);

if (!datafile.good())

cout << "File Bad!";

// Read header

//Class.ReadHeader(datafile);

int num_entries;

datafile.getline(comments,150,':') >> num_entries;

datafile.get();

datafile.getline(comments,150); // read second comment line

for (int i=0; i<num_entries; i++)

{

int index;

datafile.getline(comments,30);

datafile >> index;

if (index < 0 || index >=250)

cout << "Error!";

else

cout << "No Error!";

}

}

And here is the format of the "PL_TABLE.DAT". (The spacing is a bit off from what it looks like after I pasted it in here.)

There are more listings than just these two but they represent the setup.

number of players in file : 232

Name # T C D V R I T TC LC Z M B BC S A Tu Se D J WR L W H

AH-1W 2 1 5 739 5 6 4 3 15 2 2 0 14 0 15 6 8 24 3 1 10000 13.87 3.28 4.44

AV-8B 3 2 6 662 5 2 80 2 14 1 1 0 16 6 14 7 0 23 3 0 10000 15.58 9.25 3.55

Now, the specific problem lies in that the line, "datafile >> index;", is not reading in an index regardless of what I try to tweak. It should be reading, 2 on the first loop, 3 on the next, and so on and so forth. Now that this has been brought to VS 2005, it won't run properly. What am I missing Any advice or comments are welcome, thank you.



Re: Visual C++ Language ifstream issues - reading in a data file

einaros

Try extracting the name explicitly first:

Code Snippet

for (int i=0; i<num_entries; i++)

{

string name;

datafile >> name;

int index;

datafile >> index;

if (index < 0 || index >=250)

cout << "Error!";

else

cout << "No Error!";

datafile.getline(comments,150);

}






Re: Visual C++ Language ifstream issues - reading in a data file

Draznar

I am able to get the name out, your example works well for that too. But here is another issue. First time it will get 2 for the index, and the 2nd time in the loop it will get 3, but after that it gets stuck on 3 and it never changes Here is more of the file I am reading in to help illustrate this.

number of players in file : 232

Name # T C D V R I T TC LC Z M B BC S A Tu Se D J WR L W H

AH-1W 2 1 5 739 5 6 4 3 15 2 2 0 14 0 15 6 8 24 3 1 10000 13.87 3.28 4.44

AV-8B 3 2 6 662 5 2 80 2 14 1 1 0 16 6 14 7 0 23 3 0 10000 15.58 9.25 3.55

Bar Lock (EW/CGI) 4 68 7 2061 5 11 10 59 6 4 1.5 0 93 1 0 0 0 48 0 0 0 3 3 4

BMP 5 9 7 144 5 1 1 1 13 4 1.5 0 3 3 3 0 1 8 0 0 3000 6.72 3.15 2.45

Command Center 6 10 7 278 5 62 61 80 6 4 1.5 0 8 2 0 0 0 0 0 0 0 6 3 3

CH-46E 7 2 5 748 7 4 81 4 15 2 2.5 0 15 0 5 5 0 3 3 0 0 13.89 4.49 5.08

CH-53E 8 3 5 760 8 7 3 5 15 2 3 0 87 0 5 5 0 3 3 0 0 22.35 2.69 8.97

Cymbelline (EW/CGI) 9 11 7 2169 5 61 60 75 6 4 1.5 0 23 1 0 0 0 12 0 0 0 3 3 4

......(it goes on for a while).

I believe the problem lies in the spaces in some of the names, IE "Bar Lock" or "Command Center". What would be the best way for me to be able to get any kind of name within a certain amount of characters but without having any specific dilimiters such as a ':' or other symbols I can't change the format of this file, I have to stick with it.





Re: Visual C++ Language ifstream issues - reading in a data file

einaros

Yep, with the spaced names, that previous suggestion wont work. If each line has the same amount of chars (whitespace or otherwise) prior to the number, just read them all off the stream before parsing the number. Otherwise your options are to read until you hit a number (and you're sure it's a number not attached to the name), or use a regex library to split the lines apart properly. www.boost.org has a good regex implementation, should you wish to follow that path.




Re: Visual C++ Language ifstream issues - reading in a data file

Simple Samples

For the purpose of a common terminology, let's say that each line consists of tokens separated by whitespace. In this situation, whitespace probably consists of one or more spaces but the term whitespace is common and relevant here.

Assuming that, other than the name, the number of tokens per line is constant, then you can simply count the tokens and when the count is greater than a specific amount, you know the number of additional tokens in the name. Or you could remove tokens beginning at the end and then after the specific number of tokens you can assume the remainer (beginning of the line) is the name.