c程序函数无法正常工作

标签 c function file

#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/

相关文章:

c - 使用 n 个参数运行函数

c - 如何更新矢量化汇编(AVX)中的数组?

c++ - 进入函数时堆栈溢出

c - fprintf 消耗字符

C 数据结构 - 存储二叉树的表示

c# - 从 C# (Windows) 调用 C 函数 (Linux)

javascript - 单击时未调用函数内的 Coffeescript 函数

javascript - this 关键字不适用于箭头函数

php - 如何使用此备份脚本排除文件夹?

C#:如何打开选择了多个文件的 Windows 资源管理器窗口