#include"stdafx.h"
#include<stdio.h>
#include
#include
#include
using namespace std;
#define INFINITY INT_MAX //定义无穷大
typedef enum { FALSE, TRUE }Boolean;
typedef struct { //图上的点,即城市
bool pass = false;
string country, city;
float L, B;
}City, Vertex;
typedef struct { //图上的边,即路径
string origin_city, destination_city, transport, other_information;
float time, cost;
}Route, Arc;
typedef struct { //图
Vertex *vertex;
Arc arcs[199][199];
int vexnum, arcnum;
}MGragh;
bool readfile(char* CtFile, char* RtFile, City* ct, Route* rt) {
//存城市信息
FILE *fp = nullptr;
errno_t err = fopen_s(&fp, CtFile, “r”);
if (fp == NULL)
return false;
char ch;
float f;
int i = 0;
while (!feof(fp)) {
//按”行“进行读取
//先读取字符信息
ch = fgetc(fp);
for (; ch != ‘,’; ch = fgetc(fp)) {
ct[i].country += ch;
}
ch = fgetc(fp);
for (; ch != ‘,’; ch = fgetc(fp)) {
ct[i].city += ch;
}
//再读取数字信息
fscanf_s(fp, “%f,”, &f);
ct[i].L = f;
fscanf_s(fp, “%f\n”, &f);
ct[i].B = f;
i++;
}
//关闭文件
fclose(fp);
//存路径信息
FILE *fp1 = nullptr;
errno_t err1 = fopen_s(&fp1, RtFile, “r”);
if (fp1 == NULL)
return false;
i = 0;
ch = fgetc(fp1);
while (!feof(fp1)) {
while (ch != ‘,’) {
rt[i].origin_city += ch;
ch = fgetc(fp1);
}
ch = fgetc(fp1);
while (ch != ‘,’) {
rt[i].destination_city += ch;
ch = fgetc(fp1);
}
ch = fgetc(fp1);
while (ch != ‘,’) {
rt[i].transport += ch;
ch = fgetc(fp1);
}
fscanf_s(fp1, “%f,”, &rt[i].time);
fscanf_s(fp1, “%f,”, &rt[i].cost);
ch = fgetc(fp1);
while (ch != ‘\n’) {
rt[i].other_information += ch;
ch = fgetc(fp1);
}
ch = fgetc(fp1);
i++;
}
fclose(fp1);
return true;
}
运行后string值读不上,调试后,其值为
std::_String_alloc<std::_String_base_types<char,std::allocator > > = {_Mypair={_Myval2={_Bx={_Buf=0x413fc910 “Afghanistan” _Ptr=0x68676641 <读取字符串的字符时出错。> _Alias=0x413fc910 “Afghanistan” } …} } }
有大神知道改怎么改吗!