-3

I'm trying to read a table of tab deliminated data into a 2 dimensional vector in c++. The following code compiles with no errors, yet the condition in the while loop (I think) is always evaluated to be 0 so the 2-D vector is never built. What am I doing wrong?

#pragma once
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(int argc, char * argv[])
{
    fstream tabFile("C:\dev\file.tab", ios_base::in);
    vector< vector<float> > verts;
    float x, y, z;
    int ind;
    while (tabFile >> ind >> x >> y >> z)
    {
        vector<float> vec{x,y,z};
        verts.push_back(vec);
    }
    cout << verts.size() << endl;
    system("PAUSE");
    return 0;
} 

file.tab contents:

1 2 3 4
5 6 7 8
9 10 11 12
  • hi! use ready-to-use solutions! cheers! – gaussblurinc May 20 '17 at 23:40
  • What you're doing wrong is that you're failing to provide a [mcve]. There's nothing wrong with the shown code. Most likely it simply doesn't match the actual format of `file.tab`, but since the contents of the input file are not shown, no answer will be possible. – Sam Varshavchik May 20 '17 at 23:44
  • @gaussblurinc what the hell would that even mean... – Quentin May 20 '17 at 23:44
  • @SamVarshavchik what do you mean? You can create the tab file as shown (or any similar file), copy and paste the code shown, compile, and run. What more do you want me to provide to you? – Curious engineer May 20 '17 at 23:50
  • @Curiousengineer you hadn't shown the file's content at the time. – Quentin May 20 '17 at 23:52
  • Please see [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – IInspectable May 21 '17 at 09:40
  • @Quentin 'table of tab delimited data' brings us to something like csv/yaml files. Instead of writing a small program with a lot of bugs I suggest to take a ready-to-use solution ( csv/yaml library ) – gaussblurinc May 21 '17 at 13:32

1 Answers1

0

In "C:\dev\file.tab", the backslashes are interpreted as character escapes -- in fact, this code shouldn't compile. You need to write "C:\\dev\\file.tab", and of course check that the file has actually been opened before trying to read.

As a side note, #pragma once is supposed to go in header files only, but this is an implementation file.

As another side note, please don't use using namespace std;.

Community
  • 1
  • 1
Quentin
  • 62,093
  • 7
  • 131
  • 191
  • `#pragma once` is not standard. Widely supported, yes. But not standard. Then, doesn't behave exactly as an include guard. Prefer include guards to that. – arboreal84 May 20 '17 at 23:51
  • @arboreal84 granted, but who said it was standard? – Quentin May 20 '17 at 23:54
  • Thanks! That did the trick. I guess I was taking examples on this method too literally. – Curious engineer May 20 '17 at 23:55
  • @Quentin: OP said it was C++ not some C++ from an specific vendor. Therefore try to stick to standards. – arboreal84 May 21 '17 at 00:02
  • @arboreal84: The `#pragma` directive is part of the C++ Standard. If a particular directive isn't supported, it has no effect. This being a compilation unit (vs. a header file that needs to be guarded), it doesn't matter, whether it is supported or not. The code will compile with **any** compliant compiler. You are barking up the wrong tree. – IInspectable May 21 '17 at 09:39
  • @arboreal84 OP is apparently using a toolchain where `#pragma once` exists, and could be used better. Is *not* rampaging against OP's compiler extensions a reason for getting DV'd now? – Quentin May 21 '17 at 10:57
  • Still makes the code less portable because it's a non-standard preprocessor directive. C was built with portability in mind. – arboreal84 May 21 '17 at 18:24
  • @arboreal84 if you don't like that extension, go pester OP for choosing it, not me for correcting it. – Quentin May 21 '17 at 19:11