-
save as file name?
Hi...
i would like to know how one can save the filename according to what the user types...
cin>>whatusertypes;
write.open("whatusertypes.txt",ios::out);
that doesn't seem to work...it'll produce whatusertypes.txt instead of what the user really typed....any ideas??
-
---
---
char fname[12];
cout<<"enter file name : ";
gets(fname);
fout.open(fname,ios::out);
---
---
-
Well duh. You can't put quotes around a string name, then it'll parse it as an actual string...
-
Thanks! it worked fine but how can i let the computer add the extension for the filename entered by the user??
such as *.txt type
-
#include<fstream>
#include<string.h>
fstream write;
char whateverusertypes[10];
cin >> whatusertypes;
write.open(whatusertypes ,ios::out);
strcat(whateverusertypes,".txt");
------
strcat() will append the second parameter (a string) to the first (another string). It requires the header string.h
The Gekko
-
i'm sorry....but the code you gave couldn't work....
it would still save the file name as what the user typed, but without the extension
is there a clue??
-
Try it in this order -
cin >> whatusertypes;
strcat(whateverusertypes,".txt");
write.open(whatusertypes ,ios::out);
Make sure whatusertypes has enough memory allocated for it's description + 4.
-
thanks!! it worked like a charm!
does anyone know how we can make the program read the harddisk
if the required file is not found, then ask the user to create?
-
Hahaha...whoops...sorry about that man. Thats what i get for cut and pasting your code instead of just rewritting my own:)
-
Im not sure what you want. With what your doing the file is created no matter what. If its there its replaced..if not its created. Do you want to ask to replace it if theres already one there instead of just overwritting it?
-
And one more thing...start looking through code you get to understand it instead of just throughing it in your program. It will help you learn a lot. That bug in my program should have been real easy for you to spot if you had looked at it. It makes me look bad, it being so simple to spot:)
-
thanks for the advice
you see i'm a student and i'm doing a project
the thing is, what i've learned in school just isn't enough
thats why i don't know alot of stuffs..
hm about the file thingy, what i wanted is, the program would check the harddisk (well local folder only anyways) and if the file is found.......which is, a year's record, then it'll carry on
if that file is not found, that it prompts the user to create a new record..so is there a way to search for the file's presence?
-
Hmmm...there prob a better way...but you could try to open it and test for an error...if theres no error opning the file you could prob assum it was there and if there was an error than it wasn't there. Not perfect I know, but if noone else speaks up it will prob work for you.
right after you open the file do this,
if (!fileName)
{
error opening...do whatever
}
else
{
found it...do whatever
}
I cant remeber if your going to have to use ios::nocreate or not when opening it...
-
I'm sorry, but i've tried thousands of ways to make the below correct but it just can't work!
int data=add[hold].staff_id;
strcat(c:\windows\desktop\data,".txt");
write.open(data,ios::out);
does this means strcat can only work for characters? my structure staff id is an integer, therefore it can't combine it with the "txt extension. Is there a way to solve this??
btw...the c:\windows bla bla . Do we use "\" or "/" ???
-
Why don't you input the extension while entering the name of file.
like
gets(fname); //myfile.dat
from your code above it looks like you want to save the file in another directory.
use the chdir command in dir.h
chdir("c:\windows\dektop");
--- do the file stuff ---
chdir("???");
here ??? implies the c++ directory, like mine is "c:\langs\tc\bin".
check your help on dir.h for the commands relating to directory.