Tag: 文件

如何使用C文件打开函数打开.ttcn文件?

我正在研究TTCN-3(测试和测试控制表示法)脚本语言。 我想准备这个代码文件的指南检查器。 为此,我想将TTCN-3脚本文件(有些像file.ttcn)的行逐个读入缓冲区。 但对我来说,fopen / sopen / open / fgetc / fscanf无法正常工作且无法正确读取文件。 它给出了NULL。 有什么方法可以将它的字符读入缓冲区。 我认为C无法读取超过三个扩展字符的文件(如.ttcn)。 如果我的假设是错误的,请原谅我。 我的环境是Windows上的Turbo C. 编辑 : 是的我也检查了这些错误,但是他们给read()提供了未知错误,并且没有这样的文件或目录。 我的代码如下 #include #include #include #include #include #include #include int main(void) { int handle; int status; int i=0; char ch; FILE *fp; char *buffer; char *buf; clrscr(); handle = sopen(“c:\\tc\\bin\\hi.ttcn”, O_BINARY, SH_DENYNONE, S_IREAD); / 这里甚至我使用O_TEXT和其他人 […]

从文件读取到数组 – C

int main() { FILE* infile1; int stockCode[15]; char stockName[100]; int stockQuantity[15]; int stockReorder[15]; int unitPrice[15]; int i; infile1 = fopen(“NUSTOCK.TXT”, “r”); while(fscanf(infile1, “%d %s %d %d %f”, &stockCode, stockName, &stockQuantity, &stockReorder, &unitPrice) != EOF) { printf(” %3d %-18s %3d %3d %6.2f \n”, stockCode, stockName, stockQuantity, stockReorder, unitPrice); } fclose(infile1); } 我要做的是从文件中获取信息并将其存储到5个单独的数组中。 但是,在打印时,它只会正确打印出名称。 1394854864 Prune-Basket 1394854688 […]

阅读.tar.gz文件

我在Windows上编程C。 我在尝试阅读.tar.gz文件时遇到此问题。 该文件看起来像(用notepad ++打开): 我以前读过的代码如下: iFile = fopen(“my.tar.gz”, “r”); while ((oneChar = fgetc(iFile)) != EOF) { printf(“%c”, oneChar); } 下图显示了我的程序的结果: 我遇到的问题是,结果只有几行,而原始文件有几千行(6310行,如你所见)。 我的猜测是.tar.gz文件包含一些奇怪的字符(比如文件中间的EOF?)。 我的问题是为什么notepad ++可以显示整个文件而我的程序不能。 这个问题有解决方案吗?

如何确定父/其他目录中的文件和目录

我发现这里的另一个问题的答案非常有用。 似乎有一个sys / stat.h库的限制,因为当我试图查看其他目录时,所有内容都被视为一个目录。 我想知道是否有人知道另一个系统function或为什么它将当前工作目录之外的任何内容视为仅一个目录。 我感谢任何人提供的任何帮助,因为这让我感到困惑,各种搜索都没有帮助。 我测试的代码是: #include #include #include int main(void) { int status; struct stat st_buf; struct dirent *dirInfo; DIR *selDir; selDir = opendir(“../”); // ^ or wherever you want to look while ((dirInfo = readdir(selDir))) { status = stat (dirInfo->d_name, &st_buf); if (S_ISREG (st_buf.st_mode)) { printf (“%s is a regular file.\n”, dirInfo->d_name); […]

尝试删除空格时C代码出错

这段代码是词法分析器的基础,它执行从源文件中删除空格的基本操作,并将其重写为另一个文件,每个单词在不同的行中。 但我无法理解为什么文件lext.txt没有得到更新? #include /* this is a lexer which recognizes constants , variables ,symbols, identifiers , functions , comments and also header files . It stores the lexemes in 3 different files . One file contains all the headers and the comments . Another file will contain all the variables , another will contain all […]

如何从文件中获取替代行并将其作为字符串存储到结构中?

