C语言将字符写入文件失败是什么原因
用fopen以“w”方式打开文件,如果文件不存在,本应该新建文件,然而在这个子程序中实际并没有新建,求问会是什么原因
(这里在做的是把以head为头的链表每一节点中存有0/1编码的数据域code依次每8位以字节封装输出)
根据对待输入字符的(拓展)ASCII码判断确定应该逻辑没有问题,就是没有能新建文件并写入
子函数代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structure.h"
void writebit ( struct character * head)
{
FILE * fp;
char fincode;
unsigned char word;
int i;
int count;
int code;
struct character * curr;
fp = fopen("C:\\Users\\Administrator\\Desktop\\Encode.dat","w");
curr = head;
count = 0;
word = '\0';
fincode = '\0';
if(fp==NULL)
{
printf("打开文件失败\n");
exit(0);
}
for(curr=head;curr!=NULL;curr=curr->next)
{
for(i=0;curr->code[i]!='\0';i++)
{
fincode = curr->code[i];
count++;
code = fincode - '0';
word <<= 1;
word = word | code;
if(count==8)
{
//fwrite(&word,sizeof(char),1,fp);
fprintf(fp,"%c",word);
printf("%d\n",word);
count = 0;
word = '\0';
fincode = '\0';
}
}
}
if(count != 8)
{
word = word <<= (8-count);
//fwrite(&word,sizeof(char),1,fp);
fprintf(fp,"%c",word);
printf("%d\n",word);
}
fclose(fp);
}