LINUX中c语言怎么把汉字存放到数组中
发布网友
发布时间:2023-07-21 04:41
我来回答
共1个回答
热心网友
时间:2024-12-05 14:43
//代码已测,我用的是一位数组,没必要用二维数组
#include <stdio.h>
main()
{
char buf[100] = {0};
int len;
FILE* f = NULL;
printf("read from file? y/n\n");
if (getchar() == 'y')
{
if ((f = fopen("1.txt", "rb")) == NULL)
{ printf("open file failed."); return; }
len = fread(buf, 1, sizeof(buf), f);
if (len) { printf("%s\n", buf); }
fclose(f);
}
else
{
printf("input your words:\n");
scanf("%s", buf);
if ((f = fopen("1.txt", "ab+")) == NULL)
{ printf("open file failed."); return; }
//find word length
for (len = 0; len < (sizeof(buf) - 1); len++)
if (buf[len] == 0 && buf[len + 1] == 0)
{ break; }
len = fwrite(buf, 1, len, f);
fclose(f);
}
}
追问我的目的是测试二维数组,不好意思一位数组我也会,我的想法是看一下汉子在二维数组里是怎么存放于读出的,谢谢