c语言中有fscanf这样的函数,c++中的fstream类好像没提供这样的函数啊

kevin820601 2009-10-18 11:09:10
到底有没有呢
...全文
305 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞天御剑流 2009-10-18
  • 打赏
  • 举报
回复
晕,没留意有个输入16进制的,改为这样:


#include <iostream>
#include <fstream>

int main()
{
using std::cout;
using std::endl;
std::ifstream FileRd( "key.txt" );
char str[100] = { 0 };
unsigned long ulVal = 0;
while( FileRd >> str >> std::hex >> ulVal )
cout<<str<<" "<<ulVal<<endl;
return 0;
}
arong1234 2009-10-18
  • 打赏
  • 举报
回复
输入格式化是比较奇特的要求,因为通过文本控制输入格式即使用fscanf也不是那么容易控制的,你可以认为只有输出需要格式化,输入要求的格式越简单越好(因为没有哪个用户原意花很大力气去给你一个格式化良好的输入文本)
[Quote=引用 4 楼 kevin820601 的回复:]
C/C++ codefstream不能格式化啊。
我这段代码怎么改成c++风格的啊
#include<iostream>
#include<fstream>usingnamespace std;int main()
{

FILE*fp= fopen("key.txt","r");char str[100]= {0 };
unsignedlong ulVal=0;wh¡­
[/Quote]
飞天御剑流 2009-10-18
  • 打赏
  • 举报
回复
漏了using std::ifstream;
飞天御剑流 2009-10-18
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>

int main()
{
using std::cout;
using std::endl;
ifstream FileRd( "key.txt" );
char str[100] = { 0 };
unsigned long ulVal = 0;
while( FileRd >> str >> ulVal )
cout<<str<<" "<<ulVal<<endl;
return 0;
}
飞天御剑流 2009-10-18
  • 打赏
  • 举报
回复
[Quote=引用楼主 kevin820601 的回复:]
到底有没有呢
[/Quote]

怎么会没有呢。

ifstream FileRd;
int Content;
FileRd >> Content;

就是了。
kevin820601 2009-10-18
  • 打赏
  • 举报
回复
fstream不能格式化啊。
我这段代码怎么改成c++风格的啊
#include <iostream>
#include <fstream>
using namespace std;

int main()
{

FILE *fp = fopen("key.txt","r");
char str[100] = { 0 };
unsigned long ulVal = 0;

while(!feof(fp))
{
fscanf(fp,"%s %x", &str, &ulVal);
cout<<str<<" "<<ulVal<<endl;
}

return 0;
}

[Quote=引用 3 楼 arong1234 的回复:]
为什么不用>>?这个不比fscanf好?
引用 2 楼 kevin820601 的回复:
晕,这个函数多好使啊,不可思议啊

[/Quote]
arong1234 2009-10-18
  • 打赏
  • 举报
回复
为什么不用>>?这个不比fscanf好?
[Quote=引用 2 楼 kevin820601 的回复:]
晕,这个函数多好使啊,不可思议啊
[/Quote]
kevin820601 2009-10-18
  • 打赏
  • 举报
回复
晕,这个函数多好使啊,不可思议啊
jackyjkchen 2009-10-18
  • 打赏
  • 举报
回复
C++了类型较严格,格式化方面的弱化了……
所有的 C / C++ 函数 Constructors (cppstring) Constructors (cppvector) Operators (cppbitset) Operators (cppdeque) Operators (cppstack) Operators (cppstring) Operators (cppvector) abort (stdother) abs (stdmath) acos (stdmath) any (cppbitset) append (cppstring) asctime (stddate) asin (stdmath) assert (stdother) assign (cppdeque) assign (cpplist) assign (cppstring) assign (cppvector) at (cppdeque) at (cppstring) at (cppvector) atan (stdmath) atan2 (stdmath) atexit (stdother) atof (stdstring) atoi (stdstring) atol (stdstring) back (cppdeque) back (cpplist) back (cppqueue) back (cppvector) bad (cppio) begin (cppdeque) begin (cpplist) begin (cppmap) begin (cppmultimap) begin (cppmultiset) begin (cppset) begin (cppstring) begin (cppvector) bsearch (stdother) c_str (cppstring) calloc (stdmem) capacity (cppstring) capacity (cppvector) ceil (stdmath) clear (cppdeque) clear (cppio) clear (cpplist) clear (cppmap) clear (cppmultimap) clear (cppmultiset) clear (cppset) clear (cppvector) clearerr (stdio) clock (stddate) compare (cppstring) copy (cppstring) cos (stdmath) cosh (stdmath) count (cppbitset) count (cppmap) count (cppmultimap) count (cppmultiset) count (cppset) ctime (stddate) data (cppstring) #define (preproc) difftime (stddate) div (stdmath) empty (cppdeque) empty (cpplist) empty (cppmap) empty (cppmultimap) empty (cppmultiset) empty (cpppriorityqueue) empty (cppqueue) empty (cppset) empty (cppstack) empty (cppstring) empty (cppvector) end (cppdeque) end (cpplist) end (cppmap) end (cppmultimap) end (cppmultiset) end (cppset) end (cppstring) end (cppvector) eof (cppio) equal_range (cppmap) equal_range (cppmultimap) equal_range (cppmultiset) equal_range (cppset) erase (cppdeque) erase (cpplist) erase (cppmap) erase (cppmultimap) erase (cppmultiset) erase (cppset) erase (cppstring) erase (cppvector) #error (preproc) exit (stdother) exp (stdmath) fabs (stdmath) fail (cppio)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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