csv転送jsonファイル
10131 ワード
今日は同僚のニュースの内容をhtmlに入力するのを手伝う必要があるので、毎回手を変えるのは不便で、だからcsv(excel)をしてjsonのc++プログラムを回転して、それからejsを利用してそれをホームページにレンダリングして、レンダリングしたホームページを開けて更に保存して(ソースファイルを保存することができなくて、さもなくばまだ空いています)、内容を1つのhtmlにすることができて、これはmarkで、以下はプログラムです
// convert.cpp
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main() {
freopen("in.csv", "r", stdin);
freopen("json.js", "w", stdout);
string str, s[50];
int g = 0;
int status = 0;
char ch;
str = "";
while((ch = cin.get()) != EOF) {
str += ch;
}
for(int i = 0; i < str.size(); i++) {
if(status == 0) {
if(str[i] == '\"') {
status = 1;
} else {
s[g] += str[i];
status = 2;
}
} else if(status == 1) {
if(str[i] == '\"') {
if(i + 1 >= str.size()) {
g++;
break;
} else if(str[i + 1] == '\"') {
s[g] += '\"';
++i;
} else if(str[i + 1] == ',') {
status = 0;
++g;
++i;
} else if(str[i + 1] == '
') {
status = 0;
++g;
++i;
}
} else {
s[g] += str[i];
}
} else if(status == 2) {
if(str[i] == ',') {
++g;
} else if(str[i] == '
') {
++g;
} else {
s[g] += str[i];
}
}
}
int x = 0;
puts("var data = [");
string name[] = {"title", "detail", "img", "url"};
for(int i = 0; i < g; i++) {
if(x == 0) {
if(i == 0) {
puts("\t{");
} else {
puts("\t},");
puts("\t{");
}
}
cout << "\t\t" << name[x] << ": \"" << s[i] << "\"" << (x == 3 ? "" : ",") << endl;
x = (x + 1) % 4;
}
puts("\t}");
puts("]");
return 0;
}