1

I am trying to read a csv file using the ios package, but nothing is being read. I checked the failbit in the filestream, and for some reason it is immediately set before I actually read anything. Here is my code:

#include <fstream>
#include <iostream> 
using namespace std;
int main(int argc, char* argv[]){
    ifstream infile("~/txs_sample3.csv");
        cout<<"badbit: "<<infile.bad()<<
        ", failbit: "<<infile.fail()<<
        ", eof: "<<infile.eof()<<
        ", good: "<<infile.good()<<endl;
}

txs_sample3.csv:

block-number,gas,gas-price,gas-used,value,from,hash,timestamp,to
2100001,132650,20000000000,32650,1023220.89,0x8224ec27ed526e48cbaba8cb603b1d092e210ebb,0xc3deb238df92f77a41508bb1f731b17c03603f9f5ee802f043a0e35bff6160e7,1471600276,0xe94b04a0fed112f3664e45adb2b8915693dd5ff3
2100001,250000,20000000000,21000,1048570.97,0x9e6316f44baeeee5d41a1070516cc5fa47baf227,0xce7f6bfef33e1982a727b78118faaa55a7f0d9f87240b4a717ea08a8d6b5818b,1471600276,0x29158e02823fdd9cbc66dd3d4a9c51b9df88f823
2100001,90000,20000000000,21000,1010199.4901117386,0xea674fdde714fd979de3edf0f56aa9716b898ec8,0xec2493c5da6a8e562bdffba2c0a7dc7ed1ed261100f013b69a6a764032586c75,1471600276,0xc976d91972173ec9698a9e190f8cd8541e18d2be
2100001,21000,20000000000,21000,1489845.61,0xa8e8311be097afc3537dd084cdd507d6436d72b7,0xb2c139c4038c7c677ff45a197e8724f517ef2bb04eaf0ee7003182663f2ce65f,1471600276,0x91337a300e0361bddb2e377dd4e88ccb7796663d

Any help with what is going on here?

-Paul

Paul
  • 1,106
  • 1
  • 16
  • 39
  • 2
    In your code there is `"~/txs_sample3.csv"` while you talk about `txs_sample2.csv`, guess it's not just a typographical error. – Jack Mar 25 '17 at 00:31
  • @Jack, thank you. But I thought this sort of thing is supposed to be handled by badbit, rather than just failbit? – Paul Mar 25 '17 at 00:57
  • 1
    I got this understanding from this post: http://stackoverflow.com/questions/11085151/c-ifstream-failbit-and-badbit – Paul Mar 25 '17 at 01:18

1 Answers1

5

ifstream, like the fopen and open functions, do not interpolate the ~ character. You'll need to use a different pathname or prepend getenv("HOME") yourself.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • 1
    yes, or you can use [`wordexp`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/wordexp.html) if you have posix compliance. – Jack Mar 25 '17 at 00:41
  • 4
    To expand on this a bit: The ~ expansion we all know and love is a feature of the shell (e.g. Bash), not the filesystem. – John Zwinck Mar 25 '17 at 00:52