c++文件有一些问题,求大神解答

鱼酥不是叔 2019-05-20 11:30:04
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int a[20];
int b[20];
fstream f1,f2;
f1.open("D://a1.txt");
f2.open("D://a2.txt");
for (int i = 0; i < 20; i++)
{
cin >> a[i];
f1 << a[i];
if (i % 1 == 0)
f1 << endl;
}
for (int i = 0; i < 20; i++)
{
f1 >> b[i];
cout << b[i];
}
f1.close();
f2.close();
return 0;
}
以上为代码,编译通过。但结果b数组不能成功得到f1文件里的数据,请问这是为什么
...全文
127 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
鱼酥不是叔 2019-05-23
  • 打赏
  • 举报
回复
引用 13 楼 袁君元 的回复:
主要存在以下问题:
1、文件的斜杠是\\,还有就是写入文件后要重置流指针,这个在上一题中已经说过了
2、你的打开文件的方式不能用app方式,因为它只支持写,不支持读,其实缺省就可以了。
3、你的find函数最好加一个没有找到的输出,人性化一点。
欢迎结贴给分哦!我这里全是干货
修改后的代码如下:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class book {
private:
int num;
string name;
string she;
string author;
public:
friend istream& operator>>(istream& is, book& b);
friend ostream& operator<<(ostream& os, book& b);
friend void find(fstream& f, string a);
};
istream& operator>>(istream& is, book& b) {
is >> b.name >> b.num >> b.she >> b.author;
return is;
}
ostream& operator<<(ostream& os, book& b) {
os << b.name << " " << b.num << " " << b.she << " " << b.author;
return os;
}
void shuru(fstream &f,int n) {
book *b;
b = new book[n];
cout << "start input"<<" ";
for (int i = 0; i < n; i++) {
cin >> b[i];
f << b[i];
f << endl;
}
}
void find(fstream &f,string a) {
book b;
int flag=0;//标记是否找到
while (!f.eof()) {
f >> b;
if (b.name == a) {
cout << endl << b.name << endl;
flag=1;
}
}
if(flag==0)
cout<<"没有找到"<<endl;
}
int main() {
fstream f;
f.open("D:\\book.txt");//斜杠是\\这样的/,还有app支持写,不支持读。直接缺省值就行!
shuru(f,3);
shuru(f,1);
f.seekg(ios::beg);//重置流指针,回到文件开始,不然流指针在最后,读的就是随机值
find(f, "an");
f.close();
return 0;
}
好的谢谢大神!!
CaptainXue 2019-05-22
  • 打赏
  • 举报
回复
主要存在以下问题:
1、文件的斜杠是\\,还有就是写入文件后要重置流指针,这个在上一题中已经说过了
2、你的打开文件的方式不能用app方式,因为它只支持写,不支持读,其实缺省就可以了。
3、你的find函数最好加一个没有找到的输出,人性化一点。
欢迎结贴给分哦!我这里全是干货
修改后的代码如下:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class book {
private:
int num;
string name;
string she;
string author;
public:
friend istream& operator>>(istream& is, book& b);
friend ostream& operator<<(ostream& os, book& b);
friend void find(fstream& f, string a);
};
istream& operator>>(istream& is, book& b) {
is >> b.name >> b.num >> b.she >> b.author;
return is;
}
ostream& operator<<(ostream& os, book& b) {
os << b.name << " " << b.num << " " << b.she << " " << b.author;
return os;
}
void shuru(fstream &f,int n) {
book *b;
b = new book[n];
cout << "start input"<<" ";
for (int i = 0; i < n; i++) {
cin >> b[i];
f << b[i];
f << endl;
}
}
void find(fstream &f,string a) {
book b;
int flag=0;//标记是否找到
while (!f.eof()) {
f >> b;
if (b.name == a) {
cout << endl << b.name << endl;
flag=1;
}
}
if(flag==0)
cout<<"没有找到"<<endl;
}
int main() {
fstream f;
f.open("D:\\book.txt");//斜杠是\\这样的/,还有app支持写,不支持读。直接缺省值就行!
shuru(f,3);
shuru(f,1);
f.seekg(ios::beg);//重置流指针,回到文件开始,不然流指针在最后,读的就是随机值
find(f, "an");
f.close();
return 0;
}
鱼酥不是叔 2019-05-22
  • 打赏
  • 举报
回复
引用 11 楼 袁君元 的回复:
程序存在以下:
1、路径的斜杠写反了,应该为\\
2、f1文件写入完成后,需要重新回到文件开始,否则会产生随机值。f1.seekg(ios::beg);
3、根据你写的程序,发现你的f2在这里并没有作用,如果是想把f1的内容写到f2中,应该加一句 f2 << b[i]<<endl;表示把b[i]的值写入到f2中。
修改后的代码如下:


#include<iostream>
#include<fstream>
using namespace std;
int main() {
int a[20];
int b[20];
fstream f1,f2;
f1.open("D:\\a1.txt");//路径是\\,而不是//
f2.open("D:\\a2.txt");
for (int i = 0; i < 20; i++) {
cin >> a[i];
// f1 << a[i];
// if (i % 1 == 0)//这里不需要,因为任何数%1都等于0,所以这里不需要
// f1 << endl;
f1<<a[i]<<endl;//把前面三句整合为这一句
}
f1.seekg(ios::beg);//重置流指针,回到文件开始,不然流指针在最后,读的就是随机值
for (int i = 0; i <20; i++) {
f1 >> b[i];
f2 << b[i]<<endl;
cout <<b[i]<<endl;
}
f1.close();
f2.close();
return 0;
}

好的,谢谢大神。能再问大神一个问题吗
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class book
{
private:int num;
string name;
string she;
string author;
public:friend istream& operator>>(istream& is, book& b);
friend ostream& operator<<(ostream& os, book& b);
friend void find(fstream& f, string a);
};
istream& operator>>(istream& is, book& b)
{
is >> b.name >> b.num >> b.she >> b.author;
return is;
}
ostream& operator<<(ostream& os, book& b)
{
os << b.name << " " << b.num << " " << b.she << " " << b.author;
return os;
}
void shuru(fstream &f,int n)
{
book *b;
b = new book[n];
cout << "start input"<<" ";
for (int i = 0; i < n; i++)
{
cin >> b[i];
f << b[i];
f << endl;
}
}
void find(fstream &f,string a)
{
book b;
while (!f.eof())
{
f >> b;
if (b.name == a)
{
cout << endl << b.name << endl;
}
}
}
int main()
{
fstream f;
f.open("D://book.txt", ios::app);
shuru(f,3);
shuru(f,1);
find(f, "an");
f.close();
return 0;
}
这是代码,然后我的find函数无法正常实现功能
636f6c696e 2019-05-21
  • 打赏
  • 举报
回复
确定D://a2.txt文件存在么?
CaptainXue 2019-05-21
  • 打赏
  • 举报
回复
程序存在以下:
1、路径的斜杠写反了,应该为\\
2、f1文件写入完成后,需要重新回到文件开始,否则会产生随机值。f1.seekg(ios::beg);
3、根据你写的程序,发现你的f2在这里并没有作用,如果是想把f1的内容写到f2中,应该加一句 f2 << b[i]<<endl;表示把b[i]的值写入到f2中。
修改后的代码如下:


#include<iostream>
#include<fstream>
using namespace std;
int main() {
int a[20];
int b[20];
fstream f1,f2;
f1.open("D:\\a1.txt");//路径是\\,而不是//
f2.open("D:\\a2.txt");
for (int i = 0; i < 20; i++) {
cin >> a[i];
// f1 << a[i];
// if (i % 1 == 0)//这里不需要,因为任何数%1都等于0,所以这里不需要
// f1 << endl;
f1<<a[i]<<endl;//把前面三句整合为这一句
}
f1.seekg(ios::beg);//重置流指针,回到文件开始,不然流指针在最后,读的就是随机值
for (int i = 0; i <20; i++) {
f1 >> b[i];
f2 << b[i]<<endl;
cout <<b[i]<<endl;
}
f1.close();
f2.close();
return 0;
}

鱼酥不是叔 2019-05-21
  • 打赏
  • 举报
回复
引用 8 楼 636f6c696e 的回复:
你的代码哪里有对a2读和写了?
引用 7 楼 鱼酥不是叔 的回复:
引用 6 楼 鱼酥不是叔 的回复:
引用 4 楼 636f6c696e 的回复:
建议判断下文件打开是否成功
if (!f1 || !f2)
printf("file open failed!\n");

引用 2 楼 鱼酥不是叔 的回复:
引用 1 楼 636f6c696e 的回复:
确定D://a2.txt文件存在么?
存在的...
加了,但a2里的数据仍然为码
乱码

for (int i = 0; i < 20; i++)
{
cin >> a[i];
f1 << a[i];
if (i % 1 == 0)
f1 << endl;
f1 >> b[i];
f2 << b[i];
cout << b[i];
}
本来的for循环,然后a2里面的数据都是乱码,而且a1里只有第一个输入的数据
赵4老师 2019-05-21
  • 打赏
  • 举报
回复
仅供参考:
//NAME: essaie bla bla
//DIMENSION: 8
//DATA
//1  14  15
//2  11  10
//3  6   4
//4  7   13
//5  9   21
//6  19  3
//7  1   5
//8  8   8
//EOF
//
// 文本文件中可能还含有其他内容,但是需要用到的内容即以上

//比如data.txt:
//NAME: essaie bla bla
//其它内容
//DIMENSION: 8
//其它内容
//DATA
//其它内容
//1  14  15
//其它内容
//2  11  10
//其它内容
//3  6   4
//其它内容
//4  7   13
//其它内容
//5  9   21
//其它内容
//6  19  3
//其它内容
//7  1   5
//其它内容
//8  8   8
//其它内容
//EOF

