读取文件并将其反转到另一个文件?

时间:2013-11-17 20:34:28

标签: c

我有这个代码,但每次我尝试运行它都删除源文件而不提供任何输出,所以我该如何解决我的问题?

注意问题是问我:

编写一个程序,从命令行获取两个文件名,并将第一个文件内容的反向复制到第二个文件中,假设它能够打开第一个文件进行读取,第二个文件一个写作。如果它无法打开第一个文件进行阅读,它既不能创建也不能修改(视情况而定)第二个文件。该程序必须使用低级函数

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

int main(int argc, char *argv[])
{
    int source, dest, n;
    char buf;
    int filesize;
    int i;


    if (argc != 3)
    {   
    fprintf(stderr, "usage %s <source> <dest>\n", argv[0]);
        exit(-1);
    }

在此我想尝试使用以下格式:open(“outf”,O_WRONLY | O_CREAT | O_TRUNC,0666)

   if ((source = open(argv[1],O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
   { //read permission for user on source 

       fprintf(stderr, "can't open source\n");
       exit(-1);
   }


   if ((dest = creat(argv[2], 0666)) < 0)
   { //rwx permission for user on dest

       fprintf(stderr, "can't create dest");

       exit(-1);
   }

   filesize = lseek(source, (off_t) 0, SEEK_END); //filesize is lastby +offset

   printf("Source file size is %d\n", filesize);


   for (i = filesize - 1; i >= 0; i--)
   { //read byte by byte from end

       lseek(source, (off_t) i, SEEK_SET);


       n = read(source, &buf, 1);


       if (n != 1)
       {

       fprintf(stderr, "can't read 1 byte\n");
       exit(-1);
       }

       n = write(dest, &buf, 1);

       if (n != 1)
       {
       fprintf(stderr, "can't write 1 byte\n");

       exit(-1);
       }


   }

   write(STDOUT_FILENO, "DONE\n", 5);

   close(source);

   close(dest);

   return 0;
}

感谢

4 个答案:

答案 0 :(得分:4)

我很抱歉粗鲁,但你甚至看过你转到第一次公开电话的选项吗? O_CREAT | O_TRUNC

您认为这些选项有何作用?这些选项导致您的源文件被删除。

答案 1 :(得分:0)

您需要使用模式O_RDONLY打开 输入 文件,即仅限地址 O_WRONLY,即仅写作

答案 2 :(得分:0)

检查一下:

int checkstatus(ifstream &in)
{
ios::iostate i;
i = in.rdstate();
if(i & ios::eofbit)
return 0;//cout << "EOF encountered\n";
else if(i & ios::failbit)
return 0;//cout<<"Non-Fatal I/O error\n";
else if(i & ios::badbit)
return 0;//cout<<"Fatal I/O error\n";

return 1;
}


int main()
{
    ifstream in;
    ofstream o;
    in.open("test.txt");
    o.open("test1.txt",ios::out);
    char c;
    in.seekg(0,ios::end);
        while(checkstatus(in) != 0)
        {
            in.seekg(-1,ios::cur);
            in.get(c);
            in.seekg(-1,ios::cur);
            if(checkstatus(in) == 0)
            break;
            cout<<c;
            o.put(c);

        }
        in.close();
        o.close();
    return 0;
}

答案 3 :(得分:0)

我在这里做了一些改变,它工作得很好,但没有非常大的文件!!

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

int main(int argc, char *argv[])
{
    int source, dest, n;
    char buf;
    int filesize;
    int i;


    if (argc != 3)
    {   
    fprintf(stderr, "usage %s <source> <dest>\n", argv[0]);
        exit(-1);
    }

   if ((source = open(argv[1], 0666)) < 0)
   { //read permission for user on source

       fprintf(stderr, "can't open source\n");
       exit(-1);
   }


   if ((dest = open(argv[2],O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
   { //rwx permission for user on dest

       fprintf(stderr, "can't create dest");

       exit(-1);
   }

   filesize = lseek(source, (off_t) 0, SEEK_END); //filesize is lastby +offset

   printf("Source file size is %d\n", filesize);


   for (i = filesize - 1; i >= 0; i--)
   { //read byte by byte from end

       lseek(source, (off_t) i, SEEK_SET);


       n = read(source, &buf, 1);


       if (n != 1)
       {

       fprintf(stderr, "can't read 1 byte\n");
       exit(-1);
       }

       n = write(dest, &buf, 1);

       if (n != 1)
       {
       fprintf(stderr, "can't write 1 byte\n");

       exit(-1);
       }


   }

   write(STDOUT_FILENO, "DONE\n", 5);

   close(source);

   close(dest);

   return 0;
}

我需要用lseek()吗?