-4

I'm working with visual studio 2013. I want to output a vector of objects into several files. I am able to create the output file if I just print everything to a single file, but if I try to output to multiple files, nothing happens.

#include<vector>
#include<fstream>
#include<iostream>
#include<string>
using namespace std;

struct object
{
  int a, b;
};

int main()
{
  vector<object> DATA;

  //fill DATA

  ofstream out; string outname;
  outname = "TL" + ".txt";
  out.open(outname.c_str());

  for (int i = 0; i < p; i++)
  {
        for (int k = 0; k < DATA.size(); k++)
        {
            out << i << endl;
            if (DATA[k].a == i)
                out << DATA[k].b << endl;
        }
        out << endl;
  }
  out.close();
  return 0;
}

The above works exactly as I expect. However, if I rearrange it so that I could make separate files:

  for (int i = 0; i < p; i++)
  {
       ofstream out; string outname;
       outname = "TLR" + to_string(i) + ".txt";
       out.open(outname.c_str());
        for (int k = 0; k < DATA.size(); k++)
        {
            if (DATA[k].a == i)
                out << DATA[k].b << endl;
        }
        out.close();
  }

I get no output. I already checked to see if the files were being created in another directory and nada. Placing "cout << out.is_open()" after each of the cases shows that the single file is actually being opened (output 1), while the multiple files are not being opened (output 0).

Could anyone tell me what's going on and what can I do to fix this? I don't want to have to run the program and then open the output file to parse after I've made it.

Thank you.

Jeff
  • 695
  • 1
  • 8
  • 19
  • 1
    The above works exactly as expected? It doesn't compile. None of `'length', 'del', or 'p'` are defined. Regardless, did you also output `outname` to see the actual file name being generated? And perhaps check `perror` since the file was not created? – WhozCraig Aug 25 '16 at 19:37
  • What are length, del and p anyway? – Arnav Borborah Aug 25 '16 at 19:40
  • It should work. There is one difference though - this line `out << i << endl;` in the first case writes `i` multiple times. Maybe you're seeing only that output? – rustyx Aug 25 '16 at 19:45
  • I just replicated this example (giving some values to `p`, `del` and `length`) and everything works fine. The second case correctly creates multiple files. – Artem Sokolov Aug 25 '16 at 19:49
  • @WhozCraig Sorry, those are small ints that are defined in the beginning of main. I didn't think they were important since they have scope for all of main. outname is what I expect. I'll check perror now. – Jeff Aug 25 '16 at 19:51
  • @WhozCraig perror says "invalid argument" – Jeff Aug 25 '16 at 19:57
  • Wow, all those insertions of `std::endl`! I guess you really, really, really want that output to be flushed! (`'\n'` ends a line) – Pete Becker Aug 25 '16 at 20:03
  • Also, read about the difference between initialization and assignment. `std::string outname = "whatever"; std::of stream out(outname.c_str());` is much cleaner. And you don't need to close the file at the end of the block. The destructor will do that for you. – Pete Becker Aug 25 '16 at 20:06
  • @PeteBecker Thanks for the format advice, but that doesn't really help with my problem... – Jeff Aug 25 '16 at 20:07

1 Answers1

0

When I was making the multiple files, I used the pipe, "|", (not shown) in the filename--which is a forbidden character in Windows' filenames.

Jeff
  • 695
  • 1
  • 8
  • 19