#include<stdio.h>
#include<string.h>
void createIndexFile(char[]);
int main()
{
FILE *fp;
char fname[40];
int option;
printf("\nEnter the filename to open: ");
scanf("%s",fname);
fp=fopen(fname,"r");
if(fp==NULL)
{
printf("\nCannot open the file\n");
return 0;
}
else
{
printf("\n%s",fname);
createIndexFile(fname);
}
while(1)
{
printf("\n*****MENU*****\n");
printf("\n1.Display ......\n2.Insert new data\n3.Find data\n4.Display data\n5.Exit\n");
printf("\nChoose an operation: ");
scanf("%d",&option);
switch(option)
{
case 1: break;
case 2: break;
case 3: break;
case 4: break;
case 5: return 0;
default: printf("\nInvalid selection\n");
}
}
}
void createIndexFile(char fname[])
{
int i=0;
char tempFile[40];
char indexFile[40];
printf("\n%s",fname);
strcpy(indexFile,"xyz");
strcpy(tempFile,fname);
while(tempFile[i]!='.')
{
if(tempFile[i]=='/')
tempFile[i]='_';
}
strcat(tempFile,".idx");
strcat(indexFile,tempFile);
printf("\nIndex File Name: %s",indexFile);
}
这是一个程序,输入文件名后假设/home/abc.txt
并且我的名字是xyz
,那么应该创建一个索引文件xyz_home_abc .idx
.
输入文件名后,如果存在,则应转到其他部分和函数
createIndexFile(fname);
应该被调用。但在函数内部,没有任何东西可以正常工作。如果我们打印一个字符或者其他东西,它就会被打印出来。但如果我尝试提供另一个 printf 它就不起作用。
最佳答案
更正了您的功能
void createIndexFile(char fname[])
{
int i=0;
char tempFile[40];
char indexFile[40];
printf("\n%s",fname);
strcpy(indexFile,"");
while(fname[i]!='.')
{
if(fname[i]=='/')
tempFile[i]='_';
else
tempFile[i]=fname[i];
i++;
}
tempFile[i]='\0';
strcat(tempFile,".idx");
strcat(indexFile,tempFile);
printf("\nIndex File Name: %s",indexFile);
}
关于c程序函数无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49233650/