简体   繁体   English

C 读取二进制数文件会丢弃第一个字符

[英]C reading a file of binary numbers drops first char

Can anyone help me understand why the first char read from the file is dropped when I increase the file size to be > 19 rows?谁能帮我理解为什么当我将文件大小增加到 > 19 行时从文件中读取的第一个字符会被删除?

When I run this with < 20 rows it works perfect, reads the input and dumps it.当我用 < 20 行运行它时,它运行良好,读取输入并转储它。 when I add a 20th row the first row of input drops the leading char when I print the array.当我添加第 20 行时,当我打印数组时,输入的第一行会删除前导字符。

Im lost.我迷路了。 :) :)

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

int main () {

  int i, j = 0;
  FILE *pFile;
  char string[20][12];
  char file_contents_resultset[20][12];
  

  pFile = fopen("sonar.txt", "r");
  if (pFile == NULL)
    {
      printf ("error opening file");
      return 0;
    }
  // Load 20 row of input into in an array and store a copy
  for (i = 0; i < 20; i++)
    {
      fscanf (pFile, "%12s", &string[i]);
      strcpy (file_contents_resultset[i], string[i]);
    }

  //Dump the first 5 rows of the array
  printf ("Dump array contents \n");
  for (i = 0; i < 5; i++)
    {
      for (j = 0; j < 12; j++)
        {
          printf ("%c", file_contents_resultset[i][j]);
        }
      printf ("\n");
    }

  fclose (pFile);

  return 0;
};

This is my input file.这是我的输入文件。

000110010001
101000110000
000110010111
100011100010
111001100001
001010001010
010100100101
011000010000
111111011010
001111011101
011011010010
001100010101
001010101100
000000000000
100010111111
100100110011
111100100001
011110001110
000110100101
011101111001

and this is the output这是 output

Dump array contents 
00110010001
101000110000
000110010111
100011100010
111001100001

and this is the output if I delete the 20th row of input in the input file.如果我删除输入文件中的第 20 行输入,这就是 output。 Note the first char is no longer dropped.请注意,不再删除第一个字符。

Dump array contents 
000110010001
101000110000
000110010111
100011100010
111001100001

Your one of the arrays you use for reading is redundant.您用于阅读的 arrays 之一是多余的。 You can use only string[][] or only file_contents_resultset[][] .您只能使用string[][]或仅使用file_contents_resultset[][]

I found that your problem is in the strcpy() call.我发现您的问题出在strcpy()调用中。 Reading from the buffer is fine, but strcpy() seems to copy a blank character in the memory location of file_contents_resultset[0][0] .从缓冲区读取很好,但strcpy()似乎在file_contents_resultset[0][0]的 memory 位置复制了一个空白字符。

So to fix it by keeping as much of the program intact i did:因此,通过保持尽可能多的程序完好无损来修复它:

// ...
// stores only one row at a time
char string[12];
// ...

for (int i = 0; i < 20; i++) {
    fscanf(pFile, "%12s", &string);
    strcpy(file_contents_resultset[i], string);
}

// ...
}

If you want to be more concise and save memory, you can remove string altogether and just write:如果您想更简洁并保存 memory,您可以完全删除string ,只需编写:

// ...
// char string[12];
char file_contents_resultset[20][12];
// ...

// brackets in loops and other blocks can be omitted
// if the block is just 1 line long. 
for (int i = 0; i < 20; i++)
   fscanf(pFile, "%12s", &file_contents_resultset[20][12]);
// ...

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

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