Could someone show me how to load a file into an array?
Printable View
Could someone show me how to load a file into an array?
If you mean every char of a file in array then maybe something like this.
Code:#include <stdio.h>
FILE *f;
int i = 0;
f = fopen("c:\\textfile","r");
char c;
fseek(f,0,SEEK_END);
int filelen = ftell(f);
char *filechar= new char[filelen];
while((c = getc(f)) != EOF)
{
i++;
ar[i] = ftell(f);
}
fclose(f);
Im sorry, i didnt explain enough, i meant with the Win32 API.
Does it really make much difference how you do it?
However, see www.winprog.org/tutorial for the code to load from a file.
Yes, it does.
Okay...just out of interest...why?
I write my programs in C, and when they are all done, i put them into asm. So the function cant be language specific.
Fair enough :) I just assumed that since you were using C the standard library would be enough ;)
You can use FindFile and FindNext file functions. I made a project doing that ill try and go find it.:)
this may be what you wanted..:confused:
Code:int findfiles(char *p)
{
WIN32_FIND_DATA Find;
HANDLE hfind;
BOOL hfind2;
unsigned int i=0;
char *dirname[i];
hfind = FindFirstFile(p, &Find);
//MessageBox (NULL, Find.cFileName, "first file", 0);
if(hfind == INVALID_HANDLE_VALUE){
MessageBox(NULL, "Error opening file", "FindFiles", MB_OK|MB_ICONERROR);
} else
SendMessage(NULL, LB_ADDSTRING, 0, (LPARAM) Find.cFileName);
do{
hfind2 = FindNextFile(hfind, &Find);
dirname[i] = new char[strlen(Find.cFileName)] ;
dirname[i] = Find.cFileName;
SendMessage(listbx,LB_ADDSTRING,0,(LPARAM) Find.cFileName);
}while(hfind2!=0);
FindClose(hfind);
for(i=i;i>0;i--)
delete[] dirname[i];
return 0;
}