C语言中的文件操作

C语言中的文件操作,c,file,mkdir,C,File,Mkdir,我使用简单的“C”代码执行以下操作: 1) 从.txt文件读取 2) 根据.txt文件中的字符串,将创建一个目录 我无法执行步骤2,因为我不清楚类型转换 这是我的密码: #include <stdio.h> #include <stdlib.h> #include <direct.h> int main() { char ch, file_name[25]; FILE *fp; //printf("Enter the name of fi

我使用简单的“C”代码执行以下操作:

1) 从.txt文件读取

2) 根据.txt文件中的字符串,将创建一个目录

我无法执行步骤2,因为我不清楚类型转换

这是我的密码:

#include <stdio.h>
#include <stdlib.h>
#include <direct.h>

int main()
{
   char ch, file_name[25];
   FILE *fp;

   //printf("Enter the name of file you wish to see\n");
   //gets(file_name);

   fp = fopen("input.txt","r"); // read mode

   if( fp == NULL )
    {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
    }

   printf("The contents of %s file are :\n", file_name);

   while( ( ch = fgetc(fp) ) != EOF )
      printf("%c",ch);

    if( _mkdir(ch ) == 0 )
   {
      printf( "Directory successfully created\n" );
      printf("\n");
   }
   fclose(fp);
   return 0;
}

这是因为您只有一个
char
fgetc
中的
c
代表
char
),而
\u mkdir
需要一个字符串(即
char*


您可能应该改为使用来读取输入。

是的,编译器是正确的

您正在将char
c
传递给
\u mkdir
,而不是字符串

您应该从文件中读取字符串并将其存储到
file\u name
(我想您忘记了),然后

见下文:

#include <stdio.h>
#include <stdlib.h>
#include <direct.h>


int main()
{
    char file_name[25];
    FILE *fp;

    fp = fopen("input.txt", "r"); // read mode

    if (fp == NULL)
    {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    fgets(file_name, 25, fp);

    _mkdir(file_name);

    fclose(fp);
    return 0;
}
#包括
#包括
#包括
int main()
{
字符文件名称[25];
文件*fp;
fp=fopen(“input.txt”,“r”);//读取模式
如果(fp==NULL)
{
perror(“打开文件时出错。\n”);
退出(退出失败);
}
fgets(文件名,25,fp);
_mkdir(文件名);
fclose(fp);
返回0;
}

如果您不想使用FGET,则可以使用此功能

#include <stdio.h>
#include <stdlib.h>
#include <direct.h>
int main()
{
char file_name[25];
String str;
FILE *fp;
char ch;
int i=0;

fp = fopen("input.txt", "r"); // read mode

if (fp == NULL)
{
    perror("Error while opening the file.\n");
    exit(EXIT_FAILURE);
}
 while( ( ch = fgetc(fp) ) != EOF ){
  printf("%c",ch);
  file_name[i];
  i++
}
str=file_name;

_mkdir(str);

fclose(fp);
return 0;
}
#包括
#包括
#包括
int main()
{
字符文件名称[25];
字符串str;
文件*fp;
char ch;
int i=0;
fp=fopen(“input.txt”,“r”);//读取模式
如果(fp==NULL)
{
perror(“打开文件时出错。\n”);
退出(退出失败);
}
而((ch=fgetc(fp))!=EOF){
printf(“%c”,ch);
文件名[i];
我++
}
str=文件名;
_mkdir(str);
fclose(fp);
返回0;
}

我对你的困惑有点困惑
mkdir
接受一个字符串,您只给它一个字符。编译器也在说同样的话……好吧,我添加了这个,但它仍然不工作,
while((ch=fgetc(fp))!=EOF)printf(“%c”,ch);char-str[25]=ch;如果(_mkdir(str)==0){printf(“已成功创建目录”);}
@highlander141,为什么不使用
fgets()
从文件中读取??
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>


int main()
{
    char file_name[25];
    FILE *fp;

    fp = fopen("input.txt", "r"); // read mode

    if (fp == NULL)
    {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    fgets(file_name, 25, fp);

    _mkdir(file_name);

    fclose(fp);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>
int main()
{
char file_name[25];
String str;
FILE *fp;
char ch;
int i=0;

fp = fopen("input.txt", "r"); // read mode

if (fp == NULL)
{
    perror("Error while opening the file.\n");
    exit(EXIT_FAILURE);
}
 while( ( ch = fgetc(fp) ) != EOF ){
  printf("%c",ch);
  file_name[i];
  i++
}
str=file_name;

_mkdir(str);

fclose(fp);
return 0;
}