Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: yrinfish on August 18, 2011, 03:09:10 am
-
I want to be able to do this on a file:
myChar = getTheNthCharOrSomething(9);
that for example in pseudo-code, getTheNthCharOrSomething() is this:
char getTheNthCharOrSomething(where) {
return file.charAt(where);
}
file is a global var.
-
Hmmm, good question. You would open a file, copy its contents in a string, then run a substr (http://www.cplusplus.com/reference/string/string/substr/) on it.
-
and how to copy it in a string?
-
Use fgets.
http://www.cplusplus.com/reference/clibrary/cstdio/fgets/
-
But I need whatever comes after a newline too
-
Ah, nevermind, I found a more efficient way. Use fseek (http://www.cplusplus.com/reference/clibrary/cstdio/fseek/) to seek to the nth character, then fgetc (http://www.cplusplus.com/reference/clibrary/cstdio/fgetc/) to get that character.