我有一个文件需要通过代码读取。 该文件如下所示。 文件的第一行包含一个整数,表示文件中的日记条目数。 我需要编写一个C程序来读取文件并将内容存储在动态分配的结构数组中。 4 12/04/2010 Interview went well I think, though was told to wear shoes. 18/04/2010 Doc advised me to concentrate on something… I forget. 03/05/2010 Was asked today if I was an art exhibit. 19/05/2010 Apparently mudcakes not made of mud, or angry wasps. 我能够strtok()将日期,月份和年份存储在我的结构中,但是我坚持将字符串保存到我的结构中。 这是我的strtok()代码, FILE* file=fopen(“struct.txt”,”r”); if (file==NULL){ perror(“Error opening […]

使用临时文件C在文本文件中编辑一行

我正在尝试编辑文本文件中的一行但我在编辑文件时有一个意外的行为。 我想要做的是调整看起来像的文本的特定行(点数:100)。 在函数中,我通过值传递参数,要调整的新硬币和文件与ftell-> user_point的偏移量。 我得到的输出很奇怪。 我尝试将文件的其余部分复制到temp,使用已编辑的行,然后将其从我复制到temp的点复制回原始文件(这就是带有ftell的user_point偏移量)。 这是原始的fie与这样的条目: … _______________________________________ nickname : geo password : cuvctq Name : george Surname : papas points : 100 participated : past draws : 0 Chosen No. : future draws : 0 Registered : Sun Feb 05 19:23:50 2012 … 我在第二次编辑运行后得到的是: … _______________________________________ nickname : geo password : cuvctq Name […]

C – 尝试返回文件中的上一行

我必须阅读一个可以从可选注释开始的文本文件。 在实践中,我必须跳过文件开头的任何不以’@’或’>’开头的行。 在我的测试用例中,文件看起来像: # Sun Jul 12 22:04:52 2009 /share/apps/corona/bin/filter_fasta.pl –output=/data/results/solid0065/primary.20090712170542775 # Cwd: /state/partition1/home/pipeline # Title: solid0065_20090629_FC1_Tomate_Heinz_4_5_Kb_Tomate_Heinz_4_5_Kb_01 >125_963_316_F3 T1230330231223011323010013 所以我必须跳过前3行(但一般来说我必须跳过n行)。 我必须用2或4个文件[它们在FILE ** inputFiles中]重复这个。 我试过这个循环: buffer = (char*) malloc (sizeof(char) * 5000); if (buffer == NULL) notEnoughMemory(); for (i = 0; i ‘)){ fgetpos(inputFiles[i], &position); fgets(buffer, 4999, inputFiles[i]); } fsetpos(inputFiles[i], &position); } 其中cIn是number_of_input_files + 1.尝试调试它时,循环在读取第四行后正确停止。 但是当我使用setpos时,它不会像我期望的那样回到第四行的开头,而是在第三行的中间。 […]

提取文件的扩展名

一些细节 语言:C 系统:Linux; 使用命令行(终端),通过终端读入文件 C用户体验:3个月 我一直在尝试提取给定文件的扩展名,例如“myfile.wld”,以便稍后我可以检查是否在终端上输入了正确的文件类型文件。 这对于作业来说是必要的 我使用函数“strtok”将输入分隔为分隔符“。”。 dot=strtok(argv[1], “.”); filename=dot; 文件名现在是输入“myfile”的第一部分,我的问题是如何在“。”之后进入第二部分,请提出建议,并请耐心等待,使其尽可能简单,以便我可以制作使用你的回复 谢谢

如何比较ASCII值

我想将字母的ASCII值存储到变量中,我该怎么做? 例如 : r ASCII variable = 82 main() { character = “character read from a file”; variable= “r ascii”; //(in this case 82), the problem is that the letter is always variable.; printf( “the value of %c is %d, character, variable) } 我怎样才能做到这一点? 另外还要注意,如何逐个字符地读取.txt文件? 所以它可以保存在字符变量上。