我需要我的程序来读取格式如下的文件:
latitude,longitude,address,city,state,zip,phone,college,district,website
34.9438553,-120.4214783,800 S College Dr,Santa Maria,CA,93454, 805.922.6966,ALLAN HANCOCK COLLEGE, Allan Hancock Joint Community College District, www.hancockcollege.edu
38.649353,-121.3482895,4700 College Oak Dr,Sacramento,CA,95841, 916.484.8011,AMERICAN RIVER COLLEGE, Los Rios Community College District, www.arc.losrios.edu
34.6749175,-118.1843197,3041 W Avenue K,Lancaster,CA,93536, 661.722.6300,ANTELOPE VALLEY COLLEGE, Antelope Valley Community College District, www.avc.edu
35.4108801,-118.9736161,1801 Panorama Dr,Bakersfield,CA,93305, 661.395.4011,BAKERSFIELD COLLEGE, Kern Community College District, www.bakersfieldcollege.edu
34.8708435,-117.0210879,2700 Barstow Rd,Barstow,CA,92311, 760.252.2411,BARSTOW COLLEGE, Barstow Community College District, www.barstow.edu
37.8699917,-122.2700007,2050 Center St,Berkeley,CA,94704, 510.981.2800,BERKELEY CITY COLLEGE, Peralta Community College District, www.berkeley.peralta.edu
39.6462028,-121.6477912,3536 Butte Campus Dr,Oroville,CA,95965, 530.895.2511,BUTTE COLLEGE, Butte-Glenn Community College District, www.butte.edu
36.9915657,-121.9243927,6500 Soquel Dr,Aptos,CA,95003, 831.479.6100,CABRILLO COLLEGE, Cabrillo Community College District, www.cabrillo.edu
我想只控制每个学院的纬度和经度,这是每行用逗号分隔的第一组和第二组数字。
即:S学院博士的34.9438553和-120.4214783
我想出了如何阅读整篇文章,但我无法弄清楚如何只阅读文本的某些部分。
以下是我的第一次尝试,没什么特别的,只读全文,因为我不知道从哪里开始。请帮助我,我迷路了
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, const char * argv[])
{
string line;
ifstream myfile ("cccGeoList.txt");
if (myfile.is_open())
{
while (!myfile.eof())
{
getline(myfile, line);
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
答案 0 :(得分:0)
阅读完第一行后,
我认为你可以做到以下几点:
vector<double> latitude( N );
vector<double> longitude( N );
char commaChar;
while ( myfile >> latitude[i] >> commaChar >> longitude[i] ) // read the first part of line.
myfile.ignore( 256, '\n' ); // ignore the rest part till end of line character.
答案 1 :(得分:0)
在读取两个值后,您可以丢弃剩下的行:
string latitude, longitude;
char dummy;
while (myfile >> latitude >> dummy >> longitude)
{
std::cout << latitude << longitude << '\n';
myfile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
更新:在回复评论时,请尝试以下方法:
string line;
int i = 0;
myfile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
while (std::getline(myfile, line, ','))
{
std::cout << line;
if (++i == 2)
{
myfile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
i = 0;
}
}