Open file with user input (string) - C

后端 未结 3 674
星月不相逢
星月不相逢 2021-01-22 04:37

I\'m trying to figure out how user input can be used as a filename in C. Here\'s the simple program I wrote.

#include 
#define MAX 100

int main()         


        
相关标签:
3条回答
  • 2021-01-22 04:50

    You have to add ".txt" extension and then it will work.
    The following program opens a txt file in the same directory as the name of the following date:

    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char a[1000000],s[20];
        strcpy(s,__DATE__);
        int i,j,k,l;
        strcat(s,".txt");
        printf("%s",s);
        FILE *f,*ff;
        f=fopen(s,"w");
    }
    
    0 讨论(0)
  • 2021-01-22 04:51

    here's a function that you'll have to add after the fgets to slove this problem

    void newlineRemover(char *array)
    {
        int i , lenght ;
        lenght = strlen(array);
        for(i = 0 ; i < lenght ; i++)
         {
            if(array[i] == '\n')
                array[i] = '\0' ;
         }
    }
    
    0 讨论(0)
  • 2021-01-22 04:55

    fgets retains the newline in the input.

    0 讨论(0)
提交回复
热议问题