读写文件的问题

jlus 2006-06-23 08:52:33
现在有类似于这样的一个文件,每行里的字段都是以逗号分隔,行末也有可能是逗号,也可能没有,要将所有字段内容写入到一个数组里,请问该怎么实现啊,而且文件中每行长度不固定,但是每个字段的长度都是固定的,为10位,初始化时数组的长度怎么分配啊,文件内容如下所示
1234567890,1234567891,1234567892,1234567893
1234567894,1234567895,1234567896,
1234567897,1234567898,1234567899,1334567810,1334567810
1234567890,1234567891,1234567892,1234567893,1334567810,1334567810
...

跪求答案。
...全文
314 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jlus 2006-06-26
  • 打赏
  • 举报
回复
下面是我的实现代码,但是有死循环
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct {
char sHishimuke_Cd[10+1];
} ST_Oriset_Cd;

static long glOrisetLine;
long getFileCount();
long getFileInf(ST_Oriset_Cd*);

main()
{
ST_Oriset_Cd * pstOrisetHenkan;

glOrisetLine=getFileCount();
if (glOrisetLine<0)
{
return 1;
}
pstOrisetHenkan = NULL;
pstOrisetHenkan = (ST_Oriset_Cd *)malloc( (glOrisetLine+1)*sizeof(ST_Oriset_Cd) );
if (pstOrisetHenkan == NULL) {
return 1;
}

memset(pstOrisetHenkan, NULL, sizeof(ST_Oriset_Cd)*(glOrisetLine+1));
getFileInf(pstOrisetHenkan);

return 0;

}

long getFileCount(){
FILE *fp;
long lLine = 0;
char sBUF[1];
char sTmp[10];

if( ( fp = fopen("test.txt", "r") ) == NULL ){
printf("11111111111111");
return 1;
}

memset((void*)sBUF, NULL, sizeof(sBUF));

while (fgets(sBUF,1,fp) != NULL )//死循环的地方
{
printf("222222222222");
if ((sBUF[0] == ',')||
(sBUF[0] == '\n')||
(sBUF[0] == '\r'))
{
printf("33333333333");
fgets(sTmp,10,fp);
}else{
printf("444444444444");
lLine++;
}
}

fclose(fp);
return lLine;
}

long getFileInf(ST_Oriset_Cd * pstOriset){

FILE *fp;
char sBUF[1];
char tmp[10];
int i;
long lLine;

if((fp = fopen("test.txt","r")) == NULL){
fprintf(stderr,"can't open the file","test.txt");
return 1;
}

lLine = 0;
i=0;

memset((void*)sBUF, NULL, sizeof(sBUF));
memset((void*)tmp, NULL, sizeof(tmp));
while (fgets(sBUF,1,fp) != NULL)
{
if (sBUF[0]!= ',')
{
if ((sBUF[0]!= '\n')||
(sBUF[0]!= '\r'))
{
tmp[i]=sBUF[0];
printf("tmp=[%s]",tmp[i]);
i++;
}
}else
{
sprintf(pstOriset[lLine].sHishimuke_Cd, "%s", tmp);
memset((void*)tmp, NULL, sizeof(tmp));
lLine++;
}
}

fclose(fp);
}
jlus 2006-06-26
  • 打赏
  • 举报
回复
呵呵,我不大会用,但是已经用别的办法实现了,刚刚调试成功,谢谢各位大大们的指导,受益良多啊
BEN1978 2006-06-26
  • 打赏
  • 举报
回复
1、使用vector<int>,自动分配空间
2、getline获取每一行字符
3、分析每次的getline,以逗号为分隔符,加入到vector
jlus 2006-06-26
  • 打赏
  • 举报
回复
您这是C++实现的吧,谢谢duduhaha(三人行必有我师) ,是没注意。
du51 2006-06-26
  • 打赏
  • 举报
回复
/* 这份源代码文件已被未注册的SourceFormatX格式化过 */
/* 如果您想不再添加此类信息,请您注册这个共享软件 */
/* 更多相关信息请访问网站: http://cn.textrush.com */

