Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C编程文件I/O_C_File Io - Fatal编程技术网

C编程文件I/O

C编程文件I/O,c,file-io,C,File Io,我试图从一个文本文件中读取并写入一个,但每次执行代码时,文本文件都不会发生任何变化。所谓“什么也不发生”,我的意思是程序不会读取我的输入文件,也不会将任何数据导出到我的输出文件中。有人能指出它为什么不起作用吗?谢谢你事先给予的帮助。这是我的密码: #include <stdio.h> #include <stdlib.h> FILE *inptr, *outptr; int main() { int a, b, c; inptr = fopen("t

我试图从一个文本文件中读取并写入一个,但每次执行代码时,文本文件都不会发生任何变化。所谓“什么也不发生”,我的意思是程序不会读取我的输入文件,也不会将任何数据导出到我的输出文件中。有人能指出它为什么不起作用吗?谢谢你事先给予的帮助。这是我的密码:

#include <stdio.h>
#include <stdlib.h>

FILE *inptr, *outptr; 

int main() {
    int a, b, c;
    inptr = fopen("trianglein.txt","r"); //Initialization of pointer and opening of file trianglein.txt
    outptr = fopen("triangleout.txt","w"); //Initialization of pointer and opening of file triangleout.txt

    while((fscanf(inptr,"%d %d %d",&a, &b, &c))!= EOF){  
        fprintf(outptr,"\n%2d %2d %2d\n",a,b,c); 
        if(a+b>c && b+c>a && c+a>b){
            fprintf(outptr, "This is a triangle.\n"); 
            if(a !=b && b !=c && a!=c){ 
                fprintf(outptr, "This is a scalene triangle.\n");
                if(a==b && a==c && c==b){
                    fprintf(outptr, "This is an equilateral triangle.\n");
                    if(a*a+b*b==c*c || b*b+c*c==a*a || a*a+c*c==b*b){
                        fprintf(outptr, "This is a right trianlge.\n");
                    }
                }
            } 
        }
    }

    return 0;
}

编辑:正如icktoofay所建议的,这个答案是错误的

您必须执行
fclose()
fflush()
才能将数据写入文件。 在
返回0之前插入这些代码

fclose(inptr);
fclose(outptr);
试一试推杆

fclose(inptr);

在代码末尾。

多个问题

首先,您需要通过对NULL进行测试来检查inptr和outptr是否有效

其次,fscanf可以返回EOF、0或>0

如果您的输入文件不包含有效的输入

还有一个问题是,您可以成功读取3个整数,或者2个整数或1,并且a、b和c的值只能选择设置

如果输入上没有发生转换,则返回零值,在这种情况下,while循环将退出

还请记住,对于scanf样式函数,此输入将成功并返回值1

“1rubbish”

我想你可能想要的是如下所示:

// Somewhere near the top
#include <stderr.h>
// ... other includes

const char* inname = "trianglein.txt";
const char* outname = "triangleout.txt";

// Any other stuff


// Inside main...

// Initialization of pointer and opening of file trianglein.txt
if ((inptr = fopen(inname,"r")) == 0){
  fprintf(stderr, "Error opening file %s: %s", inname, strerror(inname));
  return -1;
}

// Initialization of pointer and opening of file triangleout.txt
if ((outptr = fopen(outname,"w")) == 0){
  fprintf(stderr, "Error opening file %s: %s", outname, strerror(outname));
  return -1;
}


int result;
while(true){
  result = fscanf(inptr,"%d %d %d",&a, &b, &c);
  if (result == EOF)
    break;

  if (result < 3)  // Ignore incomplete lines
    continue;

  // do the normal stuff
}  
//靠近顶部的某个地方
#包括
// ... 其他包括
const char*inname=“trianglein.txt”;
const char*outname=“triangleout.txt”;
//还有别的吗
//内主。。。
//初始化指针并打开文件triangalein.txt
如果((inptr=fopen(inname,“r”))=0){
fprintf(stderr,“打开文件%s:%s时出错”,inname,strerror(inname));
返回-1;
}
//初始化指针并打开文件triangleout.txt
如果((outptr=fopen(outname,“w”))==0){
fprintf(stderr,“打开文件%s:%s时出错”,outname,strerror(outname));
返回-1;
}
int结果;
while(true){
结果=fscanf(inptr,“%d%d%d”、&a、&b和&c);
如果(结果==EOF)
打破
if(结果<3)//忽略不完整的行
继续;
//做正常的事情
}  

