有段小代码想不通,求助大家

u010258183 2013-08-12 12:05:40
#include <string>  
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream inOut("d:\\a.txt",
fstream::ate | fstream::in | fstream::out);
if (!inOut) {
cerr << "Unable to open file!" << endl;
return EXIT_FAILURE;
}
ifstream::pos_type end_mark = inOut.tellg();
inOut.seekg(0, fstream::beg); // reposition to start of the file
int cnt = 0; // accumulator for byte count
string line; // hold each line of input
while (inOut && inOut.tellg() != end_mark
&& getline(inOut, line))
{
cnt += line.size() + 1; // add 1 to account for the newline
ifstream::pos_type mark = inOut.tellg();
inOut.seekp(0, fstream::end);// set write marker to end
inOut << cnt; // write the accumulated length
if (mark != end_mark) inOut << " ";
inOut.seekg(mark); // restore read position
}
inOut.clear(); // clear flags in case we hit an
inOut.seekp(0, fstream::end);
inOut << "\n";
inOut.close();
return 0;
}

abcd
efg
hi
j
上面这个是程序没运行前的a.txt文件,运行后变为
abcd
efg
hi
j
5 9 12 14
inOut << cnt;在这个代码前面看不太懂,当第一次getline的时候,运行到
inOut.seekp(0, fstream::end);输出流标记在j后面,因为getline输入流留下了一个换行符,运行
inOut << cnt;所以换行和5一起输出,5就跑j下面了,但是第2次循环,还是留了一个换行符,
inOut.seekp(0, fstream::end);把输出流标记标记到5后面空格的后面那个位置,这时候上面还留了个换行符,为什么没有输出来,而是直接在5后面出现了,不是5的在下面?
...全文
454 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-08-13
  • 打赏
  • 举报
回复
引用 26 楼 u010258183 的回复:
[quote=引用 24 楼 zhao4zhong1 的回复:] 代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试是程序员必须掌握的技能之一。 不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实! 有人说一套做一套,你相信他说的还是相信他做的? 其实严格来说这个世界上古往今来所有人都是说一套做一套,不是吗? 不要写连自己也预测不了结果的代码!
这个代码断点调试,进去低层好多变量都不知道什么意思,里面几千几万行的代码跳来跳去,哪里看的懂啊,主要太多了,哎,根本就看不出里面怎么搞的。那么多怎么看[/quote] 那多半是因为你在该按Shift+F11键的时候没按而已。
lm_whales 2013-08-13
  • 打赏
  • 举报
回复
引用 29 楼 u010258183 的回复:
[quote=引用 27 楼 u010258183 的回复:] [quote=引用 25 楼 lm_whales 的回复:] 你的VC10 和我的不一样??? 我的输出结果就是j后面无回车,输出 abcd efg hi j5 9 12 j 后面有回车输出 abcd efg hi j 5 9 12 14 我是同时处理这两种情况的,不存在,手动错误因素在里面 这是代码,基本没有改动你的代码 a.txt,c.txt无回车;b.txt,d.txt有回车。 a,c 结果为第一种,b,d 结果为第二种

#include <string>  
#include <iostream> 
#include <fstream>
using namespace std;  

int testCR(){
 fstream inOut("d:\\a.txt", 
		fstream::ate | fstream::in | fstream::out); 
	if (!inOut) { 
		cerr << "Unable to open file!" << endl; 
		return EXIT_FAILURE; 
	}  
	ifstream::pos_type end_mark = inOut.tellg(); 
	inOut.seekg(0, fstream::beg); // reposition to start of the file 
	int cnt = 0; // accumulator for byte count 
	string line; // hold each line of input 
	while (inOut && inOut.tellg() != end_mark 
		&& getline(inOut, line)) 
	{ 
		cnt += line.size() + 1; // add 1 to account for the newline 
		ifstream::pos_type mark = inOut.tellg(); 
		inOut.seekp(0, fstream::end);// set write marker to end 
		inOut << cnt; // write the accumulated length 
		if (mark != end_mark) inOut << " "; 
		inOut.seekg(mark); // restore read position 
	} 
	inOut.clear(); // clear flags in case we hit an 
	inOut.seekp(0, fstream::end); 
	inOut << "\n"; 
	inOut.close();
	return 0; 


}
int test(){
fstream inOut("d:\\b.txt", 
		fstream::ate | fstream::in | fstream::out); 
	if (!inOut) { 
		cerr << "Unable to open file!" << endl; 
		return EXIT_FAILURE; 
	}  
	ifstream::pos_type end_mark = inOut.tellg(); 
	inOut.seekg(0, fstream::beg); // reposition to start of the file 
	int cnt = 0; // accumulator for byte count 
	string line; // hold each line of input 
	while (inOut && inOut.tellg() != end_mark 
		&& getline(inOut, line)) 
	{ 
		cnt += line.size() + 1; // add 1 to account for the newline 
		ifstream::pos_type mark = inOut.tellg(); 
		inOut.seekp(0, fstream::end);// set write marker to end 
		inOut << cnt; // write the accumulated length 
		if (mark != end_mark) inOut << " "; 
		inOut.seekg(mark); // restore read position 
	} 
	inOut.clear(); // clear flags in case we hit an 
	inOut.seekp(0, fstream::end); 
	inOut << "\n"; 
	inOut.close();
	return 0; 


}

