-
strcat error
Code:
int filelen;
filelen = fseek(fp,0,SEEK_END);
char buffer[sizeof(filelen)];
rewind(fp);
char ch;
ch = getc(fp);
while(ch!=EOF){
strcat(buffer,ch);
ch = getc(fp);
}
error C2664: 'strcat' : cannot convert parameter 1 from 'char (*)[4]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
anyway i don't understand what the problem here is, any ideas?
-
You are trying to put a char array of 4 in to a single char. Make char ch; char ch*;
-
sizeof(filelen) returns 4 because it's the size of the variable, not the size of the file.
Use:
Code:
char *buffer = new char[filelen];
You'd have to delete[] it afterwards though:
Code:
int filelen;
filelen = fseek(fp,0,SEEK_END);
char *buffer = new char[filelen];
buffer[0] = 0;
rewind(fp);
char ch;
ch = getc(fp);
while(ch != EOF){
strcat(buffer,ch);
ch = getc(fp);
}
Also, can't you just use fread?
-
I could use fread, but still I never know how big the 2nd argument should be.
-
size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
Code:
MYSTRUCT toread;
fread(&toread, sizeof(toread), 1, fp);
-
what is the MYSTRUCT used for?
-
It's just a type placeholder. It signifies that you put the size of the item you are reading, and the 3rd argument is the number of items of that size.
-
But when you compile it expects a structure or at least needs one. Anyway here what I have
Code:
FILE *fp;
fp = fopen("C:/Matt/files.txt", "rb");
//int filelen;
//filelen = fseek(fp,0,SEEK_END);
MYSTRUCT toread;
fread(&toread,sizeof(toread),1,fp);
SendMessage(editbx,WM_SETTEXT,0,(LPARAM)toread);
by-the-way thanks alot for your help!
-
No no no...change MYSTRUCT to whatever you're reading from the file.