C++;文件输入和输出:无法输入 下面的C++代码是读取一个TXT文件,然后在每个行中写出字符数和TXT文件中所有字符的数目。我使用MS VisualStudio 2008。但是有些东西是错误的。只有所有字符的数量都输入到TXT文件中,但是每行的数字都没有输入到txt文件中。现在我想不出来了。有人能给我一些建议吗?非常感谢 和我的另一个问题是,我应该如何对待在TXT文件的中间插入什么东西? < /P>
这是我的代码:C++;文件输入和输出:无法输入 下面的C++代码是读取一个TXT文件,然后在每个行中写出字符数和TXT文件中所有字符的数目。我使用MS VisualStudio 2008。但是有些东西是错误的。只有所有字符的数量都输入到TXT文件中,但是每行的数字都没有输入到txt文件中。现在我想不出来了。有人能给我一些建议吗?非常感谢 和我的另一个问题是,我应该如何对待在TXT文件的中间插入什么东西? < /P>,c++,io,C++,Io,这是我的代码: #include <iostream> #include <fstream> using namespace std; int main() { fstream inOut("copy.txt",ios::in | ios::out | ios::app); if (!inOut) { cerr << "ERROR:cannot open file!" << endl; return -1; } int cn
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream inOut("copy.txt",ios::in | ios::out | ios::app);
if (!inOut)
{
cerr << "ERROR:cannot open file!" << endl;
return -1;
}
int cnt = 0;
char ch;
inOut.seekg(0);
while(inOut.get(ch))
{
cout.put(ch);
++cnt;
if ('\n' == ch)
{
ios::pos_type mark = inOut.tellg();
if (!inOut)
{
cerr << "ERROR!" << endl;
return -1;
}
inOut << cnt;
inOut.put(' ');
inOut.seekg(mark);
}
}
inOut.clear();
inOut << cnt << endl;
cout << "[" << cnt << "]" << endl;
}
#包括
#包括
使用名称空间std;
int main()
{
fstream inOut(“copy.txt”,ios::in | ios::out | ios::app);
如果(!inOut)
{
CERR 我不太了解fstream,但我认为你想改变你做这件事的方式。你可以把一个文件看作是一个连续的内存块。追加到结尾是很容易的,但是在中间插入可能是有问题的。特别是,如果你确实插入了一些东西,那么你的<代码> Sigkg可能无效。
我建议采取三种战略:
了解当前正在发生的事情(尝试在seekg
之前关闭文件,并查看inOut是否写入了任何内容。我不知道您为什么选择std::fstream作为工具。如果std::fstream未被要求或没有必要,我想提供一个示例来解决您的问题。以下是一些限制:
a、 我在一次ReadFile操作中将所有文件读入内存。如果文件很大,可以用循环替换。
b、 我想您的行分隔符是“\n”。
c、 我在“\n”之前添加了[line\u count],看起来更好
这是我的密码
#include "windows.h"
#include "sstream"
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hFile = ::CreateFile(L"C:\\Users\\wujian\\Desktop\\pingback - Copy.log", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE )
{
DWORD dwSize = ::GetFileSize(hFile, NULL);
if (dwSize)
{
char *pBuffer = new char[dwSize];
DWORD dwRead = 0;
::ReadFile(hFile, pBuffer, dwSize, &dwRead, NULL);
if (dwRead == dwSize)
{
std::stringstream ss;
int iPos = 0;
int iLine = 0;
while (iPos < dwSize)
{
if (pBuffer[iPos] == '\n')
{
ss << '[' << iLine << ']';
iLine = 0;
}
ss << pBuffer[iPos];
iPos ++, iLine ++;
}
ss << '[' << dwSize << ']';
::SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
DWORD dwWrite = 0;
::WriteFile(hFile, ss.str().c_str(), ss.str().length(), &dwWrite, NULL;
}
::CloseHandle(hFile);
}
}
return 0;
}
#包括“windows.h”
#包括“sstream”
int _tmain(int argc,_TCHAR*argv[]
{
HANDLE hFile=::CreateFile(L“C:\\Users\\wujian\\Desktop\\pingback-Copy.log”,一般读取,一般写入,0,NULL,打开,文件属性正常,NULL);
if(hFile!=无效的句柄值)
{
DWORD dwSize=::GetFileSize(hFile,NULL);
如果(dwSize)
{
char*pBuffer=新字符[dwSize];
DWORD dwRead=0;
::ReadFile(hFile、pBuffer、dwSize和dwRead,NULL);
if(dwRead==dwSize)
{
std::stringstream-ss;
int iPos=0;
内线=0;
而(首次公开募股规模#包括
#包括
使用名称空间std;
int main()
{
fstream inOut(“copy.txt”,ios::in | ios::out | ios::app);
如果(!inOut)
{
cerr空格也算作字符吗?是的~我把空格算作字符。也许你的行尾不是换行符\n而是返回字符\r?你使用什么编译器?我使用MS Visual Studio 2008,但我无法得到你的结果~~我使用vc6.0,我试过使用MS Visual Studio 2008,但我遇到了与你相同的问题。我知道为什么我会重新编辑我的答案。请看,我可以用这种方式来解决这个问题。但是如果我想在TXT文件的中间插入什么东西,我该怎么办?你知道怎么做吗?哦,你不能在TXT文件的中间插入一些东西而不必重写日期BIDET。如果你想,你可以把文件日期复制到缓冲区或FrAct中。将日期复制并重写到新文件中,然后删除旧文件
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream inOut("copy.txt",ios::in | ios::out | ios::app);
if (!inOut)
{
cerr << "ERROR:cannot open file!" << endl;
return -1;
}
int cnt = 0;
int cntline=0;
char ch;
inOut.seekg(0);
while(inOut.get(ch))
{
cout.put(ch);
//++cnt it include '\n' it should put behind of "if"
if ('\n' == ch)
{ ios::pos_type mark = inOut.tellg();
if (!inOut)
{
cerr << "ERROR!" << endl;
return -1;
}
cntline=cnt-cntline; //add it to account the number of chars of each line
inOut.seekg(0,ios::end); //here!!!!!!!!!!!!!!!!!!!!!!!!!!! add this , the resault is finally right! the reason it's down here.
inOut << cntline;
cntline=cnt; // and here! it can assure the number of the chars of each line be right!
inOut.put(' ');
inOut.seekg(mark);
}
else
{
++cnt; // it's here!
}
}
inOut.clear();
inOut << cnt << endl;
cout << "[" << cnt << "]" << endl;
}