Valhalla Legends Forums Archive | Battle.net Bot Development | sorry for the newb question

AuthorMessageTime
SubLiminaL_WolF
How do I get the program to read a file line and input it as a string for username
[code]
int __cdecl BnBot::LogRead(char *lpszFmt, ...) {
   FILE *setup;
va_list argptr;
char szInStr[1024];
va_start(argptr, lpszFmt);
vsprintf(szInStr, lpszFmt, argptr);
va_end(argptr);
setup = fopen(lpszFmt, "st");

if (!setup){
    printf("Unable to find file");
    return 0;
}
return 1;
}
[/code]
May 1, 2003, 3:02 AM
Arta
Untested, uncompiled, etc:

[code]
FILE* SomeFile = fopen("TheFile.txt", "r");
if(!SomeFile){
// couldn't open the file
}

char Username[16] = {0};
// Make sure the file isn't empty
if(!feof(SomeFile)){
// Read the first line
fgets((char*)&Username, 16, SomeFile);
}
[/code]

Username now contains the first line of the file - or the first 16 bytes of it, whichever is shorter.
May 1, 2003, 3:17 AM
iago
should get one less character, or make the array one longer, otherwise you're overwriting the null.
May 1, 2003, 5:56 AM
Skywing
[quote author=iago link=board=17;threadid=1199;start=0#msg8913 date=1051768567]
should get one less character, or make the array one longer, otherwise you're overwriting the null.
[/quote]You might want to strip the newline off too - one easy way is with a strtok(line "\r\n") call after fgets.
May 1, 2003, 12:39 PM

Search