int main(int argc, char** argv) { double d; int n = 1; if( argc!=2) { printf( "usage: program <number of numbers to read>\n"); return -1; } #ifdef STDIO FILE *f; f = fopen( "numbers.txt", "r"); if ( !f) { printf( "Cannot open file\n"); return -1; } VALUES = atoi( argv[1]); for ( ; VALUES; --VALUES) { fscanf( f,"%lf", &d); printf( "%10.5f", d); #else ifstream f( "numbers.txt", ios_base::in); if( !f.is_open()) { cout << "Cannot open file\n"; return -1; } VALUES = atoi( argv[1]); for ( ; VALUES; --VALUES) { f >> d; cout << std::setw(10) << std::setprecision(5) << std::setiosflags( ios::showpoint) << std::setiosflags( ios::fixed) << d; #endif if (n % 5 == 0) { #ifdef STDIO printf("\n"); #else cout << '\n'; #endif } } #ifdef STDIO fclose( f); #else f.close(); #endif return (EXIT_SUCCESS); }
void parseObjFile(const char* objPath, std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& texcoords, std::vector<glm::vec3>& normals, std::vector<GLuint>& vertIndices, std::vector<GLuint>& texIndices, std::vector<GLuint>& normIndices) { #if (defined USE_FSCANF || defined USE_FGETS) GLchar buf[BUFSIZE]; FILE* fp; // in Win/VS2010, position from ftell after fopen in text mode // is not accurate (due to translation of newlines) if ((NULL == objPath) || (NULL == (fp = fopen(objPath, "rb")))) { return; } #ifdef USE_FSCANF while (EOF != fscanf(fp, "%s", buf)) { #else // USE_FGETS while (NULL != fgets(buf, sizeof(buf), fp)) { #endif #else // USE_GETLINE GLchar* buf; std::string str; std::ifstream fp(objPath); if (!fp.is_open()) { return; } while (fp.good()) { std::getline(fp, str); #endif GLint res = 0; #ifdef USE_GETLINE buf = (GLchar*)str.c_str(); #endif switch (buf[0]) { case 'v': #if (defined USE_FGETS || defined USE_GETLINE) if (' ' == buf[1]) { #else // USE_FSCANF if (1 == strlen(buf)) { #endif glm::vec3 vert; #if (defined USE_FGETS || defined USE_GETLINE) res = sscanf(buf, "v %f %f %f", #else // USE_FSCANF res = fscanf(fp, "%f %f %f\n", #endif &vert.x, &vert.y, &vert.z); if (DIM3 != res) { fprintf(stderr, "Incorrect vert matches on sscanf\n"); } vertices.push_back(vert); } else if ('t' == buf[1]) { glm::vec2 tex; #if (defined USE_FGETS || defined USE_GETLINE) res = sscanf(buf, "vt %f %f", #else // USE_FSCANF res = fscanf(fp, "%f %f\n", #endif &tex.x, &tex.y); if (DIM2 != res) { fprintf(stderr, "Incorrect tex matches on sscanf\n"); } texcoords.push_back(tex); } else if ('n' == buf[1]) { glm::vec3 norm; #if (defined USE_FGETS || defined USE_GETLINE) res = sscanf(buf, "vn %f %f %f", #else // USE_FSCANF res = fscanf(fp, "%f %f %f\n", #endif &norm.x, &norm.y, &norm.z); if (DIM3 != res) { fprintf(stderr, "Incorrect norm matches on sscanf\n"); } normals.push_back(norm); } break; case 'f': GLuint vertsIdx[VERTS_FACE], normsIdx[VERTS_FACE], texIdx[VERTS_FACE] = { 1, 1, 1 }; #if (defined USE_FGETS || defined USE_GETLINE) res = sscanf(buf, "f %d/%d/%d %d/%d/%d %d/%d/%d", #else // USE_FSCANF // ensure fopen with binary NOT text mode for Win/VS2010 long posPtr = ftell(fp); // get current stream pos res = fscanf(fp, "%d/%d/%d %d/%d/%d %d/%d/%d\n", #endif &vertsIdx[0], &texIdx[0], &normsIdx[0], &vertsIdx[1], &texIdx[1], &normsIdx[1], &vertsIdx[2], &texIdx[2], &normsIdx[2]); if (RES_VertTexNorm != res) { #if (defined USE_FGETS || defined USE_GETLINE) res = sscanf(buf, "f %d//%d %d//%d %d//%d", #else // USE_FSCANF fseek(fp, posPtr, SEEK_SET); // reset stream pos res = fscanf(fp, "%d//%d %d//%d %d//%d\n", #endif &vertsIdx[0], &normsIdx[0], &vertsIdx[1], &normsIdx[1], &vertsIdx[2], &normsIdx[2]); }