【发布时间】:2011-05-30 23:51:44
【问题描述】:
我正在尝试从一个简单的文本文件中读取。每当我运行程序时,它都会打印出文件, 但不是在打印出一堆乱码之前。有什么建议吗?
乱码:
Φ#· ├ï Uï∞ Φ╖ ≈╪←└≈╪YH]├ 5tδ╝
abc
缓冲区.cc
//-----------------------------------------------------
// TextInputBuffer - Constructor for TextInputBuffer.
//-----------------------------------------------------
TextInputBuffer::TextInputBuffer(char *InputFileName)
{
//--Open file. Abort if failed.
InputFile.open(InputFileName, std::ios::in);
if (!InputFile.good()) exit(1);
}
//-----------------------------------------------------
// GetNextLine - Get next line from input file.
//
// Return: The first character of the next line.
//-----------------------------------------------------
char TextInputBuffer::GetNextLine()
{
//--Get next line from input file.
if (InputFile.eof()) *ptrChar = eofChar;
else
{
InputFile.getline(Text, MaxInputBufferSize);
ptrChar = Text;
}
return *ptrChar;
}
//-----------------------------------------------------
// GetNextChar - Get next character from the text
// buffer.
//
// Return: The next character in the text buffer.
//-----------------------------------------------------
char TextInputBuffer::GetNextChar()
{
char ch;
if (*ptrChar == eofChar) ch = eofChar;
else if (*ptrChar == eolChar) ch = GetNextLine();
else
{
++ptrChar;
ch = *ptrChar;
}
return ch;
}
列表.cc
TextInputBuffer InputBuffer(argv[1]);
char ch;
do {
ch = InputBuffer.GetNextChar();
if (ch == eolChar)
std::cout << std::endl;
std::cout << ch;
} while (ch != eofChar);
【问题讨论】:
-
您可以尝试使用非常低级的文本编辑器或对文件的前几个长字进行十六进制转储,以尝试匹配“一堆乱码”。
-
不,它不够低级,因为它很容易被不同的字符编码混淆(缺少 BOM 尤其成问题)。使用十六进制编辑器。
-
我刚做了,只有 616263(又名 abc)。我也认为 MRAB 是对的。
标签: c++