int testIO(const char *filename){
fstream inOut(filename, 
		fstream::ate | fstream::in | fstream::out); 
	if (!inOut) { 
		cerr << "Unable to open file!" << endl; 
		return EXIT_FAILURE; 
	}  
	ifstream::pos_type end_mark = inOut.tellg(); 
	inOut.seekg(0, fstream::beg); // reposition to start of the file 
	int cnt = 0; // accumulator for byte count 
	string line; // hold each line of input 
	while (inOut && inOut.tellg() != end_mark 
		&& getline(inOut, line)) 
	{ 
		cnt += line.size() + 1; // add 1 to account for the newline 
		ifstream::pos_type mark = inOut.tellg(); 
		inOut.seekp(0, fstream::end);// set write marker to end 
		inOut << cnt; // write the accumulated length 
		if (mark != end_mark) inOut << " "; 
		inOut.seekg(mark); // restore read position 
	} 
	inOut.clear(); // clear flags in case we hit an 
	inOut.seekp(0, fstream::end); 
	inOut << "\n"; 
	inOut.close();
	return 0; 


}


int main()  
{
	test();
	testCR();
	
	testIO("d:\\c.txt");
	testIO("d:\\d.txt");

	return 0; 
}
你说的vc10是vs2010吗,我用的vs2010有点时候运行2次有不同但是后面基本上是我说那个格式,真不知道这个流是怎么个运行的[/quote] 好了我搞明白了。多谢了[/quote] 不用谢,我自己对这个问题,也有点糊里糊涂的,有空再研究研究; 这里的继承体系,大概如下: ios | |----------| istream ostream | | | | | iostream | | | | ifstream fstream ofstream
lm_whales 2013-08-12
  • 打赏
  • 举报
回复
VC10 --Vs2010 结果还是那样,绝对没有什么空格. abcd efg hi j5 9 12
u010258183 2013-08-12
  • 打赏
  • 举报
回复
大神来解决下吧,在线等
lm_whales 2013-08-12
  • 打赏
  • 举报
回复
关键是编译器的问题,不同编译器,结果不同。 VC9 --Vs2008 结果就是那样,绝对没有什么空格.
u010258183 2013-08-12
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
 if (条件1) break;
 //...
 if (条件2) continue;
 //...
 if (条件3) return;
 //...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
whlie (!feof(f)) {
 a=fgetc(f);
 //...
 b=fgetc(f);//可能此时已经feof了!
 //...
}
而这样写就没有问题:
whlie (1) {
 a=fgetc(f);
 if (feof(f)) break;
 //...
 b=fgetc(f);
 if (feof(f)) break;
 //...
}
类似的例子还可以举很多。
你没看清问题哦,你说那个在这题窝要问的问题关系不大,也许你那种写法比较好。
u010258183 2013-08-12
  • 打赏
  • 举报
回复
引用 5 楼 lm_whales 的回复:
你这段程序,不同编一起,执行的结果,是不同的。Dev C++ 会永无休止的写。 VC6编译不过去。 VC9,最后一行无回车 abcd efg hi j5 9 12 VC9,最后一行有回车 abcd efg hi j 5 9 12 14 15
你这个实验没搞好啊,没j后面没回车,前面字母最后面你要保证没空格,输出不是你样的你肯定没弄好: j后面没空格和换行的 abcd efg hi j 5 9 12 14 效果是这样才对
u010258183 2013-08-12
  • 打赏
  • 举报
