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




