CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

c或c++的输入输出问题

楼主darkwowowo(黑暗中呼啸)2002-03-05 16:21:59 在 C/C++ / C语言 提问

要在abc.in(文本方式)读入数据,数据类型是整数,有多个数据,中间用空格格开,还有用回车隔开;然后将读出的数据进行运算,并将运算结果保存在abc.out(文本方式)。各位老大,快点帮我。分数没问题。 问题点数:100、回复次数:8Top

1 楼kbsoft(让世界充满爱!)回复于 2002-03-05 16:24:35 得分 5

将读出的数据放到堆栈里,然后在往文件中写就是了。Top

2 楼jyc_nj(老蔣)回复于 2002-03-05 16:28:30 得分 5

file(abc.in)   -->   memory1   -->....你的运算-->memory2->file(abc.out)Top

3 楼darkwowowo(黑暗中呼啸)回复于 2002-03-05 16:31:17 得分 0

老大,什么头文件?然后用空格隔开的和用回车隔开的怎么区分?Top

4 楼tief(但求中庸)回复于 2002-03-05 16:35:40 得分 40

#include   <iostream>  
  #include   <fstream>  
   
  using   namespace   std   ;  
   
  int   main()  
  {  
  ifstream   fin   ;  
  ofstream   fout   ;  
  int   a,b,c,d,result   ;  
   
  fin.open("abc.in")   ;  
  fin   >>   a   >>   b   >>   c   >>   d   ;  
  fin.close()   ;  
   
  result   =   a   +   b   +   c   +   d   ;  
   
  fout.open("abc.out")   ;  
  fout   <<   result   ;  
  fout.close()   ;  
  return   0   ;  
  }Top

5 楼darkwowowo(黑暗中呼啸)回复于 2002-03-05 16:41:25 得分 0

老大,我去试试,干脆有两道题您帮我做了吧,分数在加,稍候贴上来。Top

6 楼ziqiriying(紫气日盈)回复于 2002-03-05 16:50:27 得分 40

这是   C++Primer   上的例子  
  vector<string>   *retrieve_text()  
  {  
  string   file_name;  
  cout   <<   "   please   enter   file   name:   ";  
  cin   >>   file_name;  
   
  ifstream   infile(   file_name.c_str(),   ios::in   );  
  if(   !   infile   )  
  {  
  cerr   <<   "oops   !   unable   to   open   file   "  
  <<   file_name   <<   "   --   bailing   out!   \n";  
  exit(   -1   );  
  }  
   
  else   cout   <<   '\n';  
   
  vector<string>   *lines_of_text   =   new   vector<string>;  
  string   textline;  
   
  typedef   pair<string::size_type,   int>   stats;  
  stats   maxline;  
  int   linenum   =   0;  
   
  while(   getline(   infile,   textline,   '\n'   )   )  
  {  
  cout<<   "line   read:   "   <<   textline   <<   '\n';  
  if(   maxline.first   <   textline.size()   )  
  {  
  maxline.first   =   textline.size();  
  maxline.second   =   linenum;  
  }  
  lines_of_text->push_back(   textline   );  
  linenum++;  
  }  
   
  return   lines_of_text;  
  }  
  typedef   pair<short,   short>   location;  
  typedef   vector<location>   loc;  
  typedef   vector<string>   text;  
  typedef   pair<text*,loc*>   text_loc;  
   
  text_loc*   separate_words(   const   vector<string>   *text_file)  
  {  
  //   words:   holds   the   collection   of   individual   words  
  //   locations:   holds   the   associated   line/   col   information  
  vector<string>   *words   =   new   vector<string>;  
  vector<location>   *locations   =   new   vector<location>;  
  short   line_pos   =   0;   //   current   line   number;  
   
  //   iterate   through   each   line   of   text  
  for(   ;   line_pos   <   text_file->size();   ++line_pos   )  
  {  
  //   textline:   current   line   of   text   to   process  
  //   word_pos:   current   column   position   within   textline  
  short   word_pos   =   0;  
  string   textline   =   (   *text_file   )[line_pos];  
   
  string::size_type   pos   =   0   ,   prev_pos   =   0;  
  cout   <<   "textline:   "   <<   textline   <<   endl;   //   test   code  
   
  while(   (   pos   =   textline.find_first_of(   '   ',pos   )   )   !=   string::npos   )  
  {  
  //   store   a   copy   of   the   current   word   substring  
  words->push_back(   textline.substr(   prev_pos,   pos   -   prev_pos   )   );  
  cout   <<   "eol:   "   <<   textline.size()   <<   "   pos:"<<   pos   <<   "   line:"       //   test   code  
  <<   line_pos   <<   "   word:   "   <<   word_pos   <<   "   substring:   "   <<   textline.substr(   prev_pos,   pos   -   prev_pos   )   <<   '\n';   //   test   code  
   
   
  //   store   te   line/col   info   as   a   pair  
     
  locations->push_back(   make_pair(   line_pos,   word_pos   )   );  
  //   update   position   information   for   next   iteration  
  ++word_pos;  
  prev_pos   =   ++pos;  
  }  
  words->push_back(   textline.substr(   prev_pos,   pos   -   prev_pos   )   );  
   
  cout   <<   "last   word   on   line   substring:"   <<   textline.substr(   prev_pos,   pos   -   prev_pos   )   <<   endl;   //   test   code  
  locations->push_back(   make_pair(   line_pos,   word_pos)   );  
  }  
   
  return   new   text_loc(   words,   locations   );  
  }  
  整数的你自己改Top

7 楼flyingpsd(我飞呀飞呀飞呀飞)回复于 2002-03-05 17:00:42 得分 10

(UNDER   REDHAT   7.X)  
  man   sscanf  
   
  sscanf(const   char   *str,const   char   *format....)  
   
  int   a,b,c,d,e;  
  string:   1   2   3   4   5    
   
  sscanf(string,"%d   %d   %d   %d   %d",&a,&b,&c,&d,&e);  
  a   =   1   b   =   2   c   =   3   d   =   4   e   =   5Top

8 楼darkwowowo(黑暗中呼啸)回复于 2002-03-05 17:35:47 得分 0

各位谢谢了,我有两道题你们帮我做做吧,另外贴出,另外加分。Top

相关问题

  • C++是怎么输入输出的?
  • C#中文的输入、输出????
  • 请教c/c++中输入/输出的问题
  • 输入输出
  • 问个很弱的C输入输出问题
  • c的输入输出的菜鸟问题
  • C++标准输入输出流库之移花接木
  • 一个C++输入输出流的问题
  • 用C语言如何实现文件的输入/输出
  • C++ Primer 的例子 有关文件输入输出的

关键词

  • vector
  • 数据
  • textline
  • pos
  • 隔开
  • prev
  • pair
  • fin
  • words
  • fout

得分解答快速导航

  • 帖主:darkwowowo
  • kbsoft
  • jyc_nj
  • tief
  • ziqiriying
  • flyingpsd

相关链接

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

广告也精彩

反馈

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