与文本文件的两个工作 [英] Working with Text Files Two

查看:102
本文介绍了与文本文件的两个工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个问题真正了解code低于从中我在previous后获得援助。

1)。任何想法,为什么在输出中的结尾,我得到一个随机的垃圾字符打印?我释放文件等,并检查EOF。

2)。这个想法是,它可以与多个文件arguements工作,所以我想创建一个新增加的文件名,即出[I] txt文件,是可以用C?

在code本身需要包含单词都用空格隔开,就像一本书,例如一个文件,然后通过循环,并取代以\\ n各自的空间,以便形成一个列表,请找$ C $下面C:

 的#include<&stdlib.h中GT;
#包括LT&;&文件ctype.h GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdio.h中GT;/ *
 *
 * /
INT主(INT ARGC,字符** argv的){FILE * fpIn,* fpOut;
INT I;
焦炭℃;
而(argc--){
    对于(i = 1; I< = ARGC;我++){
        fpIn =的fopen(argv的[I]中,RB);
        fpOut = FOPEN(tmp.out,世行);
        而(C!= EOF){
            C =龟etc(fpIn);
            如果(isspace为(c))的
                C ='\\ n';
            的fputc(C,fpOut);
        }
    }
}
FCLOSE(fpIn);
FCLOSE(fpOut);
返回0;
}


解决方案

当你到达文件的末尾,你不要循环。那么,你是叫的fputc(C,fpOut); ç== EOF 这可能是一个未定义的行为,或至少的写作\\ 0xFF的字节。

和你不叫 FCLOSE 你的而内(ARGC - )循环,让您的文件(除了最后一个),大多是永远不会关闭,也没有脸红了。

最后,你不试的fopen 的结果,你应该测试它是非空(并打印一个错误信息,也许一些关于字符串错误(错误) PERROR ,在这种情况下)。

您应该有一个调试器(如 GDB 在Linux上)发现了,也许可以和编译器警告(但 GCC-4.6的帮助-Wall 并没有抓到你的例子任何错误)。

您可以决定输出文件名与输入文件名,或许与

 字符outname [512];
对于(i = 1; I< ARGC,我++){
   fpIn =的fopen(argv的[I]中,RB);
   如果{PERROR(的argv [I])(fpIn!);出口(1); };
   memset的(outname,0,sizeof的(outname));
   的snprintf(outname,sizeof的(outname)-1,%S〜%d.out的argv [I],I);
   fpOut = FOPEN(outname,WB);
   如果(fpOut!){PERROR(outname);出口(1); };
   ///等..
   FCLOSE(fpIn);
   FCLOSE(fpOut);
   fpIn = fpOut = NULL;
}

A couple of questions really about the code below from which I gained assistance in a previous post.

1). Any ideas why at the end of the ouput, I get a random garbage character printed? I am freeing the files etc and checking for EOF.

2). The idea is that it can work with multiple file arguements, so I want to create new file names which increment, i.e. out[i].txt, is that possible in C?

The code itself takes a file containing words all separated by spaces, like a book for example, then loops through, and replaces each space with a \n so that it forms a list, please find the code below:

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

/*
 * 
 */
int main(int argc, char** argv) {

FILE *fpIn, *fpOut;
int i;
char c;
while(argc--) {
    for(i = 1; i <= argc; i++) {
        fpIn = fopen(argv[i], "rb");
        fpOut= fopen("tmp.out", "wb");
        while (c != EOF) {
            c = fgetc(fpIn);
            if (isspace(c)) 
                c = '\n';
            fputc(c, fpOut );
        }
    }
}
fclose(fpIn);
fclose(fpOut);
return 0;
}

解决方案

When you reach the end of file, you don't break the loop. So you are calling fputc(c, fpOut); with c==EOF which is probably an undefined behavior, or at least the writing of a \0xff byte.

And you don't call fclose inside your while(argc--) loop, so your files (except the last) are mostly never closed nor flushed.

At last, you don't test the result of fopen and you should test that it is non null (and print an error message, perhaps with something about strerror(errno) or perror, in that case).

You should have found out with a debugger (like gdb on Linux), and perhaps with the help of compiler warnings (but gcc-4.6 -Wall did not caught any bugs on your example).

You could decide that the output file name is related to input file name, perhaps with

char outname[512];
for(i = 1; i < argc; i++) {
   fpIn = fopen(argv[i], "rb");
   if (!fpIn) { perror (argv[i]); exit(1); };
   memset (outname, 0, sizeof (outname));
   snprintf (outname, sizeof(outname)-1, "%s~%d.out", argv[i], i);
   fpOut= fopen(outname, "wb");
   if (!fpOut) { perror (outname); exit(1); };
   /// etc...
   fclose(fpIn);
   fclose(fpOut);
   fpIn = fpOut = NULL;
}

这篇关于与文本文件的两个工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