Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言将字符[128]数组写入文件_C_String_File - Fatal编程技术网

用C语言将字符[128]数组写入文件

用C语言将字符[128]数组写入文件,c,string,file,C,String,File,我编写了一个程序来测试在C中使用write()函数将char[128]数组写入文件。下面是我的代码,但是,在编写之后,我可以看到testFile.txt文件中字符串“testseg”后面跟着一个“d”或“È”。这是将char[]数组写入文件的正确方法吗 int main() { char pathFile[MAX_PATHNAME_LEN]; sprintf(pathFile, "testFile.txt"); int filedescriptor = open(path

我编写了一个程序来测试在C中使用write()函数将char[128]数组写入文件。下面是我的代码,但是,在编写之后,我可以看到testFile.txt文件中字符串“testseg”后面跟着一个“d”或“È”。这是将char[]数组写入文件的正确方法吗

int main()
{
    char pathFile[MAX_PATHNAME_LEN];
    sprintf(pathFile, "testFile.txt");
    int filedescriptor = open(pathFile, O_RDWR | O_APPEND | O_CREAT, 0777);

    int num_segs = 10;
    int mods = 200;
    const char *segname = "testseg";  /* */
    char real_segname[128];
    strcpy(real_segname, segname);

    write(filedescriptor, &num_segs, sizeof(int));
    write(filedescriptor, real_segname, strlen(real_segname));
    printf("real_segname length is %d \n", (int) strlen(real_segname));
    write(filedescriptor, &mods, sizeof(int));

    close(filedescriptor);


    return 0;
}

在编写之后,我可以看到testFile.txt文件中字符串“testseg”后面跟着一个“d”或“È”

为什么显示“d”或“È”?

仅尝试下面的
write
功能(在代码中,注释除下面调用之外的其余写调用)

现在查看
testFile.txt
cat testFile.txt
)的内容。它显示一些垃圾值

因为,所有
.txt
文件都将以
ASCII文本
格式显示。它将每个字节转换为
ASCII
字符。您正在以ASCII格式写入并以ASCII格式读取的字符串和字符。所以没问题。但在这里,您将mods和num_segs作为整数写入,并将它们作为ASCII格式读取。所以你得到了那些垃圾值

这是将char[]数组写入文件的正确方法吗?


是的,根据手册页,你是用正确的方式写的。请确保验证您的函数调用(
write
)。在哪里写入,在文件中写入什么取决于您的要求

…将字符[128]数组写入文件…我可以看到字符串“testseg”…
这是一个矛盾

在C语言中,字符串是由
char
组成的数组,后跟并包括
'\0'
char[128]
是长度固定的128
char

当代码写入时(filedescriptor、real_segname、strlen(real_segname)),两者都不做。它不是在写C字符串,“testseg”的7
char
'\0'
结尾。相反,它只写了7
char
,没有终止
“\0”
。它也没有写128
char

可以改为执行
write(filedescriptor、real_segname、strlen(real_segname)+1)
写入7
char
和终止的
'\0'
。或者写下长度,然后写下arry中有趣的部分。或者写入整个128
char
array`。需要确定您想要如何读回数据和其他编码目标,以便更好地提供建议


正如@SGG所指出的,异常的
char
仅仅是
write(filedescriptor,&mods,sizeof(int))的结果和不是未终止数组的一部分。

在写入srting后,将200(mod)写入文件。那200是“d”或“È”,简洁地说,是的;这是一种正确的方法。但是,您可能在读回数据时遇到一些问题;你不知道绳子有多长。这通常是通过使用固定长度的写入(例如,使用
strncpy(real_segname,segname,sizeof(real_segname);
然后
write(filedescriptor,real_segname,sizeof(real_segname));
-或者在字符串前面包含一个长度:
short s=strlen(segname);write(filedescriptor,&s,sizeof(s));write(filedescriptor,real_segname,s);
。是“d”还是“E”
testseg
之后的下一个字节?因为您正在写入
mods
之后。可能是这样吗?正如@SGG所说,您正在写入该数据。
write
函数写入二进制数据。如果您希望数字为人类可读,则应使用sprintf将其打印到字符串中,然后写入该字符串。您可能还需要添加新行字符
'\n'
是的,你说得对。它不会写空字符。我不在乎:(。
write(filedescriptor, &mods, sizeof(int));