fopen()在现有文件上调用时导致分段错误

时间:2016-07-21 01:04:01

标签: c

当我尝试在C中调用fopen()时,只有在目录中有文件的调用时才会出现分段错误。我正在开发一个使用指纹传感器的项目,将指纹注册到文件上,然后读取文件以与运行时正在读取的指纹进行比较。

这是代码的要点。

File * file;
file = fopen("right_thumb.bin", "w");
fwrite(&var, sizeof(ABS_BIR)/*size of the type the fingerprint is stored in*/
, sizeof(&var)/*8*/, file);
fclose(file);
file = fopen("right_thumb.bin", "rb");
fread(&readImage, sizeof(ABS_BIR), sizeof(&var), file);

奇怪的是,以下作品完美无缺......

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'start' ]
2 info using npm@2.15.5
3 info using node@v4.4.5
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info prestart MEAN@1.0.0
6 info start MEAN@1.0.0
7 verbose unsafe-perm in lifecycle true
8 info MEAN@1.0.0 Failed to exec start script
9 verbose stack Error: MEAN@1.0.0 start: `node server.js`
9 verbose stack Exit status 1
9 verbose stack     at EventEmitter.<anonymous> (C:\Program    Files\nodejs\node_modules\npm\lib\utils\lifecycle.js:217:16)
9 verbose stack     at emitTwo (events.js:87:13)
9 verbose stack     at EventEmitter.emit (events.js:172:7)
9 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\lib\utils\spawn.js:24:14)
9 verbose stack     at emitTwo (events.js:87:13)
9 verbose stack     at ChildProcess.emit (events.js:172:7)
9 verbose stack     at maybeClose (internal/child_process.js:827:16)
9 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
10 verbose pkgid MEAN@1.0.0
11 verbose cwd C:\Users\kmagl\Documents\CuttingEdge\new-project4
12 error Windows_NT 10.0.10586
13 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
14 error node v4.4.5
15 error npm  v2.15.5
16 error code ELIFECYCLE
17 error MEAN@1.0.0 start: `node server.js`
17 error Exit status 1
18 error Failed at the MEAN@1.0.0 start script 'node server.js'.
18 error This is most likely a problem with the MEAN package,
18 error not with npm itself.
18 error Tell the author that this fails on your system:
18 error     node server.js
18 error You can get information on how to open an issue for this project with:
18 error     npm bugs MEAN
18 error Or if that isn't available, you can get their info via:
18 error
18 error     npm owner ls MEAN
18 error There is likely additional logging output above.
19 verbose exit [ 1, true ]

奇怪的是我可以自由地打开创建的同一个文件,但如果我将文件更改为由此文件的旧实例创建的其他文件,则会收到错误。

1 个答案:

答案 0 :(得分:4)

您似乎无法理解fread fwrite使用情况和指针变量,而且与fopen电话

无关
  • fread不会为您分配缓冲区,您需要分配它
  • 当你将它用作fread的参数时,
  • readImage已经是一个指针(未分配),所以它的值可以是任何东西,
    另外,您传递的是&readImage,这意味着您将写入指针的地址。这可能是有效的,但我怀疑这是你的意图。
  • sizeof返回您作为参数传递的变量的大小,例如在您的情况下: sizeof(&var)会将地址大小返回var(系统上显然是8

我建议您使用调试器,它可以让您深入了解代码的内容。

我的猜测是你应该写下面的内容:

File * file;
file = fopen("right_thumb.bin", "w");
fwrite(var, sizeof(ABS_BIR)/*size of the type the fingerprint is stored in*/
, 1/* corrected, only one buffer of ABS_BIR size to write*/, file);
fclose(file);

file = fopen("right_thumb.bin", "rb");
ABS_BIR * readImage=malloc(sizeof(ABS_BIR));
fread(readImage, sizeof(ABS_BIR), 1, file); 

请注意,当您正在阅读并向该文件写入一个大小为sizeof(&var)的缓冲区时,我将1的实例更改为sizeof(ABS_BIR)