Opening file issue

I have macro I created that works fine on Ubuntu, but when I moved it to my windows 8 64bit machine, I can no longer open a file. I wonder if it is windows specific.

Filename is “num_bins” and stores the number of bins of data I have to plot. So basic data file.
I have it in both txt and dat file types and neither seems to work.

Basic code segment is as follows:
#include
#include
#include
#include
#include
#include
#include
#include <stdlib.h>

int main(){

std::ifstream file_num_bins(“num_bins”);
if (file_num_bins.is_open()){
getline(file_num_bins,line);
std::stringstream String_toInt(line);
String_toInt >> number_of_bins;

	  file_num_bins.close();
}
else{cout << "File did not open"; return 0;}

Macro and dat files are both saved in same folder of C:\root\macros
I have also tried for filename “num_bins.dat”, “num_bins.txt” to no avail.

I can open files in visual c++ just fine. So I don’t get the difference.
Any help would be appreciated.

Brian

Hi Brian,

This should work (if you specify the file extension). For example, the following works just fine using ROOT 5-34-00-patches on Windows 7 with Visual Studio 2010:

[code]#include “Riostream.h”

void test_ifstream()
{
string line;
ifstream file_num_bins(“num_bins.txt”);
if (file_num_bins.is_open()) {
while(getline(file_num_bins, line))
cout << line << endl;
file_num_bins.close();
}
else {
cout << “File did not open” << endl;
}
}[/code]
Cheers, Bertrand.

Thanks, this is what I was missing. I was compiling using the command shell that came with Root with the tree on it. Once I opened the windows command prompt it ran just fine. I don’t know why it wouldn’t open a file with your prompt, but now it works great.

Your visual studio comment gave me that aha moment.

Brian