回复
引用 3 楼 tsakang 的回复:
getline只是一行一行的读取文件
你写文件的那些只是在文件尾以数字空格的格式写的
在文件中5换行不换行只和文件尾部有关
换行了说明文件之前在j后面之前就有回车换行
不换行说明文件尾没有回车
这个j后面没有换行符 我自己打的我自己知道,你这样说5就不会在j下面了,你还是没明白我的意思
lm_whales 2013-08-12
  • 打赏
  • 举报
回复
你这段程序,不同编一起,执行的结果,是不同的。Dev C++ 会永无休止的写。 VC6编译不过去。 VC9,最后一行无回车 abcd efg hi j5 9 12 VC9,最后一行有回车 abcd efg hi j 5 9 12 14 15
赵4老师 2013-08-12
  • 打赏
  • 举报
回复
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
 if (条件1) break;
 //...
 if (条件2) continue;
 //...
 if (条件3) return;
 //...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
whlie (!feof(f)) {
 a=fgetc(f);
 //...
 b=fgetc(f);//可能此时已经feof了!
 //...
}
而这样写就没有问题:
whlie (1) {
 a=fgetc(f);
 if (feof(f)) break;
 //...
 b=fgetc(f);
 if (feof(f)) break;
 //...
}
类似的例子还可以举很多。
mbugaifc 2013-08-12
  • 打赏
  • 举报
回复
tsakang 2013-08-12
  • 打赏
  • 举报
回复
getline只是一行一行的读取文件
你写文件的那些只是在文件尾以数字空格的格式写的
在文件中5换行不换行只和文件尾部有关
换行了说明文件之前在j后面之前就有回车换行
不换行说明文件尾没有回车
u010258183 2013-08-12
  • 打赏
  • 举报
回复
引用 27 楼 u010258183 的回复:
[quote=引用 25 楼 lm_whales 的回复:] 你的VC10 和我的不一样??? 我的输出结果就是j后面无回车,输出 abcd efg hi j5 9 12 j 后面有回车输出 abcd efg hi j 5 9 12 14 我是同时处理这两种情况的,不存在,手动错误因素在里面 这是代码,基本没有改动你的代码 a.txt,c.txt无回车;b.txt,d.txt有回车。 a,c 结果为第一种,b,d 结果为第二种

#include <string>  
#include <iostream> 
#include <fstream>
using namespace std;  

int testCR(){
 fstream inOut("d:\\a.txt", 
		fstream::ate | fstream::in | fstream::out); 
	if (!inOut) { 
		cerr << "Unable to open file!" << endl; 
		return EXIT_FAILURE; 
	}  
	ifstream::pos_type end_mark = inOut.tellg(); 
	inOut.seekg(0, fstream::beg); // reposition to start of the file 
	int cnt = 0; // accumulator for byte count 
	string line; // hold each line of input 
	while (inOut && inOut.tellg() != end_mark 
		&& getline(inOut, line)) 
	{ 
		cnt += line.size() + 1; // add 1 to account for the newline 
		ifstream::pos_type mark = inOut.tellg(); 
		inOut.seekp(0, fstream::end);// set write marker to end 
		inOut << cnt; // write the accumulated length 
		if (mark != end_mark) inOut << " "; 
		inOut.seekg(mark); // restore read position 
	} 
	inOut.clear(); // clear flags in case we hit an 
	inOut.seekp(0, fstream::end); 
	inOut << "\n"; 
	inOut.close();
	return 0; 


}
int test(){
fstream inOut("d:\\b.txt", 
		fstream::ate | fstream::in | fstream::out); 
	if (!inOut) { 
		cerr << "Unable to open file!" << endl; 
		return EXIT_FAILURE; 
	}  
	ifstream::pos_type end_mark = inOut.tellg(); 
	inOut.seekg(0, fstream::beg); // reposition to start of the file 
	int cnt = 0; // accumulator for byte count 
	string line; // hold each line of input 
	while (inOut && inOut.tellg() != end_mark 
		&& getline(inOut, line)) 
	{ 
		cnt += line.size() + 1; // add 1 to account for the newline 
		ifstream::pos_type mark = inOut.tellg(); 
		inOut.seekp(0, fstream::end);// set write marker to end 
		inOut << cnt; // write the accumulated length 
		if (mark != end_mark) inOut << " "; 
		inOut.seekg(mark); // restore read position 
	} 
	inOut.clear(); // clear flags in case we hit an 
	inOut.seekp(0, fstream::end); 
	inOut << "\n"; 
	inOut.close();
	return 0; 


}

