23,110
社区成员
发帖
与我相关
我的任务
分享
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
void fun(char const *path, char seperator, int no);
int main(int n, char **v)
{
if (n != 4)
{
puts("usage: <filepath> <column seperator>(one character) <column number>(begin from 1) ");
}
else
{
fun(v[1], v[2][0], atoi(v[3]));
}
return 0;
}
void fun_line(std::string & , char, int);
void fun(char const *path, char seperator, int no)
{
std::vector<std::string> file_content;
std::ifstream ifile(path);
for (std::string tmp; std::getline(ifile, tmp); fun_line(tmp, seperator, no), file_content.push_back(tmp));
ifile.close();
std::ofstream ofile(path);
for (int i = 0; i != file_content.size(); ofile << file_content[i++] << std::endl);
ofile.close();
}
void fun_elem(std::string &elem);
void fun_line(std::string &line, char sep, int no)
{
std::vector<std::string> line_content;
std::istringstream iss(line);
for (std::string tmp; std::getline(iss, tmp, sep); line_content.push_back(tmp));
fun_elem(line_content[no - 1]);
line.clear();
for (int i = 0; i != line_content.size(); line += line_content[i++])
{
if (i != 0) line.push_back(sep);
}
}
#include <time.h>
void fun_elem(std::string &elem)
{
char const *fmt = "%d-%d-%d %02d:%02d:%02d";
tm t;
sscanf(elem.c_str(), fmt,
&t.tm_year, &t.tm_mon, &t.tm_mday,
&t.tm_hour, &t.tm_min, &t.tm_sec);
t.tm_year -= 1900;
t.tm_mon -= 1;
time_t tt = mktime(&t);
tt += 8 * 60 * 60;
tm *pt = localtime(&tt);
char buf[64];
sprintf(buf, fmt,
pt->tm_year + 1900, pt->tm_mon + 1, pt->tm_mday,
pt->tm_hour, pt->tm_min, pt->tm_sec);
elem = buf;
}