Tag: 文本文件

在文本文件中扫描到链接列表

我只是在学习链接列表,我必须做一个包含许多部分的任务,但我已经开始了,我需要做的第一件事是将输入文件读入链表。 部分文件是: George Washington, 2345678 John Adams, 3456789 Thomas Jefferson, 4567890 James Madison, 0987654 James Monroe, 9876543 John Quincy Adams, 8765432 并包含总共26行。 我现在要做的只是简单地在文件中读取。 我尝试使用此代码(现在在main中) #include #include struct node{ char name[20]; int id; struct node *next; }*head; int main(void){ struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); head = temp; FILE *ifp; ifp = fopen(“AssignmentOneInput.txt”, […]

从文本文件中创建一个二维数组

我正在使用稀疏矩阵,并给我一个像这样的文本文件: 0 3 1.2 2 5 3.2 3 0 2.1 3 5 4.2 4 5 2.2 0 0 5.2 基本上它的工作方式是位置[0] [3]上的数字1.2和未提及的matriz的元素保持为0,所以在这种情况下它应该如下所示: 5.2 0 0 1.2 0 0 0 0 0 0 0 0 0 0 0 0 0 3.2 2.1 0 0 0 0 4.2 0 0 0 0 0 2.2

解析具有多个公共分隔符C的文件

我正在使用C语言中的基本命令行音乐库,它允许您通过命令行打开文件,并添加艺术家,歌曲标题和发布年份等信息。 一旦退出,它就会将该信息写回同一文件中。 我遇到的问题是试图找到一个正确解析文本文件的解决方案。 例如,输入文件将如下所示: Title: Heirloom, Artist: Basenji, Year Published: 2014 Title: With Me, Artist: Cashmere Cat, Year Published: 2014 我正在研究的项目(反对通常的做法)指出,我们将一行信息存储在如下所示的struct Song : struct Song { char title[250]; char artist[250]; int year_published; }; 每首Song都存储在struct Song类型的数组中,称为music_lib[] 。 我知道如何通过执行以下操作将每一行分成一个特定的struct Song : while(fscanf(input_file, “%s %s %ld”, *temp_title, *temp_artist, *temp_year) != EOF) copy_song_to_music_library(temp_title, temp_artist, temp_year); 我不知道怎么做是如何正确解析文本文件,以便当我有一个已知的格式: Title: Heirloom, Artist: […]

如何修复用C编写的代码?

它适用于20-20表,但对于其他任何事情它都会崩溃。 该文件位于以下链接: http : //www.csd.uoc.gr/~hy100/glid.txt 。 我无法弄清楚问题是什么,因为当我放一个20-20的表时它起作用,当我给21-20表时,它完全可以完成工作,但在能够返回0之前崩溃; 。 #include #include int height, length; char **world, **new_world; int load_world_from_file(char filename[]) { int i, j; char ch; FILE *fp; fp = fopen(filename, “r”); if (fp == NULL ) { perror(“Error while opening the file.\n”); exit(EXIT_FAILURE); } fscanf(fp, “%d %d”, &height, &length); printf(“here:%d,%d\n”, height, length); /*dilosi dunamika tou […]

使用临时文件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编写用于字符串的搜索文件

因此,我必须编写一个从文件中读取并扫描它的函数,以查看其中的任何标题是否与用户放入的标题匹配,并以标题格式打印所有现有标题:(标题)作者:(姓氏,名字)。 如果没有标题匹配则打印出没有找到的标题。 我可以让程序将标题读入数组并以我想要的格式打印,但我的问题是搜索文件以找到匹配的标题来打印它们。程序只是打印出来没有标题匹配5次甚至当有比赛时…非常感谢任何帮助……谢谢…… void findBookByTitle(FILE* fp, char title[]) { FILE* open = fp; char title2[200]; char last[200]; char first[200]; int i=0; while(!feof(fp)) { fscanf(fp, “%[^;];%[^;];%[^\n]”, title2, last, first); if( strcmp(title2,title)==0) { printf(“Title: %s\n”, title2); printf(“Author: %s,%s\n”, last,first); } else { printf(“No books match the title: %s\n”, title); } } } 文本文件说: Making The Right Choices; […]

用C语言调用fprintf语法中的函数

我正在尝试将我的字符串输出打印到一个单独的文件中。 我现在遇到的问题是我的代码带有一组字符串的函数,这些字符串在我的列下面添加了虚线(纯粹是化妆品)。 如何在我的fprintf代码中调用此函数? #include /* function for the dash-line separators*/ void dashes (void) { printf (” —- —– ——————– ————–\n”); } /* end of function definition */ /* main program */ #include #include int main (void) { FILE *data_File; FILE *lake_File; FILE *beach_File; FILE *ecoli_Report; char fileName[10], lake_Table[15],beach_Table[15]; /*.txt file names */ char province[30] = […]

在C中的文本文件中读取,将行分成多个变量

我目前正在开发一个模拟各种CPU调度方法的程序。 目前我有程序要求输入: printf(“Enter type of CPU scheduling algorithm (SJF, RR, PR_noPREMP, PR_withPREMP): “); scanf(“%s”, typeOf); printf(“Enter number of processes: “); scanf(“%d”, &numPro); struct processStruct structs[numPro]; int burstTimes[numPro]; for (i = 0; i < numPro; i++) { printf("Enter process number: "); scanf("%d", &structs[i].pNum); printf("Enter arrival time: "); scanf("%d", &structs[i].arTime); printf("Enter CPU burst time: "); scanf("%d", &structs[i].cpuBur); […]

为什么fwrite()在Mac OS X上使用C中的“wb”写入二进制文件?

为了学习C并理解二进制文件和文本文件之间的区别,我试图将一个字符串写入文件,因为两种文件类型都是这样的: char * string = “I am a string!”; FILE * filePtrA = fopen(“/output.txt”, “wt”); fwrite(string, strlen(string), 1, filePtrA); FILE * filePtrB = fopen(“/output.bin”, “wb”); fwrite(string, strlen(string), 1, filePtrB); fclose(filePtrA); fclose(filePtrB); 但是”wt”和”wb”都是作为文本文件写入的,其中”wb”应该写为二进制文件。 Hex对于这两个文件都是这样的: 49 20 61 6D 20 61 20 73 74 72 69 6E 67 21 为什么会发生这种情况,我怎样才能将内容写成二进制文件? 我已经读过操作系统(Mac OS X 10.6 – GCC 4.2)可能无法区分二进制文件和文本文件,但我仍然难以理解为什么hex编辑器不会发现任何差异。

在ANSI C中计算文本文件中的行和单词的最快方法是什么?

在纯ANSI C中计算文本文件中的行和单词的最快方法是什么? 单词以空格或句点终止。 行以’\n’结尾。 这似乎是在C ++中。