错误1.
fscanf(fi, "%s %i %i %i %i %s", e, &eid, &vstart, &vend, &weight,label);
fprintf(fo, "%i\t%i\t%i\n", vstart, vend, weight);
但是一直读出来都是7位负值,每个都不对。
原因:因为源数据的存储就是一个str长串。这样是读不出来数据的。
错误2:
fopen("xxx","rb或者wb")
都应该用fread(),fwrite();而只有“r” "w“ 才能用fscanf(),fprintf();
所以最后的代码:
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<vector>
#define MAX_STRING 100
using namespace std;
vector<string> split(const string &s, const string &seperator) {
vector<string> result;
typedef string::size_type string_size;
string_size i = 0;
while (i != s.size()) {
//找到字符串中首个不等于分隔符的字母;
int flag = 0;
while (i != s.size() && flag == 0) {
flag = 1;
for (string_size x = 0; x < seperator.size(); ++x)
if (s[i] == seperator[x]) {
++i;
flag = 0;
break;
}
}
//找到又一个分隔符,将两个分隔符之间的字符串取出;
flag = 0;
string_size j = i;
while (j != s.size() && flag == 0) {
for (string_size x = 0; x < seperator.size(); ++x)
if (s[j] == seperator[x]) {
flag = 1;
break;
}
if (flag == 0)
++j;
}
if (i != j) {
result.push_back(s.substr(i, j - i));
i = j;
}
}
return result;
}
int main() {
FILE *fi, *fo;
string e;
int eid, vstart, vend, weight;
int a = 1, b = 2, c = 3, d = 4;
char str[2 * MAX_STRING + 100];
const char *sep = " ";
fi = fopen("2015-9-7-11zaolabel.txt", "r");
fo = fopen("traffic_network.txt", "w");
if (fi == NULL) {
printf("Error:network file not exist!\n");
system("pause");
exit(1);
}
int num_edges = 0;
while (fgets(str, sizeof(str), fi)) {
vector<string> v = split(str, " ");
vstart = atoi(v[2].c_str());
vend = atoi(v[3].c_str());
weight = atoi(v[4].c_str());
fprintf(fo, "%i\t%i\t%i\n", vstart, vend, weight);
num_edges++;
}
fclose(fi);
fclose(fo);
return 0;
}