fopen, include string in filename.

This is a discussion on fopen, include string in filename. within the C Programming forums, part of the General Programming Boards category; Hi i'm trying to get the user to enter a string as the save name then to save it as ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Question fopen, include string in filename.(SOLVED)

    Hi
    i'm trying to get the user to enter a string as the save name then to save it as a text file under that string. I'm a beginer to c but have used other languages if that helps. (this needs to be done in c as it is part of a project) heres what i've guessed at

    Code:
    char teststring[51];
    FILE * testFILE;
    
    /* program code here*/
    
    printf("please enter the save name?");
    		scanf("%s",&teststring);
    
    		testFILE = fopen ("F:\\Programming_output\\"+teststring+".txt","w");
    		fprintf(testFILE, "saved data here");
    		fclose (testFILE);
    i've had a look around and fstream has been mentioned but after trying this i get more errors.
    Last edited by lenxe; 03-09-2009 at 09:00 AM. Reason: correction

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lenxe View Post
    I'm a beginer to c but have used other languages if that helps.
    So you just expect it to be that easy then, eh?

    You can't use variables in C "string literals", unfortunately.
    Code:
    		testFILE = fopen ("F:\\Programming_output\\"+teststring+".txt","w");
    Having come from "other languages" myself I thot this a bit ridiculous at first, but you will have to construct a string if you want to do anything beyond the literal, eg. "F:\\Programming_output\\myfile.txt" is fine. You have at least two choices on how to do this:
    Code:
    OPTION #1
            char fullname[256]="F:\\Programming_output\\";
            strcat(fullname,teststring);
            strcat(fullname,".txt"); 
    OPTION #2
            char fullname[256];
            sprintf(fullname, "F:\\Programming_output\\%s.txt",teststring);
    Then you can use testFILE = fopen (fullname,"w");
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    c doesn't let you concatenate strings with +, you need to format the strings together with sprintf or use something like strcat to do the job. i think sprintf is better for this, so here's an example.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char fileName[51];
        char fullPath[80];
        printf("Save As: ");
        fflush(stdout);
        if (gets(fileName))
        {
            sprintf(fullPath, "F:\\Programming_output\\%s.txt", fileName);
            puts(fullPath);
        }
        return EXIT_SUCCESS;
    }

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    thanks for the quick replies. i've got a working method i used the strcat methoad as i know how this is working. (i've not come across sprintf before.) after seeing this it does make logical sence. thanks again.lewis

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 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