『壹』 c++ 如何用 ifstream 读取txt文件的全部内容
#include <fstream>
ifstream fin("a.txt");
以简森仿后在程序中春洞用 fin>> 流入变量。
当然a.txt要和exe在同一文件夹。
否则双引号中要加上路径拦纤,如c:\a.txt
若不懂,请参考c++文件流。
『贰』 c++ifstream读文件内容出现错误
char *name;
char *profession;
char *title;
这三处,都有错,。问题就是没有分内存。
两种方法解决:
方法一:
如果你知道上面三个参数最大需嫌雀配要多少字节,。可以用
这样的方法来分内存。用name为例:name=(char *) malloc(大小);
用完之后,在最后面用 free(name);释放内存。
方法二:
换芹指个岁丛定义的方法
char name[大小];
char profession[大小];
char title[大小];
『叁』 如何用ifstream读取一个100行每行6个数据的文件,存储在一个链表中
//---------------------------------------------------------------------------
#include
<fstream>
#include
<iostream>罩则宏
using
namespace
std;
typedef
struct
node{
int
data;
struct
node
*next;
}
node;
node
*creat(ifstream
&ifp)
{
node
*h=NULL,*p=NULL,*q=NULL;
int
data;
while
(ifp>>物册data)
{
p=new
node;
p->data=data;
p->next=NULL;
if
(!h)
h=p;
else
q->next=p;
q=p;
}
return
h;
}
void
prt(node
*h)
{
if
(h)
{
cout<<h->盯正data<<endl;
prt(h->next);
}
}
int
main(void)
{
ifstream
fp("d:\\data.txt",ifstream::in);
node
*hst=creat(fp);
fp.close();
prt(hst);
return
0;
}
//---------------------------------------------------------------------------
『肆』 ifstream流怎么读取文件里所有的信息急!!
操作符>>在流肢腔吵文件ifstream中,是析取器的圆碰作用,即从标准输入流中读取一个指定类型数据,对于字符串A来说,当遇到回车、空格就会自动停止,因为这被默认为一个字符串的结束。
因此,你如果需要读取txt中的全部内容,应该使用in.get(char)函数,循环读取到文件历侍末尾。
或者,用CFile吧,这个在MFC下比ifstream更好用一些,可以一个Read函数全部读取。
『伍』 c++ 如何用ifstream读取文件
分类: 电脑/网络 >> 程序设计 >> 其他编程语言
问题描述:
ifstream data_read("test.txt");
if (!data_read)
{
cerr << "读取时打开文件出错哗迅物!" << endl;
}
else
{
char ch[100],name,memo[100];
int age[100];
int i=1;
while (!data_read.eof())
{
data_read >> ch;
sscanf(ch,"%s %d %s",&name,&age[i],&memo);
cout << "name=" << name << endl;
cout << "age=" <乱液< age[i] << endl;
cout << "memo=" << memo << endl;
cout << i<< "ch=" << ch << endl ;
i++;
}
}
data_read.close();
我的test.txt文件内容是这样的:
姓名:hehe年昌凳龄:12备注:123
姓名:haihai年龄:18备注:12ff
姓名:aa年龄:22备注:ff11
我想把这些内容能够按照姓名,年龄,备注这样的不同,放在不同的数组里。。
现在我写的那段程序是出错了。。我也不知道怎么改,请高手帮忙,谢谢
我是菜鸟
解析:
不能用sscanf(ch,"%s %d %s",&name,&age[i],&memo);
来拆分你的记录,得用strstr(ch, "姓名:")这样的方法来定位,然后取值,例如:
char * p1 = strstr(ch, "姓名:");
char * p11 = p1 + strlen("姓名:");
char * p2 = strstr(ch, "年龄:");
char * p21 = p2 + strlen("年龄:");
char * p3 = strstr(ch, "备注:");
char * memo = p3 + strlen("备注:");
char name[p2-p11+1];
strncpy(name, p11, p2-p11);
char age[p3-p21+1];
strncpy(age, p21, p3-p21);
『陆』 使用ifstream的get()函数读取文件中的字符发生错误
真正原因是弯陵:在eof()这里。当文件指针到达文件末尾时,执行eof不会立刻返回true,要等到下一次读取时才会返回。即再执行一次get()之后,eof才会返回true。此时,ifstream流的状态标识为失败,get没有读到东西,所以c保存了上一次读取的数据,因此出现最后一个字符被显示两次的现象。
解决:修改代戚空码,使用eof()与fail()结合判断文件结尾。
ifstream fin("input.txt");
while(!fin.eof()){
char c;
fin.get(c);
if(fin.fail()) break;//假如是埋仔戚文件尾部,在c输出之前就跳出
cout<<c<<endl;
}
『柒』 在c++里用ifstream类读取文件错误
你使用了没有定义的标示符,检查一下所有的函数、变量是否都在头文件中定义了,检查一下cpp文件中是否包含了头文件,检查一下函数名称的大小写问题,在c++中,函数a和函数a是两个不同的函数,仔细的检查一下。
『捌』 高手们,C++MFC如何使用ifstream流隔行读取txt文件
#include<iostream>碧正
#include<好慧塌fstream>
usingnamespacestd;
intmain()
{
strings;
ifstreaminfile("txt.txt");
if(!infile)return1;
intn=0;
while(!infile.eof())
{
stringtimes,date;
infile>>times>>友圆date;
n++;
if(n%2==0)
{
cout<<times<<""<<date<<endl;
}
}
return0;
}
『玖』 ifstream读取问题
// while(fi.get(ch)) //问题在这里,fi.get(ch)把每一行的第一个字符读掉了(对于汉字余码蠢来说,就是把竖陪半个字丢了,那后面当然读取有问题模禅了,换成这样判断:
while(fi.peek()!=EOF)
{
getline(fi,a,'\n') ;
cout<<a<<endl;
}