文件读写 写进去了 可是读数据到后来 好像都是一个数字,该如何处理
文件读写 写进去了 可是读数据到后来 好像都是一个数字
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
ofstream lr("test.rl");
uchar temp=111,pre=222;
for(int i=0;i<100000;i++)
{
lr<<temp;
lr<<" ";
lr<<pre;
lr<<" ";
temp++;
pre++; //printf("%d %d\n",temp,pre);
}
lr.close();
ifstream lrr("test.rl");
for(int i=0;i<100000;i++)
{
lrr>>temp;
lrr>>pre;
printf("%d %d\n",temp,pre);
}
system("pause");
return 0;
}
代码如上。。。
我感觉写进去文件 但是当我想读出来的时候 temp 和 pre 都是 25 和171 想问下 这是因为什么 谢谢了
------解决方案--------------------
用编辑软件打开你生产的test.rl看一下,你就发现当数据是10的时候写到文件里就自动转化会0D0A,相当于多了一个数据,所以后面读数据时数据就乱了,
------解决方案--------------------
楼上是对的,你应该打印出来看看
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
ofstream lr("test.rl");
uchar temp=111,pre=222;
for(int i=0;i<100000;i++)
{
lr<<temp;
lr<<" ";
lr<<pre;
lr<<" ";
temp++;
pre++; //printf("%d %d\n",temp,pre);
}
lr.close();
ifstream lrr("test.rl");
for(int i=0;i<100000;i++)
{
lrr>>temp;
lrr>>pre;
printf("%d %d\n",temp,pre);
}
system("pause");
return 0;
}
代码如上。。。
我感觉写进去文件 但是当我想读出来的时候 temp 和 pre 都是 25 和171 想问下 这是因为什么 谢谢了
------解决方案--------------------
用编辑软件打开你生产的test.rl看一下,你就发现当数据是10的时候写到文件里就自动转化会0D0A,相当于多了一个数据,所以后面读数据时数据就乱了,
------解决方案--------------------
楼上是对的,你应该打印出来看看
- C/C++ code
========================= 111 222 112 223 113 224 114 225 115 226 116 227 117 228 118 229 119 230 120 231 121 232 122 233 123 234 124 235 125 236 126 237 127 238 128 239 129 240 130 241 131 242 132 243 133 244 134 245 135 246 136 247 137 248 138 249 139 250 140 251 141 252 142 253 143 254 144 255 145 0 146 1 147 2 148 3 149 4 150 5 151 6 152 7 153 8 ////////这一行之后写的pre出错了 154 155 156 157 158 159 ///这一行之后写的temp出错了 14 160 15 161 16 162 17 163 18 164 19 165 20 166 21 167 22 168 23 169 24 170 25 171 25 171 25 171 25 171 25 171 25 171 25 171 25 171 .... Press any key to continue . . .