您的程序在我的系统中运行良好。我在
windows7
上使用
code::Blocks 10.05

只有当
fscanf()
读取的文件triangalein.txt的整数值小于3时,才会发生逻辑错误。例如,值为
1
12
1234
12345
等的
trianglein.txt
文件将为变量
b
和/或
c
提供不正确的值。因此,在执行循环的每个迭代之前初始化
a=-1
b=-1
c=-1
,并在读取后检查它们

如果正在运行该程序,请检查文件
triangleout.txt
以获取访问权限。有时,您可能没有对该特定文件的写入权限


顺便说一下,分类逻辑是错误的。等边三角形不可能是直角三角形。

这似乎适合我。当我运行triangleout.txt时,它包含了一些东西。你能发布你的trianglein.txt的内容吗。(另外,您可能需要重新查看您的逻辑,因为直角三角形不是等边三角形。)如果什么都没有发生,那么工作目录中就没有文件“trianglein.txt”。检查fopen的返回。如果是这种情况,请将文件放入工作目录。同时检查“trianglein.txt”的内容。顺便说一句,使用调试器是个好主意。@CarealManic:这也不是问题。由于
fopen
未通过
b
标志,因此文件以文本模式打开,其中读取的换行符被标准化为
\n
,而不管它们在文件中是什么。@Cranderberry这不重要。在工具栏中,单击
project->Properties
。然后单击选项卡
bulid targets
。您应该看到类似于
输出文件名
执行工作目录
的内容。将
输出文件名
更改为
程序
,将
执行工作目录
更改为
。然后将文件
trianglein.txt
复制/移动到与
程序所在的目录相同的目录中。那么它应该可以工作了。我相信这会有帮助@你能解释一下你的想法吗?重复我对Careal Manic答案的评论,这不会有帮助:根据C99草案N1256§7.20.4.3¨4,所有流在程序终止时都会刷新。这对问题的直接问题没有帮助,但即使在main()中清理资源也是一种很好的方式,在大型程序中也是必不可少的。此外,查询者没有提到是否涉及C99编译器。没有。根据C99草案N1256§7.20.4.3¨4,所有流在程序终止时被刷新。@icktoofay哦,你说得对。我刚刚编译了代码。它起作用了。不管怎样,我不会删除这个答案。@Cranderberry:它可能什么也没做,但最好在处理完文件资源后关闭它们。在这种情况下,由于您的程序在处理后直接终止(并非在所有语言中都是如此),但并非总是如此。尽管“针对NULL的测试”在语义上是正确的,并且您的代码正确地使用了
if(!inptr)
,但两者并不匹配。
if(input!=NULL)
将显式匹配,但几乎没有人会介意。这个习惯用法倾向于
if(inptr=fopen(“trianglein.txt”)…
,在测试中使用赋值。True,我可能会正常地这样写。
fclose(outptr);
// Somewhere near the top
#include <stderr.h>
// ... other includes

const char* inname = "trianglein.txt";
const char* outname = "triangleout.txt";

// Any other stuff


// Inside main...

// Initialization of pointer and opening of file trianglein.txt
if ((inptr = fopen(inname,"r")) == 0){
  fprintf(stderr, "Error opening file %s: %s", inname, strerror(inname));
  return -1;
}

// Initialization of pointer and opening of file triangleout.txt
if ((outptr = fopen(outname,"w")) == 0){
  fprintf(stderr, "Error opening file %s: %s", outname, strerror(outname));
  return -1;
}


int result;
while(true){
  result = fscanf(inptr,"%d %d %d",&a, &b, &c);
  if (result == EOF)
    break;

  if (result < 3)  // Ignore incomplete lines
    continue;

  // do the normal stuff
}