Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I'm building a graphical program and i need to read a file to a string. The file contains simple HTML code.

Now,

If a create a blank project and add .c/.cpp file this code works fines, copying the whole file to the string.
C#
FILE *f;
int tamanho;
char *asd;

f=fopen("mail.txt","r");
if(f==NULL)
      erro("Erro abrir file");

fseek(f,0,SEEK_END);
tamanho=ftell(f);
rewind(f);
asd=(char *)malloc(tamanho+1);
fread(asd,1,tamanho,f);


Howerver if i create a windows form application and use the same code it only copies a few lines from the beggining.

I noticed that when using forms visual studio uses c++ compiler and when using blank project with .c files it uses a c compiler, i've tryed both compilers and it copies the string, the problem seem to when i use forms

Need help,
Posted
Updated 10-Dec-11 7:00am
v2
Comments
Richard MacCutchan 11-Dec-11 5:55am    
This has nothing to do with C, C++ or Forms, but rather to what is going on in your program after you have read the file. You need to step through your code with the debugger to check what data is being read and how you are processing it.

There is a minor issue with ftell; it is not guranteed to work ok for text files. See
ftell[^] for details.

Try getting the file size using GetFileSize or as recommend GetFileSizeEx (GetFileSizeEx[^], you will need a handle but i'm sure that's not going to be a problem.

Hope this helps,

Cheers, AT
 
Share this answer
 
v2
Open the file in binary mode.
Something like that:
C++
f=fopen("mail.txt","rb");
 
Share this answer
 
Comments
Cr4zYPT 11-Dec-11 7:49am    
I've tryed bynary and also didn't work
Humberto 11-Dec-11 14:26pm    
Try this modification to the read statement:

if ( fread(asd,1,tamanho,f) != tamanho) {
free(asd);
fclose(f);
erro("Erro de leitura");
}

and see if it calls erro().
I just realised this is actually a continuation of the problem you reported here[^], about which I made some suggestions - did you follow them, or indeed check exactly what data you are reading into your code?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900