#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <fstream>
#include <sstream>
#include <iterator>
#include <algorithm>
using namespace std;
typedef vector<string>Svec;
int main()
{
fstream ifs("test.txt",ios::in);
if(!ifs.is_open())exit(1);
vector<Svec>vvec;
vector<Svec>::iterator ivvec;
string tempStr,tempStr1;
Svec tempVec;
stringstream sstr;
while(getline(ifs,tempStr))
{
tempVec.clear();
sstr.clear();
sstr<<tempStr;
while(getline(sstr,tempStr1,','))tempVec.push_back(tempStr1);
vvec.push_back(tempVec);
}
ifs.close();
for(ivvec=vvec.begin();ivvec!=vvec.end();++ivvec)
{
cout<<"这一组共有"<<ivvec->size()<<"个数据"<<endl;
copy(ivvec->begin(),ivvec->end(),ostream_iterator<string>(cout," "));
cout<<endl;
}
system("PAUSE");
return 0;
}
du51 2006-06-26
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
#include<cstdlib>
#include<vector>
#include<fstream>
#include<iterator>
using namespace std;
int main()
{
fstream ifs("test.txt",ios::in);
if(!ifs.is_open())
{
exit(1);
}
vector< vector<string> > vvec;
vector< vector<string> >::iterator ivvec;
string tempStr;
vector<string> tempVec;
vector<string>::iterator ivec;
string::iterator first,last;

while(getline(ifs,tempStr))
{
tempVec.clear();
for(first=tempStr.begin();first<tempStr.end();++first)
{
last=first;
while(*first!=','&&first<tempStr.end())++first;
tempVec.push_back(string(last,first));
}
vvec.push_back(tempVec);
}
ifs.close();

for(ivvec=vvec.begin();ivvec!=vvec.end();++ivvec)
{
cout<<"这一组共有"<<ivvec->size()<<"个数据"<<endl;
for(ivec=ivvec->begin();ivec!=ivvec->end();++ivec)
{
cout<<*ivec<<"|"<<endl;//表示结束
}
cout<<endl;
}

system("PAUSE");
return 0;
}
/*
这一组共有4个数据
1234567890|
1234567891|
1234567892|
1234567893|

这一组共有3个数据
1234567894|
1234567895|
1234567896|

这一组共有5个数据
1234567897|
1234567898|
1234567899|
1334567810|
1334567810|

这一组共有6个数据
1234567890|
1234567891|
1234567892|
1234567893|
1334567810|
1334567810|

请按任意键继续. . .
*/
duduhaha 2006-06-26
  • 打赏
  • 举报
回复
fgets(sBUF,1,fp) != NULL )//死循环的地方

搞清函数库的用法.
char *fgets(char *s,int n,FILE *fp);
fgets函数用于读入n - 1个字符到数组s中,还有'\0'这个字符要占位呢.你想想你这个函数里n为1,只能读入0个字符到数组s中,文件根本没有读进去.当然会循环不停止了.
jlus 2006-06-25
  • 打赏
  • 举报
回复
抱歉
今天才有空读代码
楼上的可能没注意我说的
数组的空间该怎么分配啊,每行里用逗号分隔的项目数不是固定的
BaiYangSpirit 2006-06-23
  • 打赏
  • 举报
回复
你是不是在做计费啊?
我经常处理这种格式的文件(华为c&C08)
这是我的一个处理程序
希望对你有帮助
//用fgets()从文件中取得一行数据到内存pRec中,然后规范化后存到二维数组szColumn中
void GenerateARecord(const char* pRec, char szColumn[LINE][COLUMN])
{
const char *pHead=pRec, *pTail=pRec;
int i=0;
bool bIn = false;
while(*pTail)
{
if( ( (*pTail) != ' ' ) && ((*pTail) != ',') && ((*pTail) != 0x0D) &&((*pTail) != 0x0A))
{
pTail ++;
bIn = true;
}else
{
if(bIn)
{
memcpy(szColumn[i++], pHead, abs(pHead-pTail) );
}

pTail ++;
pHead = pTail;
bIn = false;
}
}
}
jlus 2006-06-23
  • 打赏
  • 举报
回复
补充一下,逗号左右不会出现空格,能省事就省事,但是为了以防万一,可不可以对每个字段进行一个长度判断,然后再trim(),我现在准备定成文件结尾不让写",",
jlus 2006-06-23
  • 打赏
  • 举报
回复
是不是在fgets()中这样写:fgets(buf,10,fp)?然后判断取道的字符是不是换行?
不知道c中有没有整行读进来的,然后在以分隔符(比如“,”)号拆成数组,有的话该怎么写
zhaojiang 2006-06-23
  • 打赏
  • 举报
回复
我的思路:
文件需要读两次, 第一次判断得到初始化数组的长度, 第二次才是真正的获取数据.

获取数据(数组长度)的方法: 文件作为文本文件来读, 每次读取一行来分析, 扫描一行中所有字符串, 发现","号就以逗号作为分隔符得到一个数据, 如果行尾有","号, 则最后得到的一个字符串为空(""), 那么可以舍去, 这样循环读取所有行,最后就能得到数据和数组元素的个数.

另外还有一种方法读取数据, 因为数据长度是固定的, 而且都是以逗号相隔(如果","号左右的空格也固定), 那么可以步长为10的进度来读取行字符, 如果末尾有","号, 此时读取的长度肯定不满10的MOD, 那么就应该舍去(同时也说明这一行读完了), 这样的速度应该要快些
jlus 2006-06-23
  • 打赏
  • 举报
回复
不是作计费,不过你的程序先借用一下,谢谢啊。

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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