有人可以检查我的代码,我不知道它有什么问题.每当我执行代码时,我都会在标题中得到错误.
<?php
$file = "newfile.txt";
$text = "This is a text line. ";
$handle = fopen($file, "w");
fwrite($handle, $text);
fclose($handle);
$handle = fopen($file, "a");
$text = "Here are more text lines to insert."
fwrite($handle, $text);
fclose($handle);
include ($file);
?>
Run Code Online (Sandbox Code Playgroud) 我正在使用C的fwrite函数将3个整数的数组写入文件,但是当使用gedit(使用Unicode UTF-8)打开输出文件时,我收到以下错误:
There was a problem opening the file. The file you opened has invalid characters. If you continue editing this file, you could corrupt the document.
Run Code Online (Sandbox Code Playgroud)
这是相关的代码段:
char* imageFile = "image.txt";
FILE* imageFilePtr = fopen(imageFile, "w");
int scores[3] = {69, 70, 71};
fwrite(scores, sizeof(int), sizeof(scores), imageFilePtr);
Run Code Online (Sandbox Code Playgroud)
当我使用十六进制读取器,如"xxd"时,我在终端中得到以下内容:
0000000: 4500 0000 4600 0000 4700 0000 7031 7108 E...F...G...p1q.
0000010: 0830 7108 2987 0408 2087 0408 0460 cebf .0q.)... ....`..
0000020: 0100 0000 0000 0000 0000 0000 0000 0000 ................
Run Code Online (Sandbox Code Playgroud)
请记住,在我的环境中,sizeof(int)是4个字节.因此,我可以看到十进制中的69,70和71如何以xxd显示的十六进制中的45,46和47打印到文件中.但是,"4700 …
fwrite({0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0xf3, 0xff, 0x61, 0x00, 0x00, 0x01, 0x8a, 0x49, 0x44, 0x41, 0x54, 0x38, 0x8d, 0xa5, 0x93, 0xb1, 0x72, 0xd4, 0x30, 0x10, 0x86, 0xbf, 0x95, 0xad, 0xcc, 0xc4, 0x30, 0xa4, 0xb9, 0xe6, 0x68, 0x68, 0xd3, 0x50, 0xf3, 0x42, 0xbc, 0x0e, 0xcf, 0xc3, 0x0c, 0x1d, 0x33, 0x69, 0x68, 0xa8, 0xaf, 0x09, 0xc5, 0x51, …Run Code Online (Sandbox Code Playgroud) 我试图用来fread()确定文件是否是 jpeg,我的想法是使用fread()读取文件的标题以确保它是 jpeg,然后,如果是 - 复制文件。我知道标准的 jpeg 标头是0xff 0xd8 0xff 0xe(0 to f),所以我制作了一个程序来读取文件的前四个字节。
#include <stdio.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main (int argc, char *argv[])
{
FILE *in = fopen(argv[1], "rb");
FILE *out = fopen("output.jpg", "wb");
BYTE buffer [4096];
while (fread(buffer, 1, sizeof(buffer), in))
{
if ((buffer[0] == 0xff) & (buffer[1] == 0xd8) & (buffer[2] == 0xff) & ((buffer[3] & 0xf0) == 0xe0))
{
fwrite(buffer, 1, sizeof(buffer), out);
}
}
fclose(in);
fclose(out);
}
Run Code Online (Sandbox Code Playgroud)
但是,在运行程序后,当我尝试打开 …