Can someone please explain to me what I'm doing wrong. The output is clearly wrong. The first value in the array is 73, therefore I don't think it's seeking to the beginning of the file properly, which would explain why the rest of the values are -1.
The file just contains 10 words on separate lines of various lengths.
Code:
int Process_File(arguments file_args) {
ifstream fd(file_args.file, ios::in); // file_args.file = file path
if (!fd) {
printf("File open error.\n");
return 0;
}
char tmp[50];
int lines;
while (fd) {
fd.getline(tmp, 50);
if (fd)
lines++;
}
printf("There are: %d lines.\n", lines);
int * offsets = new int[lines];
fd.seekg(0, ios_base::beg);
// Supposed to store the character positions of each new line in an array
// Array index represents the line number, value represents character position
for (int i = 0; i <= lines; i++) {
offsets[i] = fd.tellg();
fd.getline(tmp, 50);
}
for (int i = 0; i <= lines; i++)
printf("line: %d, character: %d\n", i, offsets[i]);
return 0;
}
Code:
Output:
There are: 10 lines.
line: 0, character: 73
line: 1, character: -1
line: 2, character: -1
line: 3, character: -1
line: 4, character: -1
line: 5, character: -1
line: 6, character: -1
line: 7, character: -1
line: 8, character: -1
line: 9, character: -1
line: 10, character: -1