在C中读取和打印.txt文件行最清晰的方法 [英] Clearest way to read and print .txt file lines in C

查看:333
本文介绍了在C中读取和打印.txt文件行最清晰的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





他们似乎都是根据具体的例子量身打造的。



使用最清晰的最简洁最简单的方式可以简单地: 任何文本文件的行到屏幕 最好有详细解释每行的功能。

简洁明了。

static void cat(FILE * fp)
{
char buffer [4096];
size_t nbytes;
while((nbytes = fread(buffer,sizeof(char),sizeof(buffer),fp))!=
fwrite(buffer,sizeof(char),nbytes,stdout);

$ b $ int main(int argc,char ** argv)
{
FILE * fp;
const char *文件; ((fp = fopen(file,r))!= 0)
((file = * ++ argv)!= 0)

{
cat(fp);
fclose(fp);
}
}
return(0);

$ / code>

cat()函数不是绝对必要的,但我宁愿使用它。主程序遍历每个命令行参数并打开指定的文件。如果成功,它会调用 cat()函数来打印它的内容。由于对 fopen()的调用没有指定rb,所以打开为文本文件。如果文件没有打开,这段代码默默地忽略了这个问题。如果没有指定文件,则根本不会打印任何文件。
$ b

cat()函数只是读取文本块一次最多4096个字节,并将它们写入标准输出(屏幕)。当没有更多的文件被读取时,它会停止。



如果你想扩展代码来读取没有指定文件的标准输入,那么你可以使用:


$ b

  if(argc == 1)
cat(stdin);
else
{
... while loop as now ...
}

这是 cat()函数写成如下所示的原因之一。



这段代码并不直接关注换行符或任何类型的行。如果你想正式处理一行,那么你可以做几件事情:
$ b $ pre $静态无效的猫(文件* fp)
{
char buffer [4096];
while(fgets(buffer,sizeof(buffer),fp)!= 0)
fputs(buffer,stdout);

$ / code $ / pre

这将一次读写一行。如果任何一行超过4095字节,它将在两个或多个操作中读取该行,并将其写入相同数量的操作。请注意,这假设一个文本文件,使用 fread() fwrite()的版本不会。在POSIX系统上,带有 fread() fwrite()的版本将会处理空字节(< (code>'\0'),但使用 fgets() fputs ()不会。到目前为止,这两个版本都是严格的标准C(标准的任何版本),因为它们不使用任何平台特定的扩展;它们就像代码一样可移植。

另外,如果你有POSIX 2008 getline() 函数,您可以使用它,但需要 #include< stdlib.h> (因为你最终不得不释放它分配的内存):

  static void cat(FILE * fp)
{
char * buffer = 0;
size_t buflen = 0; (getline(& buffer,& buflen,fp)!= -1)
fputs(buffer,stdout);
free(buffer);





$ b这个版本也不会处理二进制数据在里面)。当然可以升级到这样做:

  static void cat(FILE * fp)
{
char * buffer = 0;
size_t buflen = 0;
ssize_t nbytes; ((nbytes = getline(& buffer,& buflen,fp))!= -1)
fwrite(buffer,sizeof(char),nbytes,stdout);
free(buffer);

$ / code>

getline()函数报告它读取了多少字节(之后有一个空字节),但是 fwrite()函数是唯一一个接收任意字节流并将其写入的函数所有到给定的流。


There are a bunch of ways describing how to use various methods to print out lines of a text file on this site:

They all seem to be tailored to a specific example.

It would be great to have the Clearest and Most Concise and Easiest way to simply: print each line of any text file to the screen. Preferably with detailed explanations of what each line does.

Points for brevity and clarity.

解决方案

#include <stdio.h>

static void cat(FILE *fp)
{
    char   buffer[4096];
    size_t nbytes;
    while ((nbytes = fread(buffer, sizeof(char), sizeof(buffer), fp)) != 0)
         fwrite(buffer, sizeof(char), nbytes, stdout);
}

int main(int argc, char **argv)
{
    FILE *fp;
    const char *file;
    while ((file = *++argv) != 0)
    {
        if ((fp = fopen(file, "r")) != 0)
        {
            cat(fp);
            fclose(fp);
        }
    }
    return(0);
}

The cat() function is not strictly necessary, but I'd rather use it. The main program steps through each command line argument and opens the named file. If it succeeds, it calls the cat() function to print its contents. Since the call to fopen() does not specify "rb", it is opened as a text file. If the file is not opened, this code silently ignores the issue. If no files are specified, nothing is printed at all.

The cat() function simply reads blocks of text up to 4096 bytes at a time, and writes them to standard output ('the screen'). It stops when there's no more to read.

If you want to extend the code to read standard input when no file is specified, then you can use:

if (argc == 1)
    cat(stdin);
else
{
    ...while loop as now...
}

which is one of the reasons for having the cat() function written as shown.

This code does not pay direct attention to newlines — or lines of any sort. If you want to process it formally one line at a time, then you can do several things:

static void cat(FILE *fp)
{
    char buffer[4096];
    while (fgets(buffer, sizeof(buffer), fp) != 0)
         fputs(buffer, stdout);
}

This will read and write one line at a time. If any line is longer than 4095 bytes, it will read the line in two or more operations and write it in the same number of operations. Note that this assumes a text file in a way that the version using fread() and fwrite() does not. On POSIX systems, the version with fread() and fwrite() will handle arbitrary binary files with null bytes ('\0') in the data, but the version using fgets() and fputs() will not. Both the versions so far are strictly standard C (any version of the standard) as they don't use any platform-specific extensions; they are about as portable as code can be.

Alternatively again, if you have the POSIX 2008 getline() function, you can use that, but you need #include <stdlib.h> too (because you end up having to release the memory it allocates):

static void cat(FILE *fp)
{
    char *buffer = 0;
    size_t buflen = 0;
    while (getline(&buffer, &buflen, fp) != -1)
         fputs(buffer, stdout);
    free(buffer);
}

This version, too, will not handle binary data (meaning data with null bytes in it). It could be upgraded to do so, of course:

static void cat(FILE *fp)
{
    char *buffer = 0;
    size_t buflen = 0;
    ssize_t nbytes;
    while ((nbytes = getline(&buffer, &buflen, fp)) != -1)
         fwrite(buffer, sizeof(char), nbytes, stdout);
    free(buffer);
}

The getline() function reports how many bytes it read (there's a null byte after that), but the fwrite() function is the only one that takes a stream of arbitrary bytes and writes them all to the given stream.

这篇关于在C中读取和打印.txt文件行最清晰的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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