• Welcome to Valhalla Legends Archive.
 

sorry for the newb question

Started by SubLiminaL_WolF, April 30, 2003, 10:02 PM

Previous topic - Next topic

SubLiminaL_WolF

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

Arta

#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.

iago

should get one less character, or make the array one longer, otherwise you're overwriting the null.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Skywing

#3
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.