Valhalla Legends Archive

Programming => Battle.net Bot Development => Topic started by: SubLiminaL_WolF on April 30, 2003, 10:02 PM

Title: sorry for the newb question
Post by: SubLiminaL_WolF on April 30, 2003, 10:02 PM
How do I get the program to read a file line and input it as a string for username

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;
}
Title: Re:sorry for the newb question
Post by: Arta on April 30, 2003, 10:17 PM
Untested, uncompiled, etc:


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);
}


Username now contains the first line of the file - or the first 16 bytes of it, whichever is shorter.
Title: Re:sorry for the newb question
Post by: iago on May 01, 2003, 12:56 AM
should get one less character, or make the array one longer, otherwise you're overwriting the null.
Title: Re:sorry for the newb question
Post by: Skywing on May 01, 2003, 07:39 AM
Quote from: iago on May 01, 2003, 12:56 AM
should get one less character, or make the array one longer, otherwise you're overwriting the null.
You might want to strip the newline off too - one easy way is with a strtok(line "\r\n") call after fgets.