凌晨三点跪求高手帮忙,txt文档读取数据到数组中

zzsyda 2011-11-15 03:29:15
#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
{ //文件存在
LINES = CountLines(filename);
string* t1 = new string[LINES];
string* t2 = new string[LINES];
string* t3 = new string[LINES];
string* t4 = new string[LINES];
string* t5 = new string[LINES];
string* t6 = new string[LINES];
string* t7 = new string[LINES];
int i = 0;
while (!file.eof())
{ //读取数据到数组
file.get();
getline(file, t1[i], ',');
file >> t2[i];
file >> t3[i];
file >> t3[i];
file >> t5[i];
file >> t6[i];
file >> t7[i];
i++;
}
file.close(); //关闭文件
for (i = 0; i < LINES; i++) //输出数组内容
cout << t1[i] << t2[i] << t3[i] << t4[i] << t5[i] << t6[i] << t7[i] << endl;
cin.get();
cin.get();
}
return 0;
}



txt文档每行有三种情况
第一种情况:(只有6组数据)
12abcd efg1a,111,111,EEEEE, ,NN
12abcd efg1 b,112,112,FFF, ,NN

第二种情况:
12abcd efg1,12,12,A,DDD,88/88,NN
abcd efg 222,11,11,C,BBB,99/99,NN

第三种情况:(第一组数据带双引号)
"12abcd efg1",12,12,A,CCC,88/88,NN
"abcd efg",11,11,C,AAA,99/99,NN

想读取出来7列数组,逗号中间为数据,包含空格。
遇到第一种情况自动略过或强行读取为7组,最后一组为空。
遇到第二种情况正好每组数组对应一个数据
遇到第三种情况,第1列数组略过双引号读取(只读取双引号内数据),其他的跟第二组一样。


跪求高手帮我修改下,反复尝试了几个小时了,无法写出,总是错误百出。。。。而且很奇怪,
为什么我读取的数组缺少第一个字符?
...全文
238 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
柯本 2011-11-15
  • 打赏
  • 举报
回复
就按LZ的代码改下(其实文件只读一遍就可以的),仅供参考

#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;
}

}

心死 2011-11-15
  • 打赏
  • 举报
回复
楼主强人,半夜3点半还在整代码。
zzsyda 2011-11-15
  • 打赏
  • 举报
回复
1楼的代码很强大。但是我忘了说明一个问题。

第三种情况:(第一组数据带双引号)
"12abcd, efg1",12,12,A,CCC,88/88,NN
"abcd, efg",11,11,C,AAA,99/99,NN


带引号的原因是引号中有逗号,呵呵,谢谢您的代码,我再修改下试试!
谢谢楼上各位朋友。
csuyuanweiqingking 2011-11-15
  • 打赏
  • 举报
回复
对于分割字符可以使用strtok(),其具体的使用方式如下
char *tokenPtr;
tokenPtr = Strtok(string, " ");
将tokenPtr赋给string中第一个标记的指针。strtok的第二个参数””表示string中的标记用空格分开。
函数strtok搜索string中不是分隔符(空格)的第一个字符,这是第一个标记的开头。然后函数寻找字符串中的下一个分隔符,将其换成null(, w,)字符,这是当前标记的终点。函数strtok保存string中标记后面的下一个字符的指针,并返回当前标记的指针。
后面再调用strtok时,第一个参数为NULL,继续将string标记化。NULL参数表示调用strtok继续从string中上次调用 strtok时保存的位置开始标记化。如果调用strtok时已经没有标记,则strtok返回NULL。
dq 2011-11-15
  • 打赏
  • 举报
回复
C++ Primer这本书的习题有,可以参考下。
luciferisnotsatan 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 dizuo 的回复:]

你可以逐行读取,然后对每行内存进行分割
使用strtok函数,标签是", \""
[/Quote]
按lz描述,分隔符就只有逗号,分隔符应该是 "," 吧。
对于第3种,判断下分隔后的字符串,把两端的冒号去了
ryfdizuo 2011-11-15
  • 打赏
  • 举报
回复
你可以逐行读取,然后对每行内存进行分割
使用strtok函数,标签是", \""
wintree 2011-11-15
  • 打赏
  • 举报
回复
首先 楼主和楼上的精神让小弟甚是感动,,,,我是过来帮顶的啊啊啊啊啊。接分的!!
还有就是一堆每行数据都要进行判断啊。。。。再放进数组啊!!


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++;
}
yulinlang 2011-11-15
  • 打赏
  • 举报
回复
#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;
}
柯本 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zzsyda 的回复:]
1楼的代码很强大。但是我忘了说明一个问题。

第三种情况:(第一组数据带双引号)
"12abcd, efg1",12,12,A,CCC,88/88,NN
"abcd, efg",11,11,C,AAA,99/99,NN


带引号的原因是引号中有逗号,呵呵,谢谢您的代码,我再修改下试试!
谢谢楼上各位朋友。
[/Quote]
如果是这样,我的程序也要改下

#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;
}

}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