注册 登录
编程论坛 C++教室

文件输出操作

功夫熊猫 发布于 2010-10-13 20:56, 1039 次点击
#include<fstream.h>
#include<iostream.h>
int main()
{
    ifstream inFile;
    char item[80];
    float cost;
    inFile.open("data.txt");
    cout<<"~~Collection of old radios~~"<<endl;
    inFile>>item>>cost;
    cout<<item<<"  "<<cost<<endl;
    inFile>>item>>cost;
    cout<<item<<"  "<<cost<<endl;
    inFile.close();
    cout<<"End of my act:"<<endl;
    return 0;
}
我在同一文件夹下建立data。txt文件,并输入Motorola 59.59 Emerson 49.59 怎么程序输出是乱码?
16 回复
#2
功夫熊猫2010-10-13 21:29
那个data.txt究竟要建在哪里啊?求大虾赐教啊?
#3
m21wo2010-10-13 21:56
是建在程序的那个文件夹,是当前路径!你的程序没什么问题啊!
程序代码:
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
    ifstream inFile;
    char item[80];
    float cost;
    inFile.open("data.txt");
    cout<<"~~Collection of old radios~~"<<endl;
    inFile>>item>>cost;
    cout<<item<<"  "<<cost<<endl;
    inFile>>item>>cost;
    cout<<item<<"  "<<cost<<endl;
    inFile.close();
    cout<<"End of my act:"<<endl;
    return 0;
}



只有本站会员才能查看附件,请 登录
#4
tornador2010-10-13 23:07
我试了下,没有问题。data文件要放在工程里面。你说输出乱码,我试了下,程序在找不到data文件是会输出乱码。
希望对你有所帮助。
#5
功夫熊猫2010-10-14 11:17
放到工程里面?能详细解释一下吗?我用的是vc6.0
#6
hahayezhe2010-10-14 11:32
你代码的当前目录 或者绝对路径 百度下了
#7
功夫熊猫2010-10-14 11:59
我把data文件存到e:\\里,然后inFile.open("e:\\data.txt");程序还是找不到文件啊。。。
#8
聋眼睛瞎耳朵2010-10-14 13:55
你的路径写错了!!!应该是("e:\data.txt");你多写了个\     程序没错,再试试吧!!!
#9
troy02152010-10-14 16:02
回复 8楼 聋眼睛瞎耳朵
多写\没错,而且有时候你必须写俩个~
#10
m21wo2010-10-14 16:20
我都截图了,你还没看清啊!
#11
m21wo2010-10-14 16:26
绝对路径也行啊 !inFile.open("e:\\data.txt");也是对的!我用的VS2005
#12
功夫熊猫2010-10-14 20:54
你那是vc6.0的截图么?怎么跟你的不一样?我学c++没多长时间
#13
m21wo2010-10-14 21:40
我截的是txt文档放在那里的图啊!放在你的程序的当前文件夹啊!我用的是VS2005
#14
聋眼睛瞎耳朵2010-10-15 17:49
惭愧惭愧……确实不是路径的问题……楼主说的在理啊……楼主是不是写txt文档和你定义的数据类型对不上啊……实在想不出别的可能了……
#15
功夫熊猫2010-10-15 18:23
嗯,我txt文档名写成了data.txt,多加了个扩展名,花好长时间才发现的,
#16
寒风中的细雨2010-10-16 00:12
加一条判断
if( !infile  )
{
    //要表达的信息 当打开失败
}
#17
zfk3052010-10-19 15:58
只有本站会员才能查看附件,请 登录
1