0
votes

This is my code:

#include <iostream>
#include <fstream>

void WriteInDB()
{
    ofstream myfile;
    myfile.open ("result.txt");
    for(int i=0;i<512;i++)
    {
        if(strcmp(filelist[i],"")!=0)
            myfile << filelist[i]<<"\n";
    }
    myfile.close();
}

When I compile this program, I get the following errors:

Error 14 error C2228: left of '.open' must have class/struct/union
Error 17 error C2228: left of '.close' must have class/struct/union
Error 11 error C2146: syntax error : missing ';' before identifier 'myfile'
Error 10 error C2065: 'ofstream' : undeclared identifier Error 12 error C2065: 'myfile' : undeclared identifier
Error 13 error C2065: 'myfile' : undeclared identifier
Error 15 error C2065: 'myfile' : undeclared identifier
Error 16 error C2065: 'myfile' : undeclared identifier

Can anybody help me to resolve them?

3

3 Answers

3
votes

ostream is a part of the std namespace. As such you need to add:

using namespace std;

Alternatively you can prefix all instances of ostream with std::, ie:

std::ofstream myfile.

1
votes

You forgot to prepend all the standard library stuff with std::.

0
votes
#include <iostream>
#include <fstream>

int const filelist_length = 512;
char *filelist[filelist_length];
// you actually seem to use empty strings rather than null pointers as emtpy
// entries; consider a vector<string> instead

void WriteInDB() {
  using namespace std;
  ofstream myfile ("result.txt");
  for (int i = 0; i < filelist_length; i++) {
    if (strcmp(filelist[i], "") != 0) {
      myfile << filelist[i] << '\n';
    }
  }
}