C File I/O

This is a discussion on C File I/O within the C Programming forums, part of the General Programming Boards category; Hi all, I'm learning C for the first time (first time learning any programming language). I'm currently going through the ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    13

    C File I/O

    Hi all, I'm learning C for the first time (first time learning any programming language). I'm currently going through the tutorial on this site. After reading the tutorial on file I/O I've been trying a few things out.
    I'm trying to find a way of asking the user for an input, and then using this input to name a file.

    Originally I had:

    Code:
    #include<stdio.h>
    
    int main(){
    FILE *fp;
    fp=fopen("d:\\name_of_file.txt","w");
    fprintf(fp, "this is text printed in the file");
    getchar();
    return 0;
    }
    Then i want to make this something like the following:

    Code:
    #include<stdio.h>
    
    int main(){
    char x[20];
    FILE *fp;
    printf("Please enter a name for the file...\n");
    fgets(x, 20, stdin);
    fp=fopen("d:\\name_of_file.txt","w");           //here i want x to be the name of the file 
    fprintf(fp, "this is text printed in the file");
    getchar();
    return 0;
    }
    Is there a way of doing something like :
    Code:
    fp=fopen("d:\\%c",x,"w");
    Thanks for any help.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    In short, no. You create a variable for the file name, read the user's choice into that variable, then use the functions shown to you in your previous thread to assemble the full path to the file, which you pass to fopen.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    13
    So create a variable for the filename:
    Code:
    char x [20];
    Read the user's choice into that variable:
    Code:
    fgets(x, 20, stdin)
    But i can't see how this would be passed to fopen...

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    x is a C string, and the first argument to fopen is also a C string, so it is just a matter of using x as an argument. Of course, this does not mean that x is free of problems. You may want to check if (or append one so) the user's string has a certain file extension. You also have to look for and erase the newline fgets places in the string if there is room.

    Handling of files poses its own challenges, and that is why there are so many tutorials in our FAQ about it.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also don't forget to call fclose on that FILE pointer after you are done with everything.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You can use strxxx functions from string.h or use snprintf().
    Eg.
    Code:
    char file_name[50];
    /* don't forget to strip newline char in x if there's any */
    snprintf(file_name,sizeof(file_name),"%s%s.%s","d:\\\\",x,"txt");
    fp = fopen(file_name,"w");
    If you're wondering why \\\\, answer.
    Last edited by Bayint Naung; 05-31-2010 at 08:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21