c++ 读写文件出错

feel8 2006-12-22 04:47:48
判断一个文件存在不存在?
如果不存在,写进一个数字1
如果存在,读出数字,加一后再存入,直到读出的数字为100,执行test()函数。

下面的代码可以写1进去,但是读出后就变成一串数字?

#include "iostream.h"
#include "fstream.h"
using namespace std;

#define NUM_FILE "D:\\test.txt"

int main()
{
int i;

ifstream r_file(NUM_FILE, ios::binary);

if(!r_file)
{
r_file.close();
ofstream w_file(NUM_FILE, ios::binary);
w_file << 1;
w_file.close();
}
else
{
cout << r_file.get();
i << r_file.get();
cout << i;
}


system("pause");
return(0);


}
...全文
283 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
feel8 2006-12-22
  • 打赏
  • 举报
回复
谢谢大家,我再研究一下。。

kouzhongling(还没恋爱) 给的代码好像没法自加一。

lann64(昆仑大鹏@迦楼罗)的通过了。谢谢。调试的时候连警告都没有。强人。谢谢。。
sw1024 2006-12-22
  • 打赏
  • 举报
回复
给个最简单的!
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

#define NUM_FILE "D:\\test.txt"

int main()
{
int i=0;
ifstream infile;
infile.open(NUM_FILE,ios::out|ios::nocreate);

if(!infile)
{
ofstream outfile;
outfile.open(NUM_FILE,ios::in);
outfile<<1;
outfile.close();
}
else
{
infile>>i;
infile.close();
i++;
if(i<=100)
{
ofstream outfile;
outfile.open(NUM_FILE,ios::in);
outfile<<i;
outfile.close();
}
else cout<<"OK!"<<endl;
}


system("pause");
return(0);

}
kouzhongling 2006-12-22
  • 打赏
  • 举报
回复
给个比较全的
#include "iostream"
#include "fstream"
#include <vector>
using namespace std;

#define NUM_FILE "D:\\test.txt"

int main()
{
int i;
int n;

char ch;
char buf[50]={0};

vector<char> vc;
vector<char>::iterator it;

ifstream r_file(NUM_FILE, ios::binary);

if(!r_file)
{
r_file.close();
ofstream w_file(NUM_FILE, ios::binary);
w_file << 1;
w_file.close();
}
else
{


while((r_file.get(ch)!='\0')&&!r_file.eof())
vc.push_back(ch);

for(it=vc.begin();it!=vc.end();it++)
cout<<*it;
cout<<endl;

it=vc.begin();

sprintf(buf,"%s",it);
n=atoi(buf);

if (!(n<100))
{
return 1;
}
n++;
ofstream w_file(NUM_FILE, ios::binary);
w_file << n;
w_file.close();

cout<<n;

}


system("pause");
return(0);

}
lann64 2006-12-22
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
using namespace std;

#define NUM_FILE "D:\\test.txt"

int main()
{
int i;

ifstream r_file(NUM_FILE, ios::binary);

if(!r_file)
{
r_file.close();
ofstream w_file(NUM_FILE, ios::binary);
w_file << 1;
w_file.close();
}
else
{
r_file>>i;
cout<<i<<endl;
if (i<100)
{
i++;
r_file.close();
ofstream w_file(NUM_FILE,ios::binary);
w_file<<i;
w_file.close();
}
else
r_file.close();

}


//system("pause");
return(0);


}
lann64 2006-12-22
  • 打赏
  • 举报
回复
r_file>>i;//读入
kouzhongling 2006-12-22
  • 打赏
  • 举报
回复
#include "iostream"
#include "fstream"
#include <vector>
using namespace std;

#define NUM_FILE "D:\\test.txt"

int main()
{
int i;
int n;

char ch;
vector<char> vc;
vector<char>::iterator it;

ifstream r_file(NUM_FILE, ios::binary);

if(!r_file)
{
r_file.close();
ofstream w_file(NUM_FILE, ios::binary);
w_file << 1;
w_file.close();
}
else
{


while((r_file.get(ch)!='\0')&&!r_file.eof())
vc.push_back(ch);

for(it=vc.begin();it!=vc.end();it++)
cout<<*it;
cout<<endl;
}


system("pause");
return(0);


}
feel8 2006-12-22
  • 打赏
  • 举报
回复
判断一个文件存在不存在?
如果不存在,写进一个数字1
如果存在,读出数字,加一后再存入,直到读出的数字为100,执行test()函数。

下面的代码可以写1进去,但是读出后就变成一串数字?

#include "iostream.h"
#include "fstream.h"
using namespace std;


#define NUM_FILE "D:\\test.txt"

int main()
{
int i;

ifstream r_file(NUM_FILE, ios::binary);

if(!r_file)
{
r_file.close();
ofstream w_file(NUM_FILE, ios::binary);
w_file << 1;
w_file.close();
}
else
{
cout << r_file.get();
r_file.close();
}


system("pause");
return(0);


}

就是这样,原来就是这样的,输出了一串数字“494198592”
feel8 2006-12-22
  • 打赏
  • 举报
回复
因为我直接输出cout << r_file.get();的时候就是一串数字,
所以想放到一个整数变量里再输出。

应该怎么弄?
healer_kx 2006-12-22
  • 打赏
  • 举报
回复
我只会CreateFile和FILE*,不会流...
taodm 2006-12-22
  • 打赏
  • 举报
回复
i << r_file.get();
你想干什么?这是对i作左移操作。

64,648

社区成员

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

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