0

Right now, what I have is this:

#include <stdio.h>
#include <conio.h>
#include <string.h>

int main() 
{
    char fname[100];
    FILE* fp;
    memset(fname, 0, 100);

    /* ask user for the name of the file */
    printf("enter file name: ");
    gets(fname);

    fp = fopen(fname, "w");

    /* Checks if the file us unable to be opened, then it shows the 
       error message */
    if (fp == NULL)
        printf("\nError, Unable to open the file for reading\n");
    else
        printf("hello");
    getch();
}

This functions just fine, but is there a way I can force it to save as a .txt or a .data or something? Right now it just saves as the name you put in with no extension. Other than asking the user to just input the name and extension, I can't think of a way to do that. I mean, it still works just fine for reading/writing purposes, I just think an extension would be nice.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
hego64
  • 345
  • 1
  • 5
  • 17
  • 4
    man strcat..... – pm100 Feb 23 '17 at 01:27
  • 1
    1) `#include` non-standard header 2) `int main(void)` 3) `memset(fname, 0, 100);` : nonsense 4) `printf("enter file name: ");` wont be flushed, add a '\n' 5) `gets(fname);` dont use gets. 6) `getch();` : non standard function 7) `}` main should return an int; yours does not return anything. 8) What was your question, again? – wildplasser Feb 23 '17 at 01:35
  • @wildplasser I copied that from here: http://stackoverflow.com/a/25950349/7471023 I wasn't trying to make it perfect, just functional. – hego64 Feb 23 '17 at 01:37
  • @wildplasser: Starting with C99, falling of the end of `main` is equivalent to `return 0;`. – Keith Thompson Feb 23 '17 at 02:23
  • You might consider letting users specify whatever file names they want, just as you're doing now. If I want a name ending in `.txt`, I can type `.txt`. If I don't, I don't have to. – Keith Thompson Feb 23 '17 at 02:24
  • If you add `.txt`, you need to make sure the user didn't type `filename.txt` because `filename.txt.txt` is not nice. – Jonathan Leffler Feb 23 '17 at 02:42

3 Answers3

1

to expand my comment:

strcat(fname, ".txt");
pm100
  • 48,078
  • 23
  • 82
  • 145
0

The strcat function can be used to append text to a destination buffer, assuming the destination is large enough to store the new text.

char *strcat(char *destination, const char *source);

The source is the new text that you want to append (in your case the extension), and the destination is where the new text will be added. If destination is not big enough, then the behavior is undefined.

One could also use the snprintf function to append text, which is safer, as it takes a size argument.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
0

I figured it out. Credit goes to a friend of mine who showed this to me earlier today.

int main()
{
    FILE *Fileptr;
    char filename[50];
    char file[50];
    int c;

    printf("What do you want to name your file?");
    scanf("%s", filename);

    sprintf(file, "%s.txt", filename);

    Fileptr = fopen(file, "w");
    fprintf(Fileptr, "Data goes here");
    fclose(Fileptr);

    return 0;
}

Much easier than what I had been doing.

hego64
  • 345
  • 1
  • 5
  • 17