string - C++ ifstream skips 1st line -
here code.
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main ( ){ ifstream infile; char date1[8], date2[8]; int daytemp1[24], daytemp2[24]; infile.open("weatherdata.txt"); if(infile.fail()){ cout << "file failed open."; exit(1); } infile >> date1 >> date2; cout << date1 << endl; cout << date2 << endl; infile.close(); return 0; } the first 2 lines of weatherdata.txt file are:
01/04/13
01/05/13
date1 supposed contain first date when printed prints '\n' character (an empty line). don't know going on code why skipping first date line. , appreciated. i'm beginner c++.
use std::string instead:
#include <string> std::string date1; std::string date2; //... infile >> date1 >> date2; or
std::getline(infile, date1); std::getline(infile, date2);
Comments
Post a Comment