简体   繁体   English

C使用系统调用将.txt转换为二进制文件

[英]C converts .txt to binary file using System Calls

I'm writing a program that replaces every character where position % step = 0 As command line arguments I'm giving 1. file 2. character and 3. step . 我正在编写一个程序来替换position % step = 0每个字符。作为命令行参数,我给出了1. file 2. character3. step I can only use system calls. 我只能使用系统调用。 Here is my main function: 这是我的主要功能:

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

   assert(argc ==  4);

   int fdInput = open(argv[1], O_WRONLY);
   if(fdInput == -1)
      fatalError("Error opening input file.\n");

   char c[1];
   c[0] = argv[2][0];
   unsigned step = atoi(argv[3]);

   int fileSize;
   if((fileSize = lseek(fdInput,0,SEEK_END)) < 0)
      fatalError("Lseek error: Determining file size\n");

   if(lseek(fdInput,0,SEEK_SET) == -1)
      fatalError("Lseek error: Returning to the beginning\n");

   int i;
   for(i = 0; i*step < fileSize; i++)

      if(step - 1 > 0){
         if(lseek(fdInput, i*step - 1, SEEK_SET) == -1)
            fatalError("Lseek error: Within loop\n");

         if(write(fdInput, c, 1) != 1)
            fatalError("Writing error\n");

      }
      else {

         if(write(fdInput, c, 1) != 1)
            fatalError("Writing error.\n");
      }
      close(fdInput);
      return 0;
   }

Example: 例:

input.txt: 123456789 input.txt:123456789

./output input.txt x 3 would return 12x45x78x ./output input.txt x 3将返回12x45x78x

Problem: For some reason, when I compile and execute for the first time, everything works fine ! 问题:由于某种原因,当我第一次编译并执行时, 一切正常 BUT: when I execute it for the second time, it won't work. 但是:当我第二次执行它时,它将无法工作。 When I try to cat/less input.txt it tells me that file is binary. 当我尝试使用cat/less input.txt文件时,它告诉我该文件是二进制文件。

  • echo "123456789" > input.txt -> creates .txt file echo "123456789" > input.txt >创建.txt文件
  • ./output input.txt x 3 -> 12x45x78x ./output input.txt x 3 > 12x45x78x
  • ./output input.txt x 3 -> won't work (program is finished), but: ./output input.txt x 3 >将不起作用(程序已完成),但是:
  • less input.txt -> input.txt" may be a binary file. See it anyway? less input.txt > input.txt" may be a binary file. See it anyway?

How can file be binary? 文件如何为二进制文件? It should be plain text file. 它应该是纯文本文件。 What am I doing wrong here? 我在这里做错了什么? Am I doing something wrong with open ? 我在open做错什么吗?

Your first seek offset is wrong: 您的第一个寻道偏移量是错误的:

i*step - 1

with i == 0 this yields -1 , considering only the pure mathematical calculation and leaving implicit type casts out of the game. i == 0得出-1 ,仅考虑纯数学计算,将隐式类型强制退出游戏。 This is then converted to an unsigned ( off_t is signed and a negative value passed should lead to EINVAL , thus it has to be the implicit conversion) which is going to be extremely large ( UINT_MAX ). 然后将其转换为unsignedoff_t是带符号的,并且传递的负值应导致EINVAL ,因此它必须是隐式转换),这将变得非常大( UINT_MAX )。 The result is an extremely large (but sparse) file. 结果是一个非常大(但稀疏)的文件。

Oh, and: 哦,还有:

assert(argc ==  4);

assert is meant to check invariants, not for handling incorrect user input. assert用于检查不变量,而不是用于处理错误的用户输入。

You should initialize i to 1 instead of 0 ie 您应该将i初始化为1而不是0,即

for(i = 1; i*step < fileSize; i++)

Otherwise, as @Daniel Jour said, your first seek offset will be wrong (i*step - 1 == -1, considering only the pure mathematical calculation and leaving implicit type casts out of the game). 否则,就像@Daniel Jour所说的那样,您的第一个搜寻偏移将是错误的(i * step-1 == -1,仅考虑纯数学计算,而隐式类型强制转换除外)。

Also, you should add for-loop 's opening and closing braces for better legibility. 另外,您应该添加for-loop的左括号和右括号,以提高可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM