Thread: Save a file with a name in a variable?

  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,336
    You could use strcat to concatenate two strings together.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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, 02: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