Tag: file io

Windows CreateFile可能的错误代码

我正在使用将Windows,Linux和Mac文件IO调用抽象为宏(以避免C运行时,因此没有fopen , fclose等……)。 我实际上有相当多的工作,但我遇到了绊脚石。 我试图将每个平台可能抛出的所有可能错误归结为常见的错误:未找到,存在,无效访问等。 Linux显然是很好的文档,Mac甚至有最常见的文档,但Windows没有指定为其本机文件I / O函数抛出哪些错误。 我们显然需要使用GetLastError() ,但我找不到可能值的引用。 这个怎么样? 我正在编写Windows应用程序并使用CreatFile() API。 我想尽可能优雅地处理任何错误,并且可能从错误中恢复而不是告诉用户“废话!不能那样做”。 但是MSDN文档没有列出可能生成的错误代码。 有没有人引用Windows文件函数可能生成的错误代码,特别是(现在) CreateFile() ?

C函数fwrite()不写入文件

我正在尝试将tempGroupFile结构写入GroupFile 。 写入时fwrite()返回1,但实际上没有数据写入文件GroupFile 。 函数printRec()打印出屏幕上的结构。 data是结构的变量。 这些操作后,文件GroupFile为空。 码: GWTemp = fopen(tempGroupFile, “rb”); GW = fopen(GroupFile, “wb”); if((GW == NULL) || (GWTemp == NULL)) { puts(“Failed to open file.”); fflush(stdin); getchar(); return 0; } while(fread(&data, sizeof data, 1, GWTemp)) { if(fwrite(&data, sizeof data, 1, GW)) { printRec(data); } }

读取文本文件并在c编程中存储在数组中

我想读取文本文件并存储在数组然后显示。 这是我的代码: int i = 0, line = 5; char ch[100]; FILE *myfile; myfile = fopen(“test.txt”,”r”); if (myfile== NULL) { printf(“can not open file \n”); return 1; } while(line–){ fscanf(myfile,”%s”,&ch[i]); i++; printf(“\n%s”, &ch[i]); } fclose(myfile); return 0; } 这是我的文字: 测试123562 856 59986 但结果是: 美东时间 2356 56 9986 怎么了? 🙁

如何从文件中将数字扫描到数组?

假设我有一个带有数字的文件:1 2 3 4 5 6 7 8 9 10 -1 我想读取该文件并存储该文件的所有值,停在控制变量-1。 因此,如果我将该数组打印到另一个文件,它看起来像:1 2 3 4 5 6 7 8 9 10 这给了我所有的数字,但它包括-1。 怎么能掉掉-1? int arr[100]; int n; while (scanf(“%d”,&arr[n]) > 0) n++;

错误:在C中的’*’标记之前预期’=’,’,’,’;’,’asm’或’__attribute__’

我有以下声明 FILE *fptr; FILE *optr; 在algo.h中,我在main.c中使用main来打开这些文件。 如果我将声明放在头文件中,我会收到上述错误。 如果我把它放在main.c中,那么我会得到多个定义错误 src\main.o:main.c:(.bss+0xc88): multiple definition of rcount’ src\new_algo.o:new_algo.c:(.bss+0xc88): first defined here src\main.o:main.c:(.bss+0xc8c): multiple definition of condi’ src\new_algo.o:new_algo.c:(.bss+0xc8c): first defined here

在文件中搜索特定的行c代码

我正在研究C.我想问一下在文件中搜索特定行(或多行)的最佳方法是什么? 有人可以举个例子。 我有2个文件,我想看看这两个文件是否80%相同。 我想在其中一个文件中搜索其他文件中的某些特定行。 谢谢 我需要一些C代码示例。 这是一个小例子 int compareFile(FILE* file_compared, FILE* file_checked) { bool diff = 0; int N = 65536; char* b1 = (char*) calloc (1, N+1); char* b2 = (char*) calloc (1, N+1); size_t s1, s2; do { s1 = fread(b1, 1, N, file_compared); s2 = fread(b2, 1, N, file_checked); if (s1 != […]

如何读取和写入文件的整数

问题是我试图读取一些整数以了解我已经通过的游戏的级别,然后我想将当前级别的整数设置为1,如果t还没有。 它创建文件但不写任何东西。 谁知道为什么? 现在,它是第一次使用printf创建它时,但在读取它时会提供状态访问冲突。 void SaveGame(void) { FILE *pFile = fopen(“SavedData.txt”,”rb”); int MyArray[8] = {0}; if(pFile) { fscanf(pFile, “%d %d %d %d %d %d %d %d” , MyArray[0], MyArray[1], MyArray[2], MyArray[3], MyArray[4], MyArray[5], MyArray[6], MyArray[7]); fclose(pFile); } if(MyArray[Current] == 0) MyArray[Current] = 1; pFile = fopen(“SavedData.txt”, “wb”); if(pFile) { fprintf(pFile, “%d %d %d %d %d […]

使用文件i / o读取字节长度

我试图用以下代码找到两个不同文件的字节长度,但得到字节长度为1,这显然是错误的。 从长远来看,我正在尝试比较每个文件的内存位置,并打印出它们的不同之处,如您所见。 所以我没有到达任何地方,并做了printf语句,看看问题可能在哪里。 因此,看起来好像我的长度没有正确计算。 可能对我的问题有帮助的旁注 – 我发现这是为memcmp,但这是否意味着我不能使用!= ?: 如果返回值<0则表示str1小于str2 如果返回值> 0则表示str2小于str1 如果返回值if = 0则表示str1等于str2 请帮忙! void compare_two_binary_files(int f1, int f2) { ssize_t byte_read_f1, byte_read_f2, length, numRead, bob, length2; char buf1[BUF_SIZE], buf2[BUF_SIZE], a[100], b[100], counter[100]; int count = 0, b_pos1, b_pos2; while ((byte_read_f1 = read(f1, buf1, sizeof buf1) > 0) && (byte_read_f2 = read(f2, buf2, sizeof […]

getc和fscanf之间的区别

为什么以下代码工作正常: #include int main() { FILE *fp=fopen(“input.txt”,”r+”); char c; while((c=getc(fp))!=EOF) { printf(“%c”,c); } fclose(fp); return 0; } 但是这段代码给出了错误“分段错误,核心转储”: #include int main() { FILE *fp=fopen(“input.txt”,”r+”); char c; while((c=fscanf(fp,”%c”,&c))!=EOF) { printf(“%c”,c); } fclose(fp); return 0; } input.txt包含一个以空格分隔的字符列表,如:abcdef

使用C解析wtmp日志

对于我们的赋值,我们得到一个wtmp日志的副本,并期望解析它,并以排序的格式输出它,类似于last的输出。 现在,我知道文件wtmp包含一个utmp结构列表。 保证提供的文件至少包含一个utmp结构,我应该假设二进制文件中的所有结构都是正确构造的。 我通过man utmp阅读,并且我已成功编写了一个程序来读取所提供的二进制文件的结构。 (我为漫长的打印方法道歉) #include #include #include #include void utmpprint(struct utmp *log); int main() { int logsize = 10; FILE *file; struct utmp log[logsize]; int i = 0; file = fopen(“wtmp”, “rb”); if (file) { fread(&log, sizeof(struct utmp), logsize, file); for(i = 0; i ut_type, log->ut_pid, log->ut_line, log->ut_id, log->ut_user, log->ut_host, log->ut_exit.e_termination, log->ut_exit.e_exit, log->ut_session, […]