// 目标是要获取NAME后字串,DIMENSION后数值,以及DATA以下的数值
// 其中NAME就是随便个字句,DIMENSION是城市数量,DATA以下是城市编号,X坐标,Y坐标
// 所有的这些将赋值给一个事先定义好的结构
#include <stdio.h>
#include <string.h>
#define MAXCPL   80   //每行最大字符数
#define MAXCITY  100  //每组数据中DATA最多项数,DIMENSION的最大值
#define MAXNAMEL 32   //NAME最大长度
struct S {
    char NAME[MAXNAMEL+1];
    int  DIMENSION;
    struct D {
        int NO;
        int X;
        int Y;
    } DATA[MAXCITY];
} s;
FILE *f;
int st,n,i;
char ln[MAXCPL];
int main() {
    f=fopen("data.txt","r");
    if (NULL==f) {
        printf("Can not open file data.txt!\n");
        return 1;
    }
    st=0;
    n=0;
    while (1) {
        if (NULL==fgets(ln,MAXCPL,f)) break;
        if (st==0) {
            if (1==sscanf(ln,"NAME: %31[^\n]",s.NAME)) st=1;
        } else if (st==1) {
            if (1==sscanf(ln,"DIMENSION: %d",&s.DIMENSION)) st=2;
        } else if (st==2) {
            if (0==strcmp(ln,"DATA\n")) st=3;
        } else if (st==3) {
            if (3==sscanf(ln,"%d%d%d",&s.DATA[n].NO,&s.DATA[n].X,&s.DATA[n].Y)) {
                n++;
                if (n>=MAXCITY || n>=s.DIMENSION) break;
            }
        }
    }
    fclose(f);
    printf("s.NAME=[%s]\n",s.NAME);
    printf("s.DIMENSION=%d\n",s.DIMENSION);
    for (i=0;i<n;i++) {
        printf("s.DATA[%d].NO,X,Y=%d,%d,%d\n",i,s.DATA[i].NO,s.DATA[i].X,s.DATA[i].Y);
    }
    return 0;
}
//s.NAME=[essaie bla bla]
//s.DIMENSION=8
//s.DATA[0].NO,X,Y=1,14,15
//s.DATA[1].NO,X,Y=2,11,10
//s.DATA[2].NO,X,Y=3,6,4
//s.DATA[3].NO,X,Y=4,7,13
//s.DATA[4].NO,X,Y=5,9,21
//s.DATA[5].NO,X,Y=6,19,3
//s.DATA[6].NO,X,Y=7,1,5
//s.DATA[7].NO,X,Y=8,8,8

636f6c696e 2019-05-21
  • 打赏
  • 举报
回复
你的代码哪里有对a2读和写了?
引用 7 楼 鱼酥不是叔 的回复:
引用 6 楼 鱼酥不是叔 的回复:
引用 4 楼 636f6c696e 的回复:
建议判断下文件打开是否成功
if (!f1 || !f2)
    printf("file open failed!\n");
引用 2 楼 鱼酥不是叔 的回复:
引用 1 楼 636f6c696e 的回复:
确定D://a2.txt文件存在么?
存在的...
加了,但a2里的数据仍然为码
乱码
鱼酥不是叔 2019-05-21
  • 打赏
  • 举报
回复
引用 6 楼 鱼酥不是叔 的回复:
引用 4 楼 636f6c696e 的回复:
建议判断下文件打开是否成功
if (!f1 || !f2)
printf("file open failed!\n");

引用 2 楼 鱼酥不是叔 的回复:
引用 1 楼 636f6c696e 的回复:
确定D://a2.txt文件存在么?
存在的...
加了,但a2里的数据仍然为码
乱码
鱼酥不是叔 2019-05-21
  • 打赏
  • 举报
回复
引用 4 楼 636f6c696e 的回复:
建议判断下文件打开是否成功
if (!f1 || !f2)
printf("file open failed!\n");

引用 2 楼 鱼酥不是叔 的回复:
引用 1 楼 636f6c696e 的回复:
确定D://a2.txt文件存在么?
存在的...
加了,但a2里的数据仍然为代码
鱼酥不是叔 2019-05-21
  • 打赏
  • 举报
回复
引用 3 楼 赵4老师 的回复:
D://a2.txt改为D:\\a2.txt
/和\不是一回事!
我将它修改后,但a2以及b数组里的数组仍然没有读取到a1里的数据....
636f6c696e 2019-05-21
  • 打赏
  • 举报
回复
建议判断下文件打开是否成功
if (!f1 || !f2)
    printf("file open failed!\n");
引用 2 楼 鱼酥不是叔 的回复:
引用 1 楼 636f6c696e 的回复:
确定D://a2.txt文件存在么?
存在的...
赵4老师 2019-05-21
  • 打赏
  • 举报
回复
D://a2.txt改为D:\\a2.txt /和\不是一回事!
鱼酥不是叔 2019-05-21
  • 打赏
  • 举报
回复
引用 1 楼 636f6c696e 的回复:
确定D://a2.txt文件存在么?
存在的...

64,637

社区成员

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

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