int testIO(const char *filename){
fstream inOut(filename, 
		fstream::ate | fstream::in | fstream::out); 
	if (!inOut) { 
		cerr << "Unable to open file!" << endl; 
		return EXIT_FAILURE; 
	}  
	ifstream::pos_type end_mark = inOut.tellg(); 
	inOut.seekg(0, fstream::beg); // reposition to start of the file 
	int cnt = 0; // accumulator for byte count 
	string line; // hold each line of input 
	while (inOut && inOut.tellg() != end_mark 
		&& getline(inOut, line)) 
	{ 
		cnt += line.size() + 1; // add 1 to account for the newline 
		ifstream::pos_type mark = inOut.tellg(); 
		inOut.seekp(0, fstream::end);// set write marker to end 
		inOut << cnt; // write the accumulated length 
		if (mark != end_mark) inOut << " "; 
		inOut.seekg(mark); // restore read position 
	} 
	inOut.clear(); // clear flags in case we hit an 
	inOut.seekp(0, fstream::end); 
	inOut << "\n"; 
	inOut.close();
	return 0; 


}


int main()  
{
	test();
	testCR();
	
	testIO("d:\\c.txt");
	testIO("d:\\d.txt");

	return 0; 
}
你说的vc10是vs2010吗,我用的vs2010有点时候运行2次有不同但是后面基本上是我说那个格式,真不知道这个流是怎么个运行的[/quote] 好了我搞明白了。多谢了
u010258183 2013-08-12
  • 打赏
  • 举报
回复
引用 25 楼 lm_whales 的回复:
你的VC10 和我的不一样??? 我的输出结果就是j后面无回车,输出 abcd efg hi j5 9 12 j 后面有回车输出 abcd efg hi j 5 9 12 14 我是同时处理这两种情况的,不存在,手动错误因素在里面 这是代码,基本没有改动你的代码 a.txt,c.txt无回车;b.txt,d.txt有回车。 a,c 结果为第一种,b,d 结果为第二种

#include <string>  
#include <iostream> 
#include <fstream>
using namespace std;  

int testCR(){
 fstream inOut("d:\\a.txt", 
		fstream::ate | fstream::in | fstream::out); 
	if (!inOut) { 
		cerr << "Unable to open file!" << endl; 
		return EXIT_FAILURE; 
	}  
	ifstream::pos_type end_mark = inOut.tellg(); 
	inOut.seekg(0, fstream::beg); // reposition to start of the file 
	int cnt = 0; // accumulator for byte count 
	string line; // hold each line of input 
	while (inOut && inOut.tellg() != end_mark 
		&& getline(inOut, line)) 
	{ 
		cnt += line.size() + 1; // add 1 to account for the newline 
		ifstream::pos_type mark = inOut.tellg(); 
		inOut.seekp(0, fstream::end);// set write marker to end 
		inOut << cnt; // write the accumulated length 
		if (mark != end_mark) inOut << " "; 
		inOut.seekg(mark); // restore read position 
	} 
	inOut.clear(); // clear flags in case we hit an 
	inOut.seekp(0, fstream::end); 
	inOut << "\n"; 
	inOut.close();
	return 0; 


}
int test(){
fstream inOut("d:\\b.txt", 
		fstream::ate | fstream::in | fstream::out); 
	if (!inOut) { 
		cerr << "Unable to open file!" << endl; 
		return EXIT_FAILURE; 
	}  
	ifstream::pos_type end_mark = inOut.tellg(); 
	inOut.seekg(0, fstream::beg); // reposition to start of the file 
	int cnt = 0; // accumulator for byte count 
	string line; // hold each line of input 
	while (inOut && inOut.tellg() != end_mark 
		&& getline(inOut, line)) 
	{ 
		cnt += line.size() + 1; // add 1 to account for the newline 
		ifstream::pos_type mark = inOut.tellg(); 
		inOut.seekp(0, fstream::end);// set write marker to end 
		inOut << cnt; // write the accumulated length 
		if (mark != end_mark) inOut << " "; 
		inOut.seekg(mark); // restore read position 
	} 
	inOut.clear(); // clear flags in case we hit an 
	inOut.seekp(0, fstream::end); 
	inOut << "\n"; 
	inOut.close();
	return 0; 


}

