Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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
"E;%FF";目录错误?似乎是由于触摸[C]_C_String_File Io - Fatal编程技术网

"E;%FF";目录错误?似乎是由于触摸[C]

"E;%FF";目录错误?似乎是由于触摸[C],c,string,file-io,C,String,File Io,我正在做一个C项目,用户可以通过这个程序直接创建一个博客。我正在设置输出HTML和CSS文件,但似乎无法修复我遇到的错误 明确地说,编译器不会发出错误或警告 错误是这样的:对于我输入的一些(并非所有)博客标题,目录名会被附加%8B%FF,或者只是%FF,或者其他一些变体,但总是以%FF结尾。此文件夹不包含任何文件,在计算机重新启动之前无法删除。当错误未出现时,文件夹中存在文件 我包含了我的大部分代码,因为我真的不理解这个错误我认为错误在CreateNewBlog()的底部,在那里我需要创建HTM

我正在做一个C项目,用户可以通过这个程序直接创建一个博客。我正在设置输出HTML和CSS文件,但似乎无法修复我遇到的错误

明确地说,编译器不会发出错误或警告

错误是这样的:对于我输入的一些(并非所有)博客标题,目录名会被附加
%8B%FF
,或者只是
%FF
,或者其他一些变体,但总是以
%FF
结尾。此文件夹不包含任何文件,在计算机重新启动之前无法删除。当错误未出现时,文件夹中存在文件

我包含了我的大部分代码,因为我真的不理解这个错误我认为错误在
CreateNewBlog()
的底部,在那里我需要创建HTML和CSS文件。如果没有这些行,则不会发生错误。

那么,在我的目录末尾的
%FF
是什么意思,为什么它会出现

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void CreateNewBlog();
void NewBlogHTML(char path[92], char blog_title[64]);
void NewBlogCSS(char path[92], char blog_title[64]);

int main()
{
  CreateNewBlog();

  return 0;
}

void CreateNewBlog()
{
  char blog_title[64];
  char dir_name[64];
  char mkdir[70];
  char touch[70];
  char html_path[92];
  char css_path[92];
  int i = 0;

  printf("Enter blog title: ");

  fgets(blog_title, 64, stdin);
  blog_title[strlen(blog_title) - 1] = '\0';  // removes trailing '\n' from fgets()

  for (i = 0; blog_title[i]; i++) {           // convert "Blog Title" to "blog-title"
    if (blog_title[i] == ' ') {
      dir_name[i] = '-';
    } else dir_name[i] = tolower(blog_title[i]);
  }

  dir_name[strlen(dir_name)] = '\0';

  sprintf(mkdir, "mkdir %s", dir_name);
  system(mkdir);

  sprintf(touch, "touch %s/index.html", dir_name);
  system(touch);

  sprintf(html_path, "%s/index.html", dir_name);
  sprintf(css_path, "%s/style.css", dir_name);
  NewBlogHTML(html_path, blog_title);  // HERE IS THE PROBLEM (when uncommented)
  NewBlogCSS(css_path, blog_title);    //
}

void NewBlogHTML(char path[92], char blog_title[64])
{
  FILE *index = fopen(path, "w");
  // html file text
  fclose(index);
}

void NewBlogCSS(char path[92], char blog_title[64])
{
  FILE *style = fopen(path, "w");
  // css file text
  fclose(style);
}
#包括
#包括
#包括
#包括
void CreateNewBlog();
void NewBlogHTML(char路径[92],char blog_标题[64]);
void NewBlogCSS(char路径[92],char blog_标题[64]);
int main()
{
CreateNewBlog();
返回0;
}
void CreateNewBlog()
{
char blog_title[64];
char dir_name[64];
char-mkdir[70];
触摸字符[70];
char html_path[92];
char css_path[92];
int i=0;
printf(“输入博客标题:”);
fgets(博客标题,64,标准文本);
blog_title[strlen(blog_title)-1]='\0';//从fgets()中删除尾随'\n'
对于(i=0;blog_title[i];i++){//将“blog title”转换为“blog title”
如果(博客标题[i]=''){
目录名[i]='-';
}else dir_name[i]=tolower(blog_title[i]);
}
目录名[strlen(目录名)]='\0';
sprintf(mkdir,“mkdir%s”,目录名);
系统(mkdir);
sprintf(touch,“touch%s/index.html”,目录名);
系统(触摸屏);
sprintf(html\u路径,“%s/index.html”,目录名);
sprintf(css_路径,“%s/style.css”,目录名);
NewBlogHTML(html_路径,blog_标题);//这里是问题所在(未注释时)
NewBlogCSS(css路径、博客标题)//
}
void NewBlogHTML(char路径[92],char blog_标题[64])
{
文件*索引=fopen(路径,“w”);
//html文件文本
fclose(索引);
}
void NewBlogCSS(char路径[92],char blog_标题[64])
{
文件*style=fopen(路径,“w”);
//css文件文本
fclose(风格);
}
这里有一个问题:

for (i = 0; blog_title[i]; i++) {           // convert "Blog Title" to "blog-title"
  if (blog_title[i] == ' ') {
    dir_name[i] = '-';
  } else dir_name[i] = tolower(blog_title[i]);
}
写入
dir\u name[]
的最后一个字符决不是
'\0'
,因为当
blog\u title[i]
'\0'
时,循环体不会执行

您需要显式地将
'\0'
附加到
目录名[]
。如果不这样做,
dir\u name[]
可能在写入它的所有字符后包含垃圾,只有在某个时候,它才会有
'\0'
,这可能是函数返回地址的一部分,也可能是堆栈上存储的另一个变量的一部分

另一个不相关的奇怪之处是:

sprintf(html_path, "%s/index.html/", dir_name);
为什么文件名以
'/'
结尾?

表示(i=0;blog_title[i];i++){//将“blog title”转换为“blog title”
 for (i = 0; blog_title[i]; i++) {           // convert "Blog Title" to "blog-title"
    if (blog_title[i] == ' ') {
      dir_name[i] = '-';
    } else dir_name[i] = tolower(blog_title[i]);
  }

   dir_name[i] = '\0'; // <<-- Add this line
如果(博客标题[i]=''){ 目录名[i]='-'; }else dir_name[i]=tolower(blog_title[i]); }
目录名称[i]='\0';//后面的斜杠已经消除,我添加了行
dir_name[strlen(dir_name)]='\0'在循环之后,问题仍然存在。同样的错误。这不是一个非常聪明的解决方法。想想看。
strlen()
是如何工作的?它查找
“\0”
,并计算前面的字符数。但是您尚未将
'\0'
写入
目录名[]
。因此,此行(希望)会找到一些
'\0'
(可能在写入
dir\u name[]
的有效字符之后很长一段时间)并用
'\0'
覆盖它,完全不做任何更改。您希望在循环中写入的最后一个字符之后立即将
'\0'
放入
dir\u name[]
。您可以使用
strlen(blog_title)
i
作为写入
'\0'
dir_name[strlen(dir_name)]='\0'的位置是假的。strlen()的计数一直到第一个'\0',因此不需要在那里放置'\0'。