Flex读取txt文件中的内容(二)

本文介绍了一个使用Flex技术读取TXT文件的应用实例。通过LoadTxt-app.xml配置文件,详细展示了如何定义应用程序的基本属性,包括标识符、名称、版本等,并说明了初始窗口设置的相关参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flex读取txt文件中的内容


自动生成的文件

LoadTxt-app.xml:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<application xmlns="http://ns.adobe.com/air/application/1.5.3">

<!-- Adobe AIR Application Descriptor File Template.

	Specifies parameters for identifying, installing, and launching AIR applications.

	xmlns - The Adobe AIR namespace: http://ns.adobe.com/air/application/1.5.3
			The last segment of the namespace specifies the version 
			of the AIR runtime required for this application to run.
			
	minimumPatchLevel - The minimum patch level of the AIR runtime required to run 
			the application. Optional.
-->

	<!-- The application identifier string, unique to this application. Required. -->
	<id>LoadTxt</id>

	<!-- Used as the filename for the application. Required. -->
	<filename>LoadTxt</filename>

	<!-- The name that is displayed in the AIR application installer. 
	     May have multiple values for each language. See samples or xsd schema file. Optional. -->
	<name>LoadTxt</name>

	<!-- An application version designator (such as "v1", "2.5", or "Alpha 1"). Required. -->
	<version>v1</version>

	<!-- Description, displayed in the AIR application installer.
	     May have multiple values for each language. See samples or xsd schema file. Optional. -->
	<!-- <description></description> -->

	<!-- Copyright information. Optional -->
	<!-- <copyright></copyright> -->

	<!-- Publisher ID. Used if you're updating an application created prior to 1.5.3 -->
	<!-- <publisherID></publisherID> -->

	<!-- Settings for the application's initial window. Required. -->
	<initialWindow>
		<!-- The main SWF or HTML file of the application. Required. -->
		<!-- Note: In Flash Builder, the SWF reference is set automatically. -->
		<content>[此值将由 Flash Builder 在输出 app.xml 中覆盖]</content>
		
		<!-- The title of the main window. Optional. -->
		<!-- <title></title> -->

		<!-- The type of system chrome to use (either "standard" or "none"). Optional. Default standard. -->
		<!-- <systemChrome></systemChrome> -->

		<!-- Whether the window is transparent. Only applicable when systemChrome is none. Optional. Default false. -->
		<!-- <transparent></transparent> -->

		<!-- Whether the window is initially visible. Optional. Default false. -->
		<!-- <visible></visible> -->

		<!-- Whether the user can minimize the window. Optional. Default true. -->
		<!-- <minimizable></minimizable> -->

		<!-- Whether the user can maximize the window. Optional. Default true. -->
		<!-- <maximizable></maximizable> -->

		<!-- Whether the user can resize the window. Optional. Default true. -->
		<!-- <resizable></resizable> -->

		<!-- The window's initial width. Optional. -->
		<!-- <width></width> -->

		<!-- The window's initial height. Optional. -->
		<!-- <height></height> -->

		<!-- The window's initial x position. Optional. -->
		<!-- <x></x> -->

		<!-- The window's initial y position. Optional. -->
		<!-- <y></y> -->

		<!-- The window's minimum size, specified as a width/height pair, such as "400 200". Optional. -->
		<!-- <minSize></minSize> -->

		<!-- The window's initial maximum size, specified as a width/height pair, such as "1600 1200". Optional. -->
		<!-- <maxSize></maxSize> -->
	</initialWindow>

	<!-- The subpath of the standard default installation location to use. Optional. -->
	<!-- <installFolder></installFolder> -->

	<!-- The subpath of the Programs menu to use. (Ignored on operating systems without a Programs menu.) Optional. -->
	<!-- <programMenuFolder></programMenuFolder> -->

	<!-- The icon the system uses for the application. For at least one resolution,
		 specify the path to a PNG file included in the AIR package. Optional. -->
	<!-- <icon>
		<image16x16></image16x16>
		<image32x32></image32x32>
		<image48x48></image48x48>
		<image128x128></image128x128>
	</icon> -->

	<!-- Whether the application handles the update when a user double-clicks an update version
	of the AIR file (true), or the default AIR application installer handles the update (false).
	Optional. Default false. -->
	<!-- <customUpdateUI></customUpdateUI> -->
	
	<!-- Whether the application can be launched when the user clicks a link in a web browser.
	Optional. Default false. -->
	<!-- <allowBrowserInvocation></allowBrowserInvocation> -->

	<!-- Listing of file types for which the application can register. Optional. -->
	<!-- <fileTypes> -->

		<!-- Defines one file type. Optional. -->
		<!-- <fileType> -->

			<!-- The name that the system displays for the registered file type. Required. -->
			<!-- <name></name> -->

			<!-- The extension to register. Required. -->
			<!-- <extension></extension> -->
			
			<!-- The description of the file type. Optional. -->
			<!-- <description></description> -->
			
			<!-- The MIME content type. -->
			<!-- <contentType></contentType> -->
			
			<!-- The icon to display for the file type. Optional. -->
			<!-- <icon>
				<image16x16></image16x16>
				<image32x32></image32x32>
				<image48x48></image48x48>
				<image128x128></image128x128>
			</icon> -->
			
		<!-- </fileType> -->
	<!-- </fileTypes> -->