int testIO(const char *filename){
fstream inOut(filename, 
		fstream::ate | fstream::in | fstream::out); 
	if (!inOut) { 
		cerr << "Unable to open file!" << endl; 
		return EXIT_FAILURE; 
	}  
	ifstream::pos_type end_mark = inOut.tellg(); 
	inOut.seekg(0, fstream::beg); // reposition to start of the file 
	int cnt = 0; // accumulator for byte count 
	string line; // hold each line of input 
	while (inOut && inOut.tellg() != end_mark 
		&& getline(inOut, line)) 
	{ 
		cnt += line.size() + 1; // add 1 to account for the newline 
		ifstream::pos_type mark = inOut.tellg(); 
		inOut.seekp(0, fstream::end);// set write marker to end 
		inOut << cnt; // write the accumulated length 
		if (mark != end_mark) inOut << " "; 
		inOut.seekg(mark); // restore read position 
	} 
	inOut.clear(); // clear flags in case we hit an 
	inOut.seekp(0, fstream::end); 
	inOut << "\n"; 
	inOut.close();
	return 0; 


}


int main()  
{
	test();
	testCR();
	
	testIO("d:\\c.txt");
	testIO("d:\\d.txt");

	return 0; 
}
你说的vc10是vs2010吗,我用的vs2010有点时候运行2次有不同但是后面基本上是我说那个格式,真不知道这个流是怎么个运行的
u010258183 2013-08-12
  • 打赏
  • 举报
回复
引用 24 楼 zhao4zhong1 的回复:
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试是程序员必须掌握的技能之一。 不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实! 有人说一套做一套,你相信他说的还是相信他做的? 其实严格来说这个世界上古往今来所有人都是说一套做一套,不是吗? 不要写连自己也预测不了结果的代码!
这个代码断点调试,进去低层好多变量都不知道什么意思,里面几千几万行的代码跳来跳去,哪里看的懂啊,主要太多了,哎,根本就看不出里面怎么搞的。那么多怎么看
lm_whales 2013-08-12
  • 打赏
  • 举报
回复
你的VC10 和我的不一样??? 我的输出结果就是j后面无回车,输出 abcd efg hi j5 9 12 j 后面有回车输出 abcd efg hi j 5 9 12 14 我是同时处理这两种情况的,不存在,手动错误因素在里面 这是代码,基本没有改动你的代码 a.txt,c.txt无回车;b.txt,d.txt有回车。 a,c 结果为第一种,b,d 结果为第二种

#include <string>  
#include <iostream> 
#include <fstream>
using namespace std;  

int testCR(){
 fstream inOut("d:\\a.txt", 
		fstream::ate | fstream::in | fstream::out); 
	if (!inOut) { 
		cerr << "Unable to open file!" << endl; 
		return EXIT_FAILURE; 
	}  
	ifstream::pos_type end_mark = inOut.tellg(); 
	inOut.seekg(0, fstream::beg); // reposition to start of the file 
	int cnt = 0; // accumulator for byte count 
	string line; // hold each line of input 
	while (inOut && inOut.tellg() != end_mark 
		&& getline(inOut, line)) 
	{ 
		cnt += line.size() + 1; // add 1 to account for the newline 
		ifstream::pos_type mark = inOut.tellg(); 
		inOut.seekp(0, fstream::end);// set write marker to end 
		inOut << cnt; // write the accumulated length 
		if (mark != end_mark) inOut << " "; 
		inOut.seekg(mark); // restore read position 
	} 
	inOut.clear(); // clear flags in case we hit an 
	inOut.seekp(0, fstream::end); 
	inOut << "\n"; 
	inOut.close();
	return 0; 


}
int test(){
fstream inOut("d:\\b.txt", 
		fstream::ate | fstream::in | fstream::out); 
	if (!inOut) { 
		cerr << "Unable to open file!" << endl; 
		return EXIT_FAILURE; 
	}  
	ifstream::pos_type end_mark = inOut.tellg(); 
	inOut.seekg(0, fstream::beg); // reposition to start of the file 
	int cnt = 0; // accumulator for byte count 
	string line; // hold each line of input 
	while (inOut && inOut.tellg() != end_mark 
		&& getline(inOut, line)) 
	{ 
		cnt += line.size() + 1; // add 1 to account for the newline 
		ifstream::pos_type mark = inOut.tellg(); 
		inOut.seekp(0, fstream::end);// set write marker to end 
		inOut << cnt; // write the accumulated length 
		if (mark != end_mark) inOut << " "; 
		inOut.seekg(mark); // restore read position 
	} 
	inOut.clear(); // clear flags in case we hit an 
	inOut.seekp(0, fstream::end); 
	inOut << "\n"; 
	inOut.close();
	return 0; 


}

