0

I am currently working on an assignment and I am trying to figure out a way to output into a file without using file adress as a function parameter since I am not very good at pointers and this function I am going to declare is going to be used in a class. But however, when I try to build the sample function in a new project I created, I get the error "The system cannot find the file specified". I have tried to clean solution and then rebuild it, build failed and when I tried to run, I still got the same error. Here is my function simplified:

#include <iostream>
#include <fstream>

using namespace std;


int main() {

    void Export(int data) {
        ofstream out("structures_initial.txt");
        out << data << endl;
        return;
    }

    for (int i = 0; i < 10; i++) { //I declared i's type but still the same
        Export(i);
    }

    return 0;
}

I am not sure what causes this error and I was wondering that does my function Export make sense? As I think, calling ofstream should not try to create a new output file and it should open the current one and write but I cannot check because of this error probably because I did something wrong (looked up for this error on the internet and there are just so many different reasons for it, I couldn't fix since I couldn't see the issue in my code). I would be glad if you could help me to fix this problem.

morpheus
  • 23
  • 5
  • 3
    1) The functions, within functions, as specified in your example, are not supported in C++. 2) if `ofstream` fails to find the file - it, usually, creates it. 3) When running projects from, within, VS, the location you run executable at, is **not** the same, as the output directory. So, you may be expecting for the file to be created in a wrong location. – Algirdas Preidžius Dec 18 '16 at 12:37
  • 1
    You want to append to the file: http://stackoverflow.com/questions/2393345/how-to-append-text-to-a-text-file-in-c – doctorlove Dec 18 '16 at 12:37
  • since you already get an error while building the program, you might have a problem with your compiler setup. – tly Dec 18 '16 at 12:39

2 Answers2

0

You could rearrange your code to have the function not defined inside main. Also tell the file to append if that's what you want. Otherwise it will destroy the previous contents, contrary to what you say, "calling ofstream should not try to create a new output file and it should open the current one and write "

The ofstream will attempt to open in the current directory - which you may not have permission to write to.

#include <iostream>
#include <fstream>

using namespace std;

void Export(int data) {
    ofstream out("structures_initial.txt", std::ios_base::app);
    out << data << endl;
    return;
}

int main() {
    for (int i = 0; i < 10; i++) {
        Export(i);
    }
}
doctorlove
  • 17,477
  • 2
  • 41
  • 57
  • Thanks for your solution, realising that I declared the function in the main was a shame. It is working now. I have tried to just take the function declaration outside of the main and after the program executed, the program did not write anything at all to the output file. But it worked when I used ios_base::app but this was something I didn't learn at lectures. Now I looked up for std::ios_base::app and it pretty makes sense. But why didn't using it work at the first place? – morpheus Dec 18 '16 at 12:53
  • You haven't given enough info - not being able to find a file will depend on how/where you ran the code. – doctorlove Dec 18 '16 at 12:54
  • When thinking about iterators, it should not but I am confused since this is the first time I see this. Also this was my first file output including assignment so I was assuming that not moving iterator would always overwrite the first out but it didn't do anything at all like there weren't any iterator in the beginning. – morpheus Dec 18 '16 at 12:57
  • I see, thanks for your help, it really helped a lot :) – morpheus Dec 18 '16 at 12:57
0

You have forget to use ios::app (append)

#include <iostream>
#include <fstream>

using namespace std;

void Export(int data) {
    ofstream out("structures_initial.txt", ios::app);
    out << data << endl;
    return;
}

int main() {



    for (int i = 0; i < 10; i++) { //I declared i's type but still the same
        Export(i);
    }

    return 0;
}

For more information see C++ Files and Streams

Angelica
  • 456
  • 4
  • 18