Tag: 文件

从C中的文本文件中读取int值

我有一个包含以下三行的文本文件: 12 5 6 4 2 7 9 我可以使用fscanf函数读取前3个值并将它们存储在3个变量中。 但我无法阅读其余的内容。 我尝试使用fseek函数,但它只适用于二进制文件。 请帮我将所有值存储在整数变量中。

检索文件中的总行数

谁能告诉我如何用编程语言C获取文本文件中的总行数?

C文件处理查询

所以我有一个程序,它接受用户输入并将其与文件中的特定行进行比较,但是最后一行总是被认为是不正确的,所以有人能为我解决这个问题吗?谢谢。 文件内容(只是一个随机单词列表) Baby Milk Car Face Library Disc Lollipop Suck Food Pig (库是stdio,conio和string) char text[100], blank[100]; int c = 0, d = 0; void space(void); int main() { int loop = 0; char str[512]; char string[512]; int line = 1; int dis = 1; int score = 0; char text[64]; FILE *fd; fd = fopen(“Student […]

为文件创建自定义标头(元数据)

在这里,我想创建一个包含其他文件详细信息的标头,如其他文件的元数据。 如果我使用struct file_header静态值,这段代码可以正常工作。 如果我使用malloc for struct file_header那么我在这段代码中遇到了问题。 具体来说,我遇到了一个问题。 也许fwrite工作得很好。 代码在这里: #include #include #include #include #include #include #include char path[1024] = “/home/test/main/Integration/testing/package_DIR”; //int count = 5; struct files { char *file_name; int file_size; }; typedef struct file_header { int file_count; struct files file[5]; } metadata; metadata *create_header(); int main() { FILE *file = fopen(“/home/test/main/Integration/testing/file.txt”, “w”); metadata […]

使用libavformat读取位于内存中的文件

我目前正在尝试阅读从服务器发送的小型video文件 为了使用libavformat读取文件,您应该调用 av_open_input_file(&avFormatContext, “C:\\path\\to\\video.avi”, 0, 0, 0); 问题是在这种情况下,文件不在磁盘上,而是在内存中。 我现在正在做的是下载文件,使用临时名称将其写入磁盘,然后使用临时文件名调用av_open_input_file ,这不是一个非常干净的解决方案。 事实上,我想要的是像av_open_custom(&avFormatContext, &myReadFunction, &mySeekFunction);这样的函数av_open_custom(&avFormatContext, &myReadFunction, &mySeekFunction); 但我没有在文档中找到任何内容。 我想这在技术上是可行的,因为文件的名称不能帮助库确定它使用的格式。 那么有这样的函数,还是av_open_input_file的替代品?

C读取由具有无界字大小的空格分隔的文本文件

我有一个文本文件,其中包含由空格分隔的单词(字符串)。 字符串的大小不受限制,字数也不限。 我需要做的是将文件中的所有单词放在列表中。 (假设列表工作正常)。 我无法弄清楚如何克服无限大小的字大小问题。 我试过这个: FILE* f1; f1 = fopen(“file1.txt”, “rt”); int a = 1; char c = fgetc(f1); while (c != ‘ ‘){ c = fgetc(f1); a = a + 1; } char * word = ” “; fgets(word, a, f1); printf(“%s”, word); fclose(f1); getchar(); 我的文本文件如下所示: this is sparta 请注意,我能得到的只是第一个单词,即使我做得不正确,因为我得到了错误: Access violation writing […]

如何将strtok与每个单个非alpha字符一起用作分隔符? (C)

所以我有一个字符串: **BOB**123(*&**blah**02938*(*&91820**FOO** 我希望能够使用strtok来消除每个单词。 分隔符是每个不是字母的单个字符。 我被isalpha我们isalpha ,但不知道我会怎么做。 有没有办法在不命名每个非alpha字符的情况下执行此操作? 不幸的是,不允许使用正则表达式库。

使用fseek和ftell来确定文件的大小有漏洞吗?

我已经阅读过post,展示了如何使用fseek和ftell来确定文件的大小。 FILE *fp; long file_size; char *buffer; fp = fopen(“foo.bin”, “r”); if (NULL == fp) { /* Handle Error */ } if (fseek(fp, 0 , SEEK_END) != 0) { /* Handle Error */ } file_size = ftell(fp); buffer = (char*)malloc(file_size); if (NULL == buffer){ /* handle error */ } 我即将使用这种技术但后来遇到了描述潜在漏洞的链接 。 该链接建议使用fstat。 任何人都可以评论这个吗?

访问C中的目录

该程序是打开一个目录,并显示文件的名称…即,如果有一个文件..应该说FILE …. else DIRECTORY ..但程序显示所有文件作为目录.. 任何人都可以检查代码是否有任何错误…. thnx #include #include #define DIR_path “root/test” main() { DIR *dir; dir=opendir(DIR_PATH); printf(“THe files inside the directory :: \n”); struct dirent *dent; if(dir!=NULL) { while((dent=readdir(dir))) { FILE *ptr; printf(dent->d_name); if(ptr=fopen(dent->d_name,”r”)) { print(“\tFILE\n”); fclose(ptr); } else printf(“\t DIRECTORY\n”); } close(dir); } else printf(“ERROR OPENIN DIRECTORY”); }

在C中逐行读取文件

我正在尝试编写一些代码来打开文件,逐行读取其内容并将这些行存储到数组中。 首先,我打开文件并计算行数,每行都是固定长度,所以我只是这样做: char buf2[LINE_LENGTH]; int in2 = open(“toSend2”, O_RDONLY); int number_of_lines = 0; for (;;) { char* p2 = buf2; int count = read (in2, p2, LINE_LENGTH); if (count < 0) { printf("ERROR"); break; } if (count == 0) break; number_of_lines++; printf("count: %d \n",count); printf("File 2 line : %s", p2); printf("\n"); } close (in2); 到目前为止,这很好用,number_of_lines确实是文件“toSend2”中的行数,而我的每个printf都是该文件中包含的行。 […]