</application>


### 如何使用Flex读取文本文件 要实现通过Flex读取并处理文本文件的功能,可以按照以下方法构建Lex程序。以下是详细的说明以及示例代码。 #### 构建Lex程序以读取文本文件 Flex工具本身并不直接提供文件操作功能,但它可以通过C标准库中的`fopen()`函数来打开指定的输入文件,并将其作为输入源传递给生成的扫描器。默认情况下,Flex会从标准输入流(stdin)读取数据,但如果需要从特定文件读取,则可以在调用生成的目标程序时重定向输入[^1]。 下面是一个简单的例子,展示如何创建一个能够统计单词数和字符数的Lex程序: ```c /* wc.l */ %{ #include <stdio.h> int word_count = 0; int char_count = 0; %} %% [a-zA-Z]+ { word_count++; char_count += strlen(yytext); } \n { printf("Line break encountered.\n"); } . { char_count++; } %% int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: %s filename\n", argv[0]); return 1; } FILE *file = fopen(argv[1], "r"); if (!file) { perror("Error opening file"); return 1; } yyin = file; // Redirect input stream to the opened file. yylex(); // Start lexical analysis. fclose(file); printf("Total Words: %d, Total Characters: %d\n", word_count, char_count); return 0; } ``` 上述代码定义了一个基本的词法规则集用于匹配字母序列(即单词),并计算它们的数量及其总长度。此外还包含了错误检测逻辑以便于当命令行参数不足或者无法成功开启目标文档时给出适当反馈。 为了运行此脚本,请遵循这些步骤: 1. 将上面的内容保存到名为 `wc.l` 的文件里; 2. 使用 Flex 工具转换该 `.l` 文件成 C 源码形式: ```bash $ flex wc.l ``` 3. 编译产生的 `lex.yy.c` 到可执行进制文件: ```bash $ gcc lex.yy.c -o my_word_counter -lfl ``` 4. 执行新建立的应用程式并将某个纯文字档当作其唯一实参传入即可查看结果: ```bash $ ./my_word_counter sample.txt ``` #### 处理字符串的例子 如果希望扩展这个基础版本去识别更复杂的模式比如字符串常量的话,那么可以根据需求调整正则表达式部分如下所示[^2]: ```c \"([^\\\"]|\\.)*\" { printf("String found: '%s'\n", yytext); } ``` 这将允许捕获由双引号包围起来的一系列任意非转义字符或者是经过反斜杠转义后的特殊符号组成的串列。 --- ### 注意事项 尽管Adobe Flex/AIR框架可能涉及加密等功能[^3],但这里讨论的是传统意义上的GNU Flex工具链应用于构建自定义解析引擎的情况。对于不同平台上的具体应用开发环境配置等问题不在本文探讨范围内。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值