PDA

Click to See Complete Forum and Search --> : Loading a file


ChimpFace9000
Apr 22nd, 2001, 02:18 AM
Could someone show me how to load a file into an array?

Vlatko
Apr 22nd, 2001, 04:22 AM
If you mean every char of a file in array then maybe something like this.

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

ChimpFace9000
Apr 22nd, 2001, 02:49 PM
Im sorry, i didnt explain enough, i meant with the Win32 API.

parksie
Apr 22nd, 2001, 03:02 PM
Does it really make much difference how you do it?

However, see www.winprog.org/tutorial for the code to load from a file.

ChimpFace9000
Apr 22nd, 2001, 04:50 PM
Yes, it does.

parksie
Apr 22nd, 2001, 05:37 PM
Okay...just out of interest...why?

ChimpFace9000
Apr 22nd, 2001, 05:40 PM
I write my programs in C, and when they are all done, i put them into asm. So the function cant be language specific.

parksie
Apr 22nd, 2001, 05:45 PM
Fair enough :) I just assumed that since you were using C the standard library would be enough ;)

MPrestonf12
Apr 23rd, 2001, 06:21 PM
You can use FindFile and FindNext file functions. I made a project doing that ill try and go find it.:)

MPrestonf12
Apr 23rd, 2001, 06:30 PM
this may be what you wanted..:confused:

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