Tag: file

如何fread()结构?

struct book { unsigned short size_of_content; unsigned short price; unsigned char *content; }; 假设我有包含多book的文件,每个book都有不同的size_of_content , price和content 。 我如何一次阅读一book并确定它是哪本书(例如检查价格)? size_t nread2; struct book *buff = malloc(sizeof(struct book)); while( (nread2 = fread(buff, sizeof(struct book), 1, infp)) > 0 ) { printf(“read a struct once \n”); } 这就是我到目前为止所拥有的。 每当我读取结构时,我都会尝试打印。 但是,当我尝试使用5个结构的输入文件时,它将打印15次… 谢谢。

来自stdin问题的fgets

我正在编写一个可以处理文件的程序。 我需要能够将数据作为结构输入,并最终将其读出。 我目前遇到的问题是这个代码: typedef struct { char* name; ….. }employeeRecord; employeeRecord record; char name[50]; if(choice == 1) { /*Name*/ printf(“\nEnter the name:”); fgets(name,50,stdin); record.nameLength = strlen(name) -1; record.name = malloc(sizeof(char)*record.nameLength); strcpy(record.name,name); /*Other data, similar format…*/ 如果我想要,例如,姓名地址和电话号码,并要求连续每一个(所以地址几乎与上面相同,除了用地址替换’名称’),我发现它跳过输入。 我的意思是,我没有机会输入它。 输出实际上输入名称:输入地址:(这里是它提示我输入的地方)

如何获取文件或目录的绝对路径,*不存在?

如何在GNU / Linux上的C / C ++中确定给定相对路径的文件或目录的绝对路径? 我知道realpath() ,但它不适用于不存在的文件。 假设用户输入../non-existant-directory/file.txt ,程序工作目录为/home/user/ 。 我需要的是一个返回/home/non-existant-directory/file.txt的函数。 我需要此函数来检查给定路径是否在某个子目录中。

如何使用scanf \ fscanf读取一行并解析为变量?

我正在尝试在每一行中读取使用以下格式构建的文本文件: 字符*,字符*,诠释 即: AAAAA,dfdsd,23 bbbasdaa,DDD,100 我想使用fscanf从文件中读取一行,并自动将该行解析为varilables string1,string2,intA 这样做的正确方法是什么? 谢谢

使用mmap将文件读取到字符串

我正在尝试使用mmap将文件读取到字符串。 我跟随这个例子: http : //www.lemoda.net/c/mmap-example/index.html 我的代码看起来像这样 unsigned char *f; int size; int main(int argc, char const *argv[]) { struct stat s; const char * file_name = argv[1]; int fd = open (argv[1], O_RDONLY); /* Get the size of the file. */ int status = fstat (fd, & s); size = s.st_size; f = (char *) […]

以“a +”模式打开文件

如果使用以下命令打开文件: FILE *f1=fopen(“test.dat”,”a+”); 手册页内容如下: A + 打开阅读和追加(在文件末尾写)。 如果文件不存在,则创建该文件。 用于读取的初始文件位置位于文件的开头,但输出始终附加到文件的末尾。 那么f1有两个独立的偏移指针,一个用于读取,另一个用于写入?

read()调用后printf打印垃圾。 偏移始终打印为0

#include #include #include #include #include int main() { int file; off_t offset; if((file=open(“testfile.txt”,O_RDONLY)) < -1) return 1; char buffer[19]; if(read(file,buffer,19) != 19) return 1; fprintf(stdout,"%s\n",buffer); if(offset = lseek(file,-19,SEEK_END) < 0) return 1; fprintf(stdout,"%jd\n",(intmax_t)offset); if(read(file,buffer,19) != 19) return 1; fprintf(stdout,"%s\n",buffer); return 0; } 输出如下: 这是一个测试文件 0 他是一个测试文件 testfile.txt: 这是一个测试文件测试SEEK_END的工作原理这是一个测试文件 我尝试了不同的偏移格式,例如%ld,%d,但输出仍然相同。 无法弄清楚为什么垃圾出现在第一行和最后一行的末尾。 请帮忙。

从文件或标准输入读取

我正在编写一个实用程序,它接受文件名或从stdin读取。 我想知道检查stdin是否存在的最强大/最快的方法(数据是否通过管道传输到程序),如果是,则读取该数据。如果不存在,则处理将在文件名上进行给出。 我已经尝试使用以下测试stdin大小,但我相信因为它是一个流而不是一个实际的文件,它不起作用,因为我怀疑它会,它总是打印-1 。 我知道我总是可以一次读取输入1个字符!= EOF但是我想要一个更通用的解决方案,所以如果stdin存在,我最终会得到fd或FILE *所以程序的其余部分将无缝运行。 我希望能够知道它的大小,等待前一个程序关闭流。 long getSizeOfInput(FILE *input){ long retvalue = 0; fseek(input, 0L, SEEK_END); retvalue = ftell(input); fseek(input, 0L, SEEK_SET); return retvalue; } int main(int argc, char **argv) { printf(“Size of stdin: %ld\n”, getSizeOfInput(stdin)); exit(0); } 终奌站: $ echo “hi!” | myprog Size of stdin: -1