问题描述
您好。
我有一个文件(用于学校作业),格式如下,并且
分隔符格式。文件中的每条记录都具有以下格式:
123423454567987,29873,James,Ha * rry,St。路易斯,416-555-5555;
" accountNumber,balance,lastNam * e,city,phoneNumber; "每条记录都是
保证不超过350个字符。 "平衡"不再是
而不是20个字符,accountNumber正好是15个字符和
lastName,firstName,city,phone * Number中的字符总数;
是
不超过315个字符。使用复制(初始化)的所有规则,将此记录中的字段复制到数组中第一个空帐户的
相应数据成员
Saving。
a3.dat的内容
123423454567987,29873,James,Ha * rry,St。
Louis,416-555-5555; 22342345456 * 7987, 198745,Jones,Beth,Toronto *,4 16-555-5556; 323423454567987,* 2349,Ng,Wei,Montreal,416-555-5 * 557; 423423454567987,9234617,Wo * o,Charles,Winnipe g,416- 555-555 * 8; 523423454567987,2534,DeJesus *,Pancho,Edmonto n,416-555-5559; * 623423454567987,543876,Smith,B * ob,Charlottet拥有,416-555-5544; * 723423454567987,1234,Kasim ,Vla * dislov,Halifa x,416-555-5566; 82 * 3423454567987,98765,Yamaha,Dav * id,Vancouve r,416-555-5577; 9234 * 23454567987,26486,Lee,Jim,Calg * ary,416 -555-5588; 1134234545679 * 87,83456,Baker,Susan,St。
Louis-de-ha-ha,416-555-5599; 13 * 3423454567987,29873,James ,Harr * y,St。
Louis,416-555-5555;
由于每个记录都是不是一行,并且在
文件中显示为(上图),我怎么去读取每个记录,提取信息
并发送它到
数组中第一个空对象的正确数据成员?
到目前为止,我有这样的功能
void Bank :: workFP(FILE * fp)
{
if(fp!= NULL){
}
else printf(无法打开文件。\ n);
}
提前致谢。感谢任何帮助。
从你的描述:每个记录以'';''字符结尾。
因此逐个字符阅读,直到遇到'';''。将这些
字符保存在缓冲区中(可能是std :: string)。然后你有一个记录的所有
字符,可以继续将它分成
个别作品。
-
Karl Heinz Buchegger
Hello.
I have a file (for a school assignment) with the following format and
delimiter format. Each record in the file has the following format:
123423454567987,29873,James,Ha*rry,St. Louis,416-555-5555;
"accountNumber,balance,lastNam*e,city,phoneNumber; " Each record is
guranteed to be no longer than 350 characters. "balance" is no longer
than 20 characters, "accountNumber" is exactly 15 characters and the
total number of characters in "lastName,firstName,city,phone*Number;"
is
no longer than 315 characters. Copy the fields in this record to the
appropriate data member of the first empty Account in the array
savings, using all the rules for copying (initializing).
Contents of a3.dat
123423454567987,29873,James,Ha*rry,St.
Louis,416-555-5555;22342345456*7987,198745,Jones,Beth,Toronto*,4 16-555-5556;323423454567987,*2349,Ng,Wei,Montreal,416-555-5*557;423423454567987,9234617,Wo*o,Charles,Winnipe g,416-555-555*8;523423454567987,2534,DeJesus*,Pancho,Edmonto n,416-555-5559;*623423454567987,543876,Smith,B*ob,Charlottet own,416-555-5544;*723423454567987,1234,Kasim,Vla*dislov,Halifa x,416-555-5566;82*3423454567987,98765,Yamaha,Dav*id,Vancouve r,416-555-5577;9234*23454567987,26486,Lee,Jim,Calg*ary,416-555-5588;1134234545679*87,83456,Baker,Susan,St.
Louis-de-ha-ha,416-555-5599;13*3423454567987,29873,James,Harr*y,St.
Louis,416-555-5555;
Since each "record" is not a line and is shown as is (above) in the
file, how might I go and read each "record", extract the information
and send it to the correct data member of the first empty object in the
array?
So far I have my function like this
void Bank::workFP(FILE* fp)
{
if(fp != NULL){
}
else printf("Unable to open file.\n");
}
Thanks in advance. Appreciate any help whatsoever.
From your description: Each "record" ends with a '';'' character.
So read character by character until you encounter a '';''. Save those
characters in a buffer (probably a std::string). Then you have all
characters for one record and can proceed to break it into
individual pieces.
--
Karl Heinz Buchegger
kb******@gascad.at
这篇关于用Fgets读取行(?)和一点C ++ {新手程序员}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!