CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
英特尔®游戏设计大赛100美元现金周周送 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

getline 的使用

楼主izl2zl(木木)2003-11-01 09:40:04 在 C/C++ / C语言 提问

我想根据输入的路径,打开这个文件。程序如下。可是如果输入F:\zl.txt就可以读出。但是要是输入f:\My     Documents\zl.txt就无法读出。有高手指点说用getline(cin,filepath),可是我不会把输入压入vector,以实现程序其他功能。请高手继续指点。谢谢  
  #include     <iostream>      
  #include     <fstream>      
  #include     <string>      
  using     namespace     std;      
     
  int     main()      
  {      
                        vector<string>   words;  
                        string     filepath;      
                        cout<<"请输入文件的路径,像F:\\zl.txt"     <<endl;      
                        cin     >>     filepath;      
                        ifstream     in(filepath.c_str());    
                        while(in   >>   receives)               //读入.in   >>   receives  
                              words.push_back(receives);   //把每个单词存到vector里面        
  }      
  问题点数:50、回复次数:4Top

1 楼zoohoo(zoohoo)回复于 2003-11-01 10:06:49 得分 5

char   buf[1024];  
  ifstream   in(filepath.c_str(),   ios::in);  
  if(in.bad()   ||   !in.good())  
  {  
      cout   <<   "Open   file   error\n";  
  }  
  else  
  {  
      in.getline(buf,   sizeof(buf));  
      words.push_back(string(buf));  
  }  
  Top

2 楼ttlb(__ttlb__ttlb__小鸟)回复于 2003-11-01 13:03:58 得分 35

#include     <iostream>      
  #include     <fstream>      
  #include     <string>      
  using     namespace     std;      
     
  int     main()      
  {      
                        vector<string>   words;  
                        string     filepath;      
                        cout<<"请输入文件的路径,像F:\\zl.txt"     <<endl;      
                        cin     >>     filepath;      
                        ifstream     in(filepath.c_str());    
                        string   str;  
                        while(getline(in,   str,   '\0'))               //读入.in   >>   receives  
                              words.push_back(str);   //把每个单词存到vector里面        
  }      
  Top

3 楼langzi8818(┤天道酬勤┝爱老婆┦┷我是来学习滴┷)回复于 2003-11-01 13:20:11 得分 5

#include     <iostream>      
  #include     <fstream>      
  #include     <string>      
  using     namespace     std;      
     
  int     main()      
  {      
                        vector<string>   words;  
                        string     filepath;      
                        cout<<"请输入文件的路径,像F:\\zl.txt"     <<endl;      
                        cin     >>     filepath;      
                        ifstream     in(filepath.c_str());  
                      string   words;    
                        while(in   >>   words)               //读入.in   >>   receives  
                              words.push_back(receives);   //把每个单词存到vector里面        
  }      
  Top

4 楼ywchen2000(灌水大帝:努力奋斗)回复于 2003-11-01 21:49:38 得分 5

include     <iostream>      
  #include     <fstream>      
  #include     <string>      
  using     namespace     std;      
     
  int     main()      
  {      
                        vector<string>   words;  
                        string     filepath;      
                        cout<<"请输入文件的路径,像F:\\zl.txt"     <<endl;      
                        cin     >>     filepath;      
                        ifstream     in(filepath.c_str(),ios::in);    
                        string   str;  
                        while(getline(in,   str,   '\0'))               //读入.in   >>   receives  
                              words.push_back(str);   //把每个单词存到vector里面        
  }      
   
  Top

相关问题

  • 请问高手如何避免getline()和cin使用的冲突?
  • istream & getline(istream &)
  • getline 问题?
  • getline()的问题
  • 请教getline()
  • 使用
  • *& 使用
  • textbox的使用
  • tabstrip的使用
  • Memo的使用

关键词

  • vector
  • filepath
  • getline
  • 读入
  • ifstream
  • 单词存
  • 输入
  • receives
  • words
  • cout

得分解答快速导航

  • 帖主:izl2zl
  • zoohoo
  • ttlb
  • langzi8818
  • ywchen2000

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo