Save a file with a name in a variable?

This is a discussion on Save a file with a name in a variable? within the C Programming forums, part of the General Programming Boards category; Hi, i need to know how to do something that might be really easy, but i just don't know how. ...

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Baires
    Posts
    12

    Save a file with a name in a variable?

    Hi, i need to know how to do something that might be really easy, but i just don't know how.
    The thing is that i open a file allocated, for example in," C:\example " and i need to save it in another path such as " C:\example\folder"

    But when i open the file it has a previous name (zippeddata.txt), and in the program i ask the user to enter a name to save the modified file, in the new path.

    The thing is that i cant make it work when the user writes the new name of the file, i dont know the code to do that.

    Code:
    #include <stdio.h>
    main() {
    FILE *original, *copy;
    char c, name[20];
    original = fopen ("C:\\example\\zippedData", "r");
    printf ("File name?"); //Input name to save the file//
    scanf ("%s",name);
    printf ("%s\n", name);
    copy = fopen ("C:\\example\\folder\\ *** I DON'T  KNOW WHAT GOES HERE *** .txt","w"); //Creates new file, which must have the "name" variable//
    while ((c = getc(original)) != EOF)
    if (c != ';')
    putc(c, copy);
    else putc (',', copy);
    fclose (original);
    fclose (copy);
    return 0;
    }
    Please Help!!

    Im sorry if my English it's not so good... it's not my native language

    Cheers!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You could use strcat to concatenate two strings together.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    You might want something like this:
    Code:
    char copyname[300];
    ...
    sprintf(copyname, "C:\\example\\folder\\%s.txt", name);
    copy = fopen(copyname,"w");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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