Tag: file io

fopen返回null – Perror打印无效参数

简单的fopen操作似乎不起作用。 perror返回 – 参数无效。 可能有什么不对。 我在R:中有一个名为abc.dat的ascii文件。 int main() { FILE *f = fopen(“R:\abc.dat”,”r”); if(!f) { perror (“The following error occurred”); return 1; } } 输出: 发生以下错误:参数无效。

在C程序中复制文件,但文件为空

我正在尝试将文件test1.mal的内容复制到output.txt并且程序说它正在这样做并且所有内容都编译,但是当我打开output.txt文件时,它是空白的……有人能说出来吗我哪里出错了? #include #include #include int main(void) { char content[255]; char newcontent[255]; FILE *fp1, *fp2; fp1 = fopen(“test1.mal”, “r”); fp2 = fopen(“output.txt”, “w”); if(fp1 == NULL || fp2 == NULL) { printf(“error reading file\n”); exit(0); } printf(“files opened correctly\n”); while(fgets(content, sizeof (content), fp1) !=NULL) { fputs(content, stdout); strcpy (content, newcontent); } printf(“%s”, newcontent); printf(“text received\n”); while(fgets(content, […]

如何将多字节字符串转换为glibc中fxprintf.c中的宽字符串?

目前, glibc perror源程序中的逻辑是这样的: 如果stderr是面向的,那么按原样使用它,否则使用dup()它并在dup() ‘ed fd上使用perror() 。 如果stderr是面向广域的,则使用stdio-common / fxprintf.c中的以下逻辑: size_t len = strlen (fmt) + 1; wchar_t wfmt[len]; for (size_t i = 0; i < len; ++i) { assert (isascii (fmt[i])); wfmt[i] = fmt[i]; } res = __vfwprintf (fp, wfmt, ap); 通过以下代码将格式字符串转换为宽字符forms,我不明白: wfmt[i] = fmt[i]; 此外,它使用isascii断言: assert (isascii(fmt[i])); 但格式字符串在宽字符程序中并不总是ascii,因为我们可能使用UTF-8格式字符串,它可以包含非7位值。 为什么在运行以下代码时没有断言警告(假设UTF-8语言环境和UTF-8编译器编码)? #include #include #include #include […]

将文本文件读入结构

我的文本文件是这样的(文件中的数据也可以增加) 822 172.28.6.56 172.34.34.53 3244 5434 844 192.150.160.145 192.67.45.123 2344 234 700 192.13.56.6 192.89.95.3 4356 3566 522 172.28.6.137 172.28.6.110 2543 5245 900 255.255.255.255 244.244.244.244 2435 3245 我的结构是这样的 struct input_par { int key_node; char src_ip[15]; char dst_ip[15]; int src_port; int dst_port; }; 我必须从存储在文件中的数据中填充这个结构,一旦完成输入结构的5个成员我必须将此结构发送到函数,即,我填充结构 822 172.28.6.56 172.34.34.53 3244 5434 然后我将这个结构发送到一个函数,在将它发送到函数后,我应该再次输入带有文件中下一个数据的结构,即 844 192.150.160.145 192.67.45.123 2344 234 我必须继续这样做直到我到达EOF。 我使用fscanf它不能正常工作怎么做? […]

如何从文本文件中写入和读取(包括空格)

我正在使用fscanf和fprintf 。 我尝试用\t分隔每行的字符串,并像这样读取它: fscanf(fp,”%d\t%s\t%s”,&t->num,&t->string1,&t->string2); 文件内容: 1[TAB]string1[TAB]some string[NEWLINE] 它没有正确读取。 如果我printf(“%d %s %s”,t->num,t->string1,t->string2)我得到: 1 string1 some 我也得到这个编译警告: warning: format specifies type ‘char *’ but the argument has type ‘char (*)[15]’ [-Wformat] 如何在不使用二进制r / w的情况下解决这个问题?

C中的浮点数组

我正在开发一个代码,我必须在文本文件中一次加载存储在每一行中的浮点值…我使用fscanf()将每个数据加载到一个浮点数组中…但是我发现浮点数以不同的方式存储,例如407.18存储为407.179993,414.35存储为414.350006 …现在我被卡住了,因为数据以它们在文件中的forms存储是绝对重要但这里似乎是虽然基本上是相同的……但是我如何得到以原始forms存储的数字?

格式与文件内容不匹配时fscanf的行为

如果文件的内容与传递给fscanf的格式字符串不匹配,那么在下次调用fscanf时会发生什么? 假设一个文件包含以下两行: 9000 pig dog 4 5 2 程序试图解析打开的文件( fp ): int a = 1, b = 1, c = 1; int x = 1, y = 1, z = 1; fscanf(fp, “%d %d %d”, &a, &b, &c); fscanf(fp, “%d %d %d”, &x, &y, &z); 我怀疑a现在会持有9000而b和c继续保持值1 – 但是x , y和z会发生什么? C99标准是否保证x , y和z保持值4和2 – 或者文件流的位置指示器保证在解析失败后保持不变,导致x保持值9000而y和z保持值1 […]

在C中将数据从一个文本文件复制到另一个文本文件

我正在编写一个基本程序,它从现有的文本文件中复制一个字符串,并将文本复制到一个新的文本文件中。 我快到了,但我有一些小问题。 首先,我在复制后将文本行输出到屏幕,它在字符串后面给我3个随机字符。 我想知道为什么会这样。 此外,程序正在创建新的文本文件,但不将字符串放入文件中。 这是我的代码: #include #include #include int main(void) { char content[80]; char newcontent[80]; //Step 1: Open text files and check that they open// FILE *fp1, *fp2; fp1 = fopen(“details.txt”,”r”); fp2 = fopen(“copydetails.txt”,”w”); if(fp1 == NULL || fp2 == NULL) { printf(“Error reading file\n”); exit(0); } printf(“Files open correctly\n”); //Step 2: Get text […]

在C中使用fread和fwrite的问题

以下是我的代码,将一些硬编码的int值(1,2,3,4,5)写入文件,关闭该文件,在读取模式下打开同一文件并读取写入的元素。 从输出中看到fwrite正常发生但fread无法正确读取。 #include int main() { FILE *fptr; FILE *optr; const char *filepath = “E:\\testinput.txt”; int buf[5]={1,2,3,4,5}; int obuf[5]; int value; int *ptr = &value; int num_bytes_read; int no_of_iterations; int i; int ret;//return value for fwrite int count = 0; no_of_iterations = 5; //open the file fptr = fopen(filepath, “wb”); if(fptr == NULL){ printf(“error in […]

从文件中读取单词到简单的链表

我需要编写一个程序来读取文件,然后将这些单词保存到链表中以供进一步使用。 我决定使用fgetc逐个字符地读取文本,然后每次检测到换行符( ‘\n’ )或空格( ‘ ‘ )时将全部保存到列表中,表示一个单词。 对不起,我是文件指针的新手,这是我到目前为止所得到的: struct list { //global char string[30]; struct list *next; }; int main(void) { FILE *filePtr; char file[] = “text.txt”; char tempStr[30]; list *curr, *header; char c; int i = 0; curr = NULL; header = NULL; if((filePtr = fopen(file, “r”)) == NULL) { printf(“\nError opening file!”); getchar(); […]