69,371
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string DeleteQuot(const char *p)
{
string s="";
char c;
while((c=*p)!='\0')
{
if (c!='\"')
s+=c;
p++;
}
return s;
}
int CountLines(char* filename) //获取文件的行数
{
ifstream ReadFile;
int n = 0;
string temp;
ReadFile.open(filename, ios::in); //ios::in 表示以只读的方式读取文件
if (ReadFile.fail())
{ //文件打开失败:返回0
return 0;
}
else
{ //文件存在,返回文件行数
while (getline(ReadFile, temp))
{
n++;
}
}
ReadFile.close();
return n;
}
int main()
{
ifstream file;
int LINES;
char filename[512];
cout << "请输入要打开的文件名:" << endl;
cin >> filename;
file.open(filename, ios::in);
if (file.fail()) {
cout << "文件不存在." << endl;
file.close();
cin.get();
cin.get();
}
else
{ //文件存在
char temp[256];
char *p;
int i=0,j;
LINES = CountLines(filename);
string* t[7];
for(j=0;j<7;j++)
t[j] = new string[LINES];
while (!file.eof())
{
file.getline(temp,256);
j=0;
p = strtok(temp, ",");
while (p)
{
t[j++][i]=DeleteQuot(p);
p = strtok(NULL, ",");
}
i++;
}
file.close(); //关闭文件
for (i = 0; i < LINES; i++) //输出数组内容
{
for(j=0;j<7;j++)
cout << t[j][i] << "|";
cout << endl;
}
cin.get();
return 0;
}
}
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int CountLines(char* filename) //获取文件的行数
{
ifstream ReadFile;
int n = 0;
string temp;
ReadFile.open(filename, ios::in); //ios::in 表示以只读的方式读取文件
if (ReadFile.fail()) {
//文件打开失败:返回0
return 0;
} else {
//文件存在,返回文件行数
while (getline(ReadFile, temp)) {
n++;
}
}
ReadFile.close();
return n;
}
int main(int argc, char* argv[])
{
ifstream file;
int LINES;
char filename[512];
cout << "请输入要打开的文件名:" << endl;
cin >> filename;
file.open(filename, ios::in);
if (file.fail()) {
cout << "文件不存在." << endl;
file.close();
return -1;
}
LINES = CountLines(filename);
string (*t)[7]= new string[LINES][7];
int i = 0;
string line;
char buf[256];
while (getline(file, line)) {
istringstream iss(line);
for (int j=0; j < 7; ++j) {
iss.get(buf, 256, ',');
t[i][j] = buf;
if (j == 0) {
if (t[i][j][0] == '"')
t[i][j].erase(0, 1);
if (t[i][j][t[i][j].length()-1] == '"')
t[i][j].erase(t[i][j].length()-1, 1);
}
iss.get();
}
i++;
}
file.close(); //关闭文件
for (i = 0; i < LINES; i++) //输出数组内容
cout << t[i][0] << t[i][1] << t[i][2] << t[i][3] << t[i][4] << t[i][5] << t[i][6] <<endl;
delete t;
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int CountLines(char* filename) //获取文件的行数
{
ifstream ReadFile;
int n = 0;
string temp;
ReadFile.open(filename, ios::in); //ios::in 表示以只读的方式读取文件
if (ReadFile.fail())
{ //文件打开失败:返回0
return 0;
}
else
{ //文件存在,返回文件行数
while (getline(ReadFile, temp))
{
n++;
}
}
ReadFile.close();
return n;
}
int main()
{
ifstream file;
int LINES;
char filename[512];
cout << "请输入要打开的文件名:" << endl;
cin >> filename;
file.open(filename, ios::in);
if (file.fail()) {
cout << "文件不存在." << endl;
file.close();
cin.get();
cin.get();
}
else
{ //文件存在
char temp[256];
char *p;
int i=0,j;
LINES = CountLines(filename);
string* t[7];
for(j=0;j<7;j++)
t[j] = new string[LINES];
while (!file.eof())
{
file.getline(temp,256);
j=0;
if (strchr(temp,'\"')!=NULL)
p = strtok(temp, "\"");
else
p = strtok(temp, ",");
while (p)
{
t[j++][i]=p;
p = strtok(NULL, ",");
}
i++;
}
file.close(); //关闭文件
for (i = 0; i < LINES; i++) //输出数组内容
{
for(j=0;j<7;j++)
cout << t[j][i] << "|";
cout << endl;
}
cin.get();
return 0;
}
}