C / C ++ file read

Reading file

 

std::ifstream infile;
infile.open("/Users/yangwenhuan/IVP_workbench/test/graph.prototxt");
std::stringstream buf;
buf << infile.rdbuf(); 
string prototxt_str = buf.str();
infile.close();

  

FILE *pfile = fopen("/Users/yangwenhuan/IVP_workbench/test/graph.prototxt", "r");
char *data;
if (pfile == NULL)
{
    cout << "Read prototxt fail!" << endl;
    return -1;
}
fseek(pfile, 0, SEEK_END);
int length = ftell(pfile);
data = (char *)malloc((length + 1) * sizeof(char));
rewind(pfile);
length = fread(data, 1, length, pfile);
data[length] = '\0';
fclose(pfile);
string prototxt_str(data);

  

std::ifstream infile("/Users/yangwenhuan/IVP_workbench/test/graph.prototxt");
std::istreambuf_iterator<char> begin(infile);
std::istreambuf_iterator<char> end;
std::string prototxt_str(begin, end);

  

 

Guess you like

Origin www.cnblogs.com/yangwenhuan/p/12409467.html