C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-09-2009, 08:13 AM   #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
lenxe is offline   Reply With Quote
Old 03-09-2009, 08:41 AM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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");
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 03-09-2009, 08:46 AM   #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;
}
Meldreth is offline   Reply With Quote
Old 03-09-2009, 08:59 AM   #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
lenxe is offline   Reply With Quote
Reply

Tags
file, filename, fopen, string

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OOP Question DB Access Wrapper Classes digioz C# Programming 2 09-07-2008 04:30 PM
Message class ** Need help befor 12am tonight** TransformedBG C++ Programming 1 11-29-2006 11:03 PM
Linked List Help CJ7Mudrover C Programming 9 03-10-2004 10:33 PM
Another overloading "<<" problem alphaoide C++ Programming 18 09-30-2003 10:32 AM
lvp string... Magma C++ Programming 4 02-27-2003 12:03 AM


All times are GMT -6. The time now is 06:07 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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