关于文件读取的问题求助

xuxiaofeng055 2009-01-19 12:49:03
我想读入文件如下

__1123_2_0
_134_0_0_0
_________0

Ten character for each line, read two together. then replace the bank as -9

成格式如下
-9 11 23 2 0
1 34 0 0 0
-9 -9 -9 -9 0

试了多次全不成功,求助。
...全文
80 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cpp权哥 2009-01-19
  • 打赏
  • 举报
回复
直接把每行读取出来,两个两个字符取出来比较是不是一对空格,是就变成“-9”,不是就保持原样,然后加个空格,就完事了。
用标准C++写,不用考虑操作系统平台的问题。
#include <fstream>
#include <string>
using namespace std;

int main()
{
char str[100], one[10]=" ";
ifstream fin("infile.txt");
ofstream fout("outfile.txt");
while(fin.getline(str, sizeof(str))){
int len = strlen(str);
for(int i=0; i<len; i+=2){
one[0] = str[i], one[1] = str[i+1];
fout << (strcmp(one," ")==0?"-9":one) << " ";
}
fout << endl;
}
fin.close();
fout.close();
return 0;
}
xiaoyisnail 2009-01-19
  • 打赏
  • 举报
回复

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{

fstream file("temp.txt", ios::in);
if(!file)
{
cerr<<"error"<<endl;
exit(1);
}

string line[128];//保存每一行
int i = 0;
while(getline(file, line[i]))//读文件
{
++i;
}

int line_num = i;

file.clear();
file.close();

file.open("temp.txt", ios::out);//以写方式重新打开文件
if(!file)
{
cerr<<"error"<<endl;
exit(1);
}

i = 0;
//对每一行进行格式处理
while(i<line_num)
{
int j = 0;
while(j<10)
{
if(line[i][j]=='_')
{
if(line[i][j+1]=='_')
file<<"-9";
else if(isdigit(line[i][j+1]))
file<<" "<<line[i][j+1];
}
else if(isdigit(line[i][j]))
{
file<<line[i][j]<<line[i][j+1];
}
if(j<8)
file<<" ";
j+=2;
}
file<<endl;
++i;

}

return 0;
}
baihacker 2009-01-19
  • 打赏
  • 举报
回复
正常情况下,输入文件应该是36Btye

30个字节的数据,还有三个\r\n
baihacker 2009-01-19
  • 打赏
  • 举报
回复

__1123_2_0
_134_0_0_0
_________0

d:\>a
0 11 23 2 0 0
1 34 0 0 0 0
0 0 0 0 0 0

18

//要确保是windows系统,这样的话,/r/n才表示换行
#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{
ifstream infile;
infile.open("temp.txt", ios::in|ios::binary);
char bit[4]={0};
int a = 0;
while(!infile.eof())
{
infile.read(bit, 2 * sizeof(char));
if(!infile) break;
if (bit[0] == '_') bit[0] = ' ';

cout <<atoi(bit) <<" ";
if(bit[0] == '\r') cout <<endl;
a++;
}
cout <<endl <<a <<endl;
return 0;
}
lann64 2009-01-19
  • 打赏
  • 举报
回复
o,忘了关文件了。
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
int main()
{
ifstream infile("temp.txt");
string s;
while (getline(infile,s))
{
int i=0;
while (i<10)
{
if (s[i]=='_')
{
if (s[i+1]=='_') cout<<"-9";
else
if (isdigit(s[i+1])) cout<<" "<<s[i+1];
else cout<<" ";

}
else
{
if (isdigit(s[i])) cout<<s[i];
else cout<<" ";
if (isdigit(s[i+1])) cout<<s[i+1] ;
else cout<<" ";
}
cout<<" ";
i+=2;
}
cout<<endl;
}
infile.close();
return 0;
}
lann64 2009-01-19
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
int main()
{
ifstream infile("temp.txt");
string s;
while (getline(infile,s))
{
int i=0;
while (i<10)
{
if (s[i]=='_')
{
if (s[i+1]=='_') cout<<"-9";
else
if (isdigit(s[i+1])) cout<<" "<<s[i+1];
else cout<<" ";

}
else
{
if (isdigit(s[i])) cout<<s[i];
else cout<<" ";
if (isdigit(s[i+1])) cout<<s[i+1] ;
else cout<<" ";
}
cout<<" ";
i+=2;
}
cout<<endl;
}
return 0;
}
xuxiaofeng055 2009-01-19
  • 打赏
  • 举报
回复
ifstream infile;
infile.open("temp.txt");
char bit[2];
int a = 0;
while(!infile.eof())
{
infile.read(bit, 2 * sizeof(char));
if(bit[0] == '\n') cout<<endl;
cout<<atoi(bit)<<" ";
a++;
}
cout<<endl<<a<<endl;

I used the above code only can get the results as below:
0 10 11 0
1 2 0 11 21
9

I do not know where is the 9 come from, and why the first char in the second line is shifted.

Thanks,

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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