C中的文件处理命令? [英] File handling commands in C?

查看:44
本文介绍了C中的文件处理命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2(类似)问题:



1.在C中编写文件处理程序时,您如何知道使用哪个命令?就像C中的不同文件处理命令之间的区别一样:fopen还是openfil?





2.声明FILE后是否有任何规则?对于任何了解C的人来说,这可能是一个非常简单的问题,但对我来说,无处不在,我到处都看到不同的公式。是否有1,2,3步?有没有办法解决这个新手,一种思维方式?





补充:(这是一个老练习考试,我想弄清楚)。一个国家组织希望按年龄列出非洲的所有大象。他们都注册了ID,年龄和面积在elephants.dat。编写代码并将每个大象读成一个数组,然后按年龄排序。



我尝试过:



阅读所有内容,阅读我老师的谷歌+资料。我找不到红线。

2 (similar) questions:

1. How do you know which command to use when writing file handling programs in C? Like what is the difference between the different file handling commands in C like: fopen or openfil?


2. Is there any rules after declaring FILE? This is probably a very simple question for anyone that knows C, but for me all I see everywhere is different formulas everywhere. Is there a 1, 2, 3-step ish? Is there a way to approach this for a newbie, a way of thinking?


Added: (this is one exercise in an old exam that I'm trying to figure out). A national organisation wants to list all the elephants in Africa by their age. They are all registred by ID, age and area in elephants.dat. Write the code and read every elephant into an array, then sort them by their age.

What I have tried:

Reading everything there is to read on google + materials from my teacher. I cant find the red thread.

推荐答案

fopen [ ^ ]是 C 用于打开文件的函数。您的 OS 也可能提供其他功能,例如 Windows 提供 CreateFile 功能)。如果可移植性很重要,请使用 fopen



FILE [ ^ ]对象在 stdio.h 头文件中声明。你通常会声明一个指向它的指针,例如

fopen[^] is the C function used to open a file. Your OS might provide other functions as well, though (for instance Windows provides, for instance, the CreateFile function). If portability matters, use fopen.

The FILE[^] object is declared in stdio.h header file. You usually declare a pointer to it, e.g.
FILE * fp;



然后在文件上使用 I / O 任务的指针,例如


and then use such a pointer for I/O tasks on the file, e.g.

fp = fopen("foo.txt", "w");
if ( fp )
{
  fprintf(fp, "fooooooo!\n");
  fclose(fp);
}


1。
没有一般规则必须使用哪种类型(文件描述符或基于流)。在几乎所有情况下都可以使用,由您决定。简要概述差异:

GNU C库:流和文件描述符 [ ^ ]



我只会在无法完成操作的情况下使用流功能和基于描述符的功能有溪流。



2.

我不太清楚你想知道什么。

但一般规则是



  • 打开文件并检查是否成功

  • 执行I / O操作

  • 关闭文件

1. There is no general rule which type (file descriptor or stream based) has to be used. In nearly all cases both can be used and it is up to you to decide. A short overview of the differences:
The GNU C Library: Streams and File Descriptors[^]

I would use stream functions in general and descriptor based functions only if the operations can't be done with streams.

2.
It is not quite clear for me what you want to know.
But the general rules are

  • Open the file and check for success
  • Perform the I/O operations
  • Close the file
#include <stdio.h>
#include <errno.h>

int someFunc(const char *fileName)
{
    int fileErr = 0;
    FILE *file = NULL;

    file = fopen(fileName, mode);
    if (NULL == file)
    {
        fileErr = errno;
        /* Optionally report error here */
    }
    else
    {
        /* Perform IO here */
        fclose(file);
    }
    return fileErr;
}


这篇关于C中的文件处理命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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