开发者

Compilation error at input\output files

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 e开发者_运维技巧rror 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?


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.


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


#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';
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