64,639
社区成员
发帖
与我相关
我的任务
分享
#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;
}
不要使用
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;
//...
}
类似的例子还可以举很多。
getline只是一行一行的读取文件
你写文件的那些只是在文件尾以数字空格的格式写的
在文件中5换行不换行只和文件尾部有关
换行了说明文件之前在j后面之前就有回车换行
不换行说明文件尾没有回车
#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;
}