`
wesleysong
  • 浏览: 21875 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
社区版块
存档分类
最新评论

c++ CGI端大文件上传读取不完整之解决方案

    博客分类:
  • life
阅读更多
虽说 getenv("CONTENT_LENGTH") 之后可以直接对stdin使用fread()
但是在实际应用时发现,有时候文件读不全

在网上搜索了半天,最后发现,要使用二进制模式来读取,而不是文本模式…

读取之前,用如下语句设置二进制模式:
_setmode(_fileno(stdin),_O_BINARY);


读取结束之后,用如下语句设回文本模式:
_setmode(_fileno(stdin),_O_TEXT);



附,二进制模式和文本模式的区别:
转自http://captain.blog.edu.cn/2006/512355.html
The Difference Between Binary and Text Files

When you access a file from within C or C++ you have a choice between treating the file as a binary file or as a text file.

C uses the fopen(file,mode) statement to open a file and the mode identifies whether you are opening the file to read, write, or append and also whether the file is to be opened in binary or text mode.

C++ opens a file by linking it to a stream so you don't specify whether the file is to be opened in binary or text mode on the open statement. Instead the method that you use to read and/or write to the file determines which mode you are using. If you use the << operator to read from the file and the >> operator to write to the file then the file will be accessed in text mode. If instead you use the put() and get() or read() and write() functions then the file will be accessed in binary mode.

So what exactly is the difference between text and binary modes? Well the difference is that text files contain lines (or records) of text and each of these has an end-of-line marker automatically appended to the end of it whenever you indicate that you have reached the end of a line. There is an end of line at the end of the text written with the C fwrite() function or in C++ when you <<endl. Binary files are not broken up into separate lines or records so the end-of line marker is not written when writing to a binary file.

Reading from a text file or binary file is different too as a text file is automatically broken up into separate records as it read in based on the location of the end-of-line markers.

So what is this end-of-line marker? Well that depends on the operating system that you are using. The Apple Macintosh computers use a single carriage return as the end-of-line marker (x'0D') while Unix based operating systems including Linux use a single line-feed character (x'0A'). Most PC based systems including DOS, all versions of windows, and OS/2 use a carriage return/line feed combination (x'0D0A') as the end-of-line marker. C and C++ terminate strings with a low value character (x'00').

So what happens when we read from a text file is that the end-of-line character for the operating system that we are using gets converted into a low value end-of-string indicator and when we write to a file the appropriate end-of-line character(s) get written when we indicate the end of the line. This makes the reading and writing of text files much easier because the appropriate end-of-line markers are handled for us.

With a binary file none of these conversions take place. When we read a binary file the end-of-line characters for our operating system will be read into the string and treated no different than any other character. When we write to a binary file the only end-of-line markers that are written will be those that we code into the output ourselves and hence will be exactly as we code it regardless of the operating system that we are running on. This makes it much easier for us when the file does not contain straight text and the end-of-line marker does not separate lines of text but rather appears as part of the non-text data content of the file.

A binary file can contain text but the text that it contains is not considered to be broken up into a number of lines by the occurrence of end-of-line markers. A binary file may alternatively contain information that contains no text whatsoever. It is up to the program reading the file to make sense of the data contained in a binary file and convert it into something meaningful (eg. an image or a series of fixed length records).


分享到:
评论
1 楼 ludatong110 2011-01-28  
这个虽然不懂,我还没有入门,不过以后工作会碰到这个问题,还是记住吧。。

相关推荐

    CGI文件上传

    在实际开发中,考虑到性能和安全,现代Web开发更倾向于使用更高效的技术如PHP-FPM、FastCGI或者框架内的文件上传处理机制,但CGI作为早期的解决方案,仍然是理解Web服务器动态交互的一个基础概念。

    qt访问cgi的方法与cgi实例

    随着技术的发展,虽然现在有更现代的解决方案如 PHP、Node.js 或 Python 的 Flask、Django 框架,但 CGI 仍然在某些场景下发挥着作用,而 Qt 作为一个强大的开发工具,依然能与之完美配合。在后续的内容中,我们将...

    cgi-util-2.2.0.rar_CGI UT_CGI linux_cgi_util-cgi_util_cgi

    标签中的“cgi_linux”表明这是一个针对Linux操作系统的CGI解决方案。Linux是许多Web服务器的首选操作系统,因为它稳定、开源且支持多任务。CGI在Linux上运行,可以通过系统调用和标准输入/输出与Web服务器进行通信...

    vc开发CGI 快速入门

    使用VC开发CGI程序,结合C++的强类型和面向对象特性,可以创建高效、可维护的CGI解决方案。通过理解CGI的基本原理和VC的项目配置,开发者可以构建出满足各种需求的Web服务应用程序。实践过程中,配合书中给出的示例...

    轻量级C++实现的httpserver和httpclient

    然而,需要注意的是,轻量级的解决方案可能在功能和灵活性上有所妥协,可能不支持某些高级特性,如WebSocket、HTTP/2或其他高级安全特性。在选择库时,应根据项目的具体需求进行权衡。 总的来说,"轻量级C++实现的...

    Cgi编程傻瓜书(教你更好的编写cgi函数)

    虽然CGI在早期Web开发中广泛应用,但随着技术发展,如PHP、ASP.NET、Node.js等脚本语言或框架提供了更高效、易用的解决方案。它们通常内置了CGI的功能,但使用更高级的接口和方法。 通过学习本书,你将掌握CGI的...

    CGI开发相关文档资源,学习用

    这个压缩包可能包含13份不同的CGI相关文档,这些文档可能涉及CGI的各个方面,如安全考虑、性能优化、错误处理和常见问题解决方案。每份文档可能针对CGI开发中的特定主题,帮助开发者解决实际问题。 5. ...

    cgi简明教程(全)

    9. **学习资源**:CGI简明教程全集可能是对CGI编程的综合介绍,包括基本概念、编程实例和常见问题解决方法,对于初学者来说是一份很好的学习资料。 总的来说,CGI是Web发展早期的关键技术,尽管现在已经被其他更...

    cgi开发资料

    如今,开发者更多地选择更高效、更安全的解决方案,如PHP、ASP.NET等,这些技术在CGI的基础上进行了优化,更适合大规模的Web应用。然而,CGI的简单性和灵活性仍使其在某些特定场景下具有价值,如简单的脚本任务或...

    CGI技术全面接触 (PDG).rar

    7. **常见问题与解决策略**:列出CGI开发中常见的问题和解决方法,帮助读者在遇到困难时能迅速找到解决方案。 学习完本教程后,读者应能熟练地利用CGI技术开发动态网站,理解其工作原理,并具备解决实际问题的能力...

    计算机软件-编程源码-CGI脚本入门学习.zip

    CGI程序可以由各种编程语言编写,如Perl、C、C++、Python等。 **CGI的应用场景** CGI广泛应用于表单数据处理、用户登录验证、数据库查询、动态网页生成等场景。例如,你可以创建一个CGI程序来接收用户在网站上填写...

    Linux下 基于Boa的应用资源管理系统 SMS.zip

    在Linux环境中,基于Boa的应用资源管理系统SMS是一个轻量级的服务器端解决方案,它主要用于管理和控制系统的各种资源,如文件、内存、CPU使用率等。Boa是一个简单但功能强大的Web服务器,它允许用户通过Web界面来...

    Python Cookbook

    14.3 用CGI上传文件 507 14.4 检查web页面的存在 509 14.5 通过HTTP检查内容类型 510 14.6 续传HTTP下载文件 512 14.7 抓取Web页面时处理Cookie 513 14.8 通过带身份验证的代理进行HTTPS导航 516 14.9 用...

    cpp-Vino是一个轻量级和高效率的Web服务器

    Vino的设计目标是提供一个快速响应、低资源占用的解决方案。这主要体现在以下几个方面: 1. **事件驱动模型**:Vino采用非阻塞I/O的事件驱动模型,如Epoll或Kqueue,使得服务器能够处理大量并发连接。这种模型允许...

    Ehs版本http server开源代码

    Ehs版本的HTTP服务器是一个基于C++编写的开源项目,其设计目的是为了提供一个轻量级、高效且功能完备的Web服务器解决方案。Ehs HTTP Server的实现深入理解了HTTP协议,包括HTTP/1.1规范,能够处理各种HTTP请求方法...

    Win2003+iis+php5.3.2+mysql安装配置教程图解[定义].pdf

    解决方案是下载并安装此包,下载地址为 `http://www.microsoft.com/downloads/details.aspx?familyid=9B2DA534-3E03-4391-8A4D-074B9F2BC1BF&displaylang=en`。 #### 五、MySQL安装与配置 虽然原文档未涉及MySQL的...

    超级有影响力霸气的Java面试题大全文档

     forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容是从哪儿来的,所以它的地址栏中还是原来的地址。...

Global site tag (gtag.js) - Google Analytics