读取和写入文件open()read()

时间:2017-11-14 18:17:59

标签: c file byte

我需要制作一个程序来读取文件(arg [1])并显示内容,但是具有特定的字节数[arg2]。如果没有指定字节,则将打印整个文件。

void read(char *arg[]){
        int fh;
        char buffer[100];
        int rd;

        fh = open(arg[1],O_RDONLY);
        printf ("Printing %s\n",arg[1]);
        while (rd = read(fh,buffer,100)) {
                buffer[rd] = '\0';
                printf("%s",buffer);
                }
        }

我至少需要使用open(),read()。 有人能帮助我吗?

2 个答案:

答案 0 :(得分:1)

关于您编写​​的程序的观察很少,

1)void read(char *arg[])这里来自main()函数你只是传递文件名,然后需要用array of char pointer来捕获文件名,捕获单个char指针就足够了。所以像void read(char *arg)

一样修改它

2)fh = open(arg[1],O_RDONLY); open()是系统调用,无论是否能够打开文件,捕获返回值&尝试使用perror().打印错误消息,然后修改它的类似

fh = open(arg[1],O_RDONLY);
    if(fh == -1) // try to read man page first.
    { 
        perror("open");
        return ;
    }

3)while (rd = read(fh,buffer,100)) { //some code }为什么旋转循环?您可以一次读取文件的整个数据,为此使用stat()系统调用并查找文件大小,然后创建等效于文件大小的动态数组,然后使用read()系统读取整个/特定数据调用

首先浏览open()read()stat()系统调用的手册页。

以下是解释您需求的解决方案

#include <stdio.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

void my_read(char *arg1){
        int fh;
        //  char buffer[100];//don't use static array bcz we don't know how much data is there in file, create it dynamically
        int rd,n;


        fh = open(arg1,O_RDONLY);
        if(fh == -1)
        {
                perror("open");
                return;
        }
        else
        {
                //first find the size of file .. use stat() system call
                struct stat v;
                stat(arg1,&v);//storing all file related information into v
                int size = v.st_size;//st.szie is a member of stat structure and it holds size of file
                //create dynamic array equal to file size 
                char *p = malloc(size * sizeof(char));

                printf("enter no of bytes you want to read :\n");
                scanf("%d",&n);

                if(n==0)
                {
                        //read data from file and copy into dynamic array and print 
                        int ret = read(fh,p,size);
                        if(ret == -1)
                        {
                                perror("read");
                                return ;
                        }
                        else
                        {
                                printf("data readed from file is :\n");
                                printf("%s\n",p);
                        }

                }
                else
                {
                        //read data from file and copy into dynamic array and print 
                        int ret = read(fh,p,n);
                        if(ret == -1)
                        {
                                perror("read");
                                return ;
                        }
                        else
                        {
                                printf("data readed from file is :\n");
                                printf("%s\n",p);
                        }
                }
        }
}
int main(int argc,char *argv[])
{
        char f_name[100];
        printf("enter the file name :\n");
        scanf("%s",f_name);
        my_read(f_name);
}

答案 1 :(得分:0)

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

void print(char* buff, int size)
{
  int i;
  for(i = 0; i < size ; i++)
  {
    printf("%c",buff[i]);
  }
  printf("\n");
}

int main(int argc, char* argv[])
{
    FILE * pFile;
    long lSize;
    char * buffer;
    size_t result;

    pFile = open ( argv[1] , O_RDONLY );
    if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
    if(argv[2])
    {
      lSize = argv[2];
    }
    else
    {
      fseek (pFile , 0 , SEEK_END);
      lSize = ftell (pFile);
      rewind (pFile);
    }

    buffer = (char*) malloc (sizeof(char)*lSize);
    if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
    result = read (pFile,buffer,lSize);
    if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
    print(buffer,lSize);


    fclose (pFile);
    free (buffer);
    return 0;
}