int testIO(const char *filename){
fstream inOut(filename, 
		fstream::ate | fstream::in | fstream::out); 
	if (!inOut) { 
		cerr << "Unable to open file!" << endl; 
		return EXIT_FAILURE; 
	}  
	ifstream::pos_type end_mark = inOut.tellg(); 
	inOut.seekg(0, fstream::beg); // reposition to start of the file 
	int cnt = 0; // accumulator for byte count 
	string line; // hold each line of input 
	while (inOut && inOut.tellg() != end_mark 
		&& getline(inOut, line)) 
	{ 
		cnt += line.size() + 1; // add 1 to account for the newline 
		ifstream::pos_type mark = inOut.tellg(); 
		inOut.seekp(0, fstream::end);// set write marker to end 
		inOut << cnt; // write the accumulated length 
		if (mark != end_mark) inOut << " "; 
		inOut.seekg(mark); // restore read position 
	} 
	inOut.clear(); // clear flags in case we hit an 
	inOut.seekp(0, fstream::end); 
	inOut << "\n"; 
	inOut.close();
	return 0; 


}


int main()  
{
	test();
	testCR();
	
	testIO("d:\\c.txt");
	testIO("d:\\d.txt");

	return 0; 
}
赵4老师 2013-08-12
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试是程序员必须掌握的技能之一。 不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实! 有人说一套做一套,你相信他说的还是相信他做的? 其实严格来说这个世界上古往今来所有人都是说一套做一套,不是吗? 不要写连自己也预测不了结果的代码!
u010258183 2013-08-12
  • 打赏
  • 举报
回复
引用 22 楼 lm_whales 的回复:
[quote=引用 21 楼 u010258183 的回复:] [quote=引用 20 楼 lm_whales 的回复:] [quote=引用 17 楼 u010258183 的回复:] [quote=引用 16 楼 lm_whales 的回复:] VC10 j后面无回车,输出如下: abcd efg hi j5 9 12 有回车,输出如下: abcd efg hi j 5 9 12 14 这里没有什么好说的。 有回车,最后一个字符是回车,输出的第一个数字在回车后面,其他各行,和回车符无关 。 没有回车,最后一个字符是j 连续输出 数字,由于最后一行包含数字,超过最开始的文件位置 j这一行,没有统计
我想问下这个代码里面的流,输入和输出是共用一个缓冲区吗,如果是getline函数存字符串的时候会留下换行在缓冲区吧,再inOut << cnt;代码执行,换行会一起输出吧,如果是这样那该怎么解释呢?[/quote] fstream 或者iostream 流对象内部的输入流和输出流,是不共用缓冲的,二者唯一的联系,打开了同一个文件。 fstream 和iostream这两个类,都是多继承的 每个fstream 和iostream 内部包含两个独立的流对象 istream,ostream 输入输出使用两套不同的缓冲。 不过可以设置缓冲,如果你设置成一个的话,那就会共用了。 [/quote] 使用既能读又能写的 fstream 类型和 stringstream 类型的时候,只有一 个保存数据的缓冲区和一个表示缓冲区中当前位置的标记。 摘自c++ primer 只有一个缓冲区难道书上说错了?每个io对象管理一个缓冲区,上面才一个io对象啊,哪里有2个缓冲区了?[/quote] 是吗,也许VC10 没有跟随标准?? 不过,个人认为,即使公用缓冲区,I,O两个父类对象,使用也不不一定会冲突,使用O对象时,可以放弃I对象正在使用的缓冲区中的内容的,使用I对象时候,可以刷新并放弃O缓冲区的内容。 fstream 类继承了istream和ostream 两个类,内部自然有两个I,O流对象。 [/quote] 如果像你这么说i,o2个交替使用前都清除缓冲的数据,那我在txt文本的最后一个字母j后面不换行不回车,输出结果为何会出现下面这种效果:5前面的换行符哪里来的? abcd efg hi j 5 9 12 14
lm_whales 2013-08-12
  • 打赏
  • 举报
