使用Visual C ++格式读取文件时出现问题 [英] Problem reading file using Visual C++ FOrms

查看:59
本文介绍了使用Visual C ++格式读取文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在构建一个图形程序,我需要将文件读取为字符串.该文件包含简单的HTML代码.

现在,

如果创建一个空白项目并添加.c/.cpp文件,则此代码可以正常工作,将整个文件复制到字符串中.

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.

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



但是,如果我创建一个Windows窗体应用程序并使用相同的代码,它只会从开始复制几行.

我注意到,当使用表格Visual Studio使用c ++编译器,并且将空白项目与.c文件一起使用时,它使用c编译器,我已经尝试了两个编译器并复制了字符串,问题似乎是当我使用表格时

需要帮助,



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,

推荐答案

ftell有一个小问题;它不保证文本文件正常工作.参见
ftell [有关详细信息,请 ^ ].

尝试使用GetFileSize或按照建议的GetFileSizeEx获取文件大小( GetFileSizeEx [
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


以二进制模式打开文件.
像这样的东西:
Open the file in binary mode.
Something like that:
f=fopen("mail.txt","rb");


我刚刚意识到这实际上是您在此处 [
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?


这篇关于使用Visual C ++格式读取文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