![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 2
| 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);
Last edited by lenxe; 03-09-2009 at 09:00 AM. Reason: correction |
| lenxe is offline | |
| | #2 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| 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");
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);
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #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 | |
| | #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 | |
![]() |
| Tags |
| file, filename, fopen, string |
| Thread Tools | |
| Display Modes | |
|
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 |