回复
引用 21 楼 u010258183 的回复:
[quote=引用 20 楼 lm_whales 的回复:] [quote=引用 17 楼 u010258183 的回复:] [quote=引用 16 楼 lm_whales 的回复:] VC10 j后面无回车,输出如下: abcd efg hi j5 9 12 有回车,输出如下: abcd efg hi j 5 9 12 14 这里没有什么好说的。 有回车,最后一个字符是回车,输出的第一个数字在回车后面,其他各行,和回车符无关 。 没有回车,最后一个字符是j 连续输出 数字,由于最后一行包含数字,超过最开始的文件位置 j这一行,没有统计
我想问下这个代码里面的流,输入和输出是共用一个缓冲区吗,如果是getline函数存字符串的时候会留下换行在缓冲区吧,再inOut << cnt;代码执行,换行会一起输出吧,如果是这样那该怎么解释呢?[/quote] fstream 或者iostream 流对象内部的输入流和输出流,是不共用缓冲的,二者唯一的联系,打开了同一个文件。 fstream 和iostream这两个类,都是多继承的 每个fstream 和iostream 内部包含两个独立的流对象 istream,ostream 输入输出使用两套不同的缓冲。 不过可以设置缓冲,如果你设置成一个的话,那就会共用了。 [/quote] 使用既能读又能写的 fstream 类型和 stringstream 类型的时候,只有一 个保存数据的缓冲区和一个表示缓冲区中当前位置的标记。 摘自c++ primer 只有一个缓冲区难道书上说错了?每个io对象管理一个缓冲区,上面才一个io对象啊,哪里有2个缓冲区了?[/quote] 是吗,也许VC10 没有跟随标准?? 不过,个人认为,即使公用缓冲区,I,O两个父类对象,使用也不不一定会冲突,使用O对象时,可以放弃I对象正在使用的缓冲区中的内容的,使用I对象时候,可以刷新并放弃O缓冲区的内容。 fstream 类继承了istream和ostream 两个类,内部自然有两个I,O流对象。
u010258183 2013-08-12
  • 打赏
  • 举报
回复
引用 20 楼 lm_whales 的回复:
[quote=引用 17 楼 u010258183 的回复:] [quote=引用 16 楼 lm_whales 的回复:] VC10 j后面无回车,输出如下: abcd efg hi j5 9 12 有回车,输出如下: abcd efg hi j 5 9 12 14 这里没有什么好说的。 有回车,最后一个字符是回车,输出的第一个数字在回车后面,其他各行,和回车符无关 。 没有回车,最后一个字符是j 连续输出 数字,由于最后一行包含数字,超过最开始的文件位置 j这一行,没有统计
我想问下这个代码里面的流,输入和输出是共用一个缓冲区吗,如果是getline函数存字符串的时候会留下换行在缓冲区吧,再inOut << cnt;代码执行,换行会一起输出吧,如果是这样那该怎么解释呢?[/quote] fstream 或者iostream 流对象内部的输入流和输出流,是不共用缓冲的,二者唯一的联系,打开了同一个文件。 fstream 和iostream这两个类,都是多继承的 每个fstream 和iostream 内部包含两个独立的流对象 istream,ostream 输入输出使用两套不同的缓冲。 不过可以设置缓冲,如果你设置成一个的话,那就会共用了。 [/quote] 使用既能读又能写的 fstream 类型和 stringstream 类型的时候,只有一 个保存数据的缓冲区和一个表示缓冲区中当前位置的标记。 摘自c++ primer 只有一个缓冲区难道书上说错了?每个io对象管理一个缓冲区,上面才一个io对象啊,哪里有2个缓冲区了?
加载更多回复(11)

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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