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;
}
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.
should get one less character, or make the array one longer, otherwise you're overwriting the null.
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.