c语言如何使用文件中的数据
答案:1 悬赏:60
解决时间 2021-10-04 10:45
- 提问者网友:别再叽里呱啦
- 2021-10-03 09:54
c语言如何使用文件中的数据
最佳答案
- 二级知识专家网友:零点过十分
- 2021-10-03 10:45
一个txt文件中有一些数据如下:
savings 123 70000
checking 661 20000
savings 128 2000
savings 131 5000
checking 681 200000
checking 688 10000
x 0
要求读取的时候先按行读入,再判断是savings还是checking 之后分类,分别读出后面的2个数字
例如 第一行 先判断是savings 读出 123和700002个数字,分别存储,并把字符串转换成数字,
之后再读第二行,遇见x 0的时候停止
急求 用文件流处理,Code不要太复杂,谢谢. int main()
{
ifstream infile;
infile.open("data.txt",ios::in);
char buffer[200];
char *cMark = new char[200];
while(infile.getline(buffer,200))
{
if(strstr(buffer,"x 0"))
{
cout < < "this is the end of file" < < endl;
break;
}
if(strstr(buffer,"savings"))
{
cout < < " this is saving " < < endl;
strt搞定(buffer, " ");
cMark = strt搞定(NULL," ");
int number1 = atoi(cMark);
cout < < "the first number is " < < number1 < < endl;
cMark = strt搞定(NULL," ");
int number2= atoi(cMark);
cout < < " the second number is " < < number2 < < endl;
//delete []cMark;
}
if(strstr(buffer,"checking"))
{
cout < < " this is checking " < < endl;
//char* cMark= new char [100];
strt搞定(buffer, " ");
cMark = strt搞定(NULL," ");
int number1 = atoi(cMark);
cout < < "the first number is " < < number1 < < endl;
cMark = strt搞定(NULL," ");
int number2= atoi(cMark);
cout < < " the second number is " < < number2 < < endl;
//delete []cMark;
}
}
delete[]cMark;
}
savings 123 70000
checking 661 20000
savings 128 2000
savings 131 5000
checking 681 200000
checking 688 10000
x 0
要求读取的时候先按行读入,再判断是savings还是checking 之后分类,分别读出后面的2个数字
例如 第一行 先判断是savings 读出 123和700002个数字,分别存储,并把字符串转换成数字,
之后再读第二行,遇见x 0的时候停止
急求 用文件流处理,Code不要太复杂,谢谢. int main()
{
ifstream infile;
infile.open("data.txt",ios::in);
char buffer[200];
char *cMark = new char[200];
while(infile.getline(buffer,200))
{
if(strstr(buffer,"x 0"))
{
cout < < "this is the end of file" < < endl;
break;
}
if(strstr(buffer,"savings"))
{
cout < < " this is saving " < < endl;
strt搞定(buffer, " ");
cMark = strt搞定(NULL," ");
int number1 = atoi(cMark);
cout < < "the first number is " < < number1 < < endl;
cMark = strt搞定(NULL," ");
int number2= atoi(cMark);
cout < < " the second number is " < < number2 < < endl;
//delete []cMark;
}
if(strstr(buffer,"checking"))
{
cout < < " this is checking " < < endl;
//char* cMark= new char [100];
strt搞定(buffer, " ");
cMark = strt搞定(NULL," ");
int number1 = atoi(cMark);
cout < < "the first number is " < < number1 < < endl;
cMark = strt搞定(NULL," ");
int number2= atoi(cMark);
cout < < " the second number is " < < number2 < < endl;
//delete []cMark;
}
}
delete[]cMark;
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