I had a strange bug with file access (windows) in my program, where some file index calculations would not point to the correct symbol in the file. I could isolate the error the seek incrementation behavior and tested it in two loops. Once in the C and C++ way.
In both, we open a file and then print each character together with its string representation. I wrote a function to_string(char) that prints the character or its name it's a whitespace or something invisible.
First the C way:
FILE* fptr = fopen("test.txt", "r");
while(!feof(fptr)){
int seek = ftell(fptr);
char c = fgetc(fptr);
const char* s = to_string(c);
printf("seek: %d, char: %s\n", seek, s); //Edit output is now in C not in C++
}
fclose(fptr);
In the following output, you will notice that the seek jumps at every 'LF' so that some seeks are skipped. So the seek 19, 34, 37 and 39 somehow do not appear. I also tried using the fread() function instead of fgetc() but the output does not change.
Output:
seek: 0, char: f
seek: 1, char: n
seek: 2, char: 'SP'
seek: 3, char: m
seek: 4, char: a
seek: 5, char: i
seek: 6, char: n
seek: 7, char: 'SP'
seek: 8, char: -
seek: 9, char: >
seek: 10, char: 'SP'
seek: 11, char: a
seek: 12, char: s
seek: 13, char: c
seek: 14, char: i
seek: 15, char: i
seek: 16, char: 'SP'
seek: 17, char: {
seek: 18, char: 'LF' <---
seek: 20, char: 'SP' <--- what is this
seek: 21, char: 'SP'
seek: 22, char: 'SP'
seek: 23, char: 'SP'
seek: 24, char: r
seek: 25, char: e
seek: 26, char: t
seek: 27, char: u
seek: 28, char: r
seek: 29, char: n
seek: 30, char: 'SP'
seek: 31, char: 1
seek: 32, char: ;
seek: 33, char: 'LF' <---
seek: 35, char: } <---
seek: 36, char: 'LF' <---
seek: 38, char: 'LF' <---
seek: 40, char: 'EOF'
Now the C++ way:
std::ifstream file("test.txt");
while(!file.eof()){
int seek = file.tellg();
char c = file.get();
std::cout << "seek: " << std::to_string(seek) << ", char: " << to_string(c) << std::endl;
}
In the following output, you will see that the seek will not jump by two at every occurence of LF but from the first to the second element the seek randomly jumps by 5 instead of incrementing by one.
Output
seek: 0, char: f <---
seek: 5, char: n <--- why?
seek: 6, char: 'SP'
seek: 7, char: m
seek: 8, char: a
seek: 9, char: i
seek: 10, char: n
seek: 11, char: 'SP'
seek: 12, char: -
seek: 13, char: >
seek: 14, char: 'SP'
seek: 15, char: a
seek: 16, char: s
seek: 17, char: c
seek: 18, char: i
seek: 19, char: i
seek: 20, char: 'SP'
seek: 21, char: {
seek: 22, char: 'LF'
seek: 23, char: 'SP'
seek: 24, char: 'SP'
seek: 25, char: 'SP'
seek: 26, char: 'SP'
seek: 27, char: r
seek: 28, char: e
seek: 29, char: t
seek: 30, char: u
seek: 31, char: r
seek: 32, char: n
seek: 33, char: 'SP'
seek: 34, char: 1
seek: 35, char: ;
seek: 36, char: 'LF'
seek: 37, char: }
seek: 38, char: 'LF'
seek: 39, char: 'LF'
seek: 40, char: 'EOF'
Do you have a clue on why those file streams behave so strangely?
Thanks for your help.