[英]Read first 1000 characters of file into a buffer, turn newlines into \0 and store the strings address location to a char*[]
Here is the exact question: 这是确切的问题:
Write a program that reads lines of text and appends them to a char buffer[1000].
编写一个程序,读取文本行并将其附加到
char buffer[1000].
Stop after reading 1,000 characters. 读取1,000个字符后停止。 As you read in the text, replace all newlines characters with null terminators.
在阅读文本时,请将所有换行符替换为空终止符。 Establish an array
char* lines[100]
so that the pointers in that array point to the beginnings of the lines in the text. 建立一个数组
char* lines[100]
以便该数组中的指针指向文本中各行的开头。
Only consider 100 input lines if the input has more lines. 如果输入有更多行,则仅考虑100条输入行。 Then display the lines in reverse order.
然后以相反的顺序显示行。 Start with the last input line.
从最后的输入行开始。
http://pastebin.com/uKv4nC5s http://pastebin.com/uKv4nC5s
// see updated code
The above works perfectly when used with the Data.txt file specified above; 与上面指定的Data.txt文件一起使用时,上面的方法可以完美地工作; however, it FAILS miserably when the file contains > 100 lines of text.
但是,当文件包含> 100行文本时,它将失败。
When it reads back the contents in reverse the first line printed is EVERYTHING after the 100th line. 当它反向读取内容时,打印的第一行在第100行之后是一切。
Consider we had the following last four lines 考虑我们有以下最后四行
99 ASDF
100 FFFF
101 3910
102 FKDS
The above would print 上面会打印
FFFF // GOOD
3910 // NOT GOOD
FKDS // NOT GOOD PART DEUX
ASDF // GOOD
I believe the desired output to be that it ignore everything after the 100th newline and or 1000th character, whichever comes first. 我相信期望的输出是它忽略第100个换行符和/或第1000个字符之后的所有内容,以先到者为准。
Any pointers or guidance in the right direction would be greatly appreciated. 朝正确方向的任何指示或指导将不胜感激。 This is homework so no explicit answers if possible.
这是家庭作业,因此,如果可能,没有明确的答案。
void read_and_print()
{
std::ifstream input("D:\\data.txt");
if (!input.is_open() || !input.good())
return;
char buffer[10001];
int b = 0;
char* lines[100];
int l = 0;
char** e;
bool set_start = false;
lines[l] = buffer + b; // the first line
e = lines + (l++);
char t;
while (input.get(t) && b < 1000)
{
if (set_start)
{
if (l < 100) {
lines[l] = buffer + b;
e = lines + l;
}
l++;
set_start = false;
}
if (t == '\n') {
t = '\0';
set_start = !set_start;
}
buffer[b] = t;
b++;
}
input.close();
if (b == 1000)
buffer[1000] = '\0'; // close out the string
else if (b < 1000)
buffer[b] = '\0'; // close out the string
while (lines <= e)
{
std::cout << *e << std::endl;
e--;
}
}
Your loop doesn't stop when you've read in 100 lines; 读完100行后,循环不会停止; rather, it continues to read but stop replacing
\\n
with \\0
. 而是继续读取,但不再将
\\n
替换为\\0
。 The result is that your last "line" contains everything from the 100th line onwards in your input until you hit the 1000 character mark. 结果是您的最后一个“行”包含从输入中的第100行到击中1000个字符标记为止的所有内容。
You should terminate the loop when you hit the \\n
for the 100th line. 在第100行中按
\\n
时,应终止循环。
Side notes: 旁注:
(t = input.get()) != EOF
isn't correct when t
is a char
( EOF
is an int
value guaranteed to be unequal to any char
converted to int
; it's not guaranteed to be unequal to a valid character when converted to a char
). (t = input.get()) != EOF
当t
是一个char
时(t = input.get()) != EOF
是不正确的( EOF
是一个int
值,它保证不等于转换为int
任何char
;转换时不保证它不等于有效字符到一个char
)。 A simple input.get(t)
would do it (it takes its argument by reference and returns a reference to the stream, whose operator bool
will return false
if an error occurred). 一个简单的
input.get(t)
就能做到(它通过引用获取其参数,并返回对该流的引用,如果发生错误,其operator bool
将返回false
)。
If you've read in 1000 characters, and the last character is not \\n
, the last string never gets closed with \\0
. 如果您读了1000个字符,而最后一个字符不是
\\n
,则最后一个字符串永远不会以\\0
。 You should probably add an extra character to the buffer so that you can close that string up with \\0
. 您可能应该在缓冲区中添加一个额外的字符,以便可以使用
\\0
结